|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the CCDNUser ProfileBundle |
| 5 | + * |
| 6 | + * (c) CCDN (c) CodeConsortium <http://www.codeconsortium.com/> |
| 7 | + * |
| 8 | + * Available on github <http://www.github.com/codeconsortium/> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CCDNUser\ProfileBundle\Command; |
| 15 | + |
| 16 | +use Symfony\Component\Console\Command\Command; |
| 17 | +use Symfony\Component\Console\Input\InputInterface; |
| 18 | +use Symfony\Component\Console\Output\OutputInterface; |
| 19 | +use CCDNUser\ProfileBundle\Entity\Factory\ProfileFactoryInterface; |
| 20 | +use CCDNUser\ProfileBundle\Model\FrontModel\UserModel; |
| 21 | +use CCDNUser\ProfileBundle\Model\FrontModel\ProfileModel; |
| 22 | + |
| 23 | +/** |
| 24 | + * Locates users without an attached profile, and attaches a default profile to them. |
| 25 | + * |
| 26 | + * @category CCDNUser |
| 27 | + * @package ProfileBundle |
| 28 | + * |
| 29 | + * @author Maarten Jacobs <maarten.j.jacobs@gmail.com> |
| 30 | + * @license http://opensource.org/licenses/MIT MIT |
| 31 | + * @version Release: 2.0 |
| 32 | + * @link https://github.com/codeconsortium/CCDNUserProfileBundle |
| 33 | + * |
| 34 | + */ |
| 35 | +class MigrateUsersCommand extends Command |
| 36 | +{ |
| 37 | + /** |
| 38 | + * @var UserModel |
| 39 | + */ |
| 40 | + private $userModel; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var ProfileModel |
| 44 | + */ |
| 45 | + private $profileModel; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var ProfileFactoryInterface |
| 49 | + */ |
| 50 | + private $profileFactory; |
| 51 | + |
| 52 | + public function __construct( |
| 53 | + UserModel $userModel, |
| 54 | + ProfileModel $profileModel, |
| 55 | + ProfileFactoryInterface $profileFactory |
| 56 | + ) { |
| 57 | + parent::__construct(); |
| 58 | + |
| 59 | + $this->userModel = $userModel; |
| 60 | + $this->profileModel = $profileModel; |
| 61 | + $this->profileFactory = $profileFactory; |
| 62 | + } |
| 63 | + |
| 64 | + protected function configure() |
| 65 | + { |
| 66 | + parent::configure(); |
| 67 | + |
| 68 | + $this->setName('ccdn-user-profile:migrate-users') |
| 69 | + ->setDescription('Creates a default profile for users without a profile.'); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @param InputInterface $input |
| 74 | + * @param OutputInterface $output |
| 75 | + * @return null |
| 76 | + */ |
| 77 | + public function execute(InputInterface $input, OutputInterface $output) |
| 78 | + { |
| 79 | + // Locate users without a profile. |
| 80 | + $profilelessUsers = $this->userModel->findAllUsersWithoutProfiles(); |
| 81 | + $output->writeln(sprintf('Migrating %d users without profiles.', count($profilelessUsers))); |
| 82 | + |
| 83 | + // Attach a profile to each of them. |
| 84 | + foreach ($profilelessUsers as $user) { |
| 85 | + $profile = $this->profileFactory->createDefaultProfile(); |
| 86 | + $profile->setUser($user); |
| 87 | + $user->setProfile($profile); |
| 88 | + $this->userModel->saveUser($user); |
| 89 | + $this->profileModel->saveProfile($profile); |
| 90 | + } |
| 91 | + |
| 92 | + // Notify the user we're done. |
| 93 | + $output->writeln(sprintf('Migrated %d users without profiles.', count($profilelessUsers))); |
| 94 | + } |
| 95 | +} |
0 commit comments