|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of HackzillaTicketBundle package. |
| 5 | + * |
| 6 | + * (c) Daniel Platt <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Hackzilla\Bundle\TicketBundle\Command; |
| 13 | + |
| 14 | +use Doctrine\Persistence\ObjectRepository; |
| 15 | +use Hackzilla\Bundle\TicketBundle\Model\UserInterface; |
| 16 | + |
| 17 | +/** |
| 18 | + * NEXT_MAJOR: Inject the object repository and the user class directly in the command |
| 19 | + * classes and remove this trait. |
| 20 | + * |
| 21 | + * @author Javier Spagnoletti <[email protected]> |
| 22 | + */ |
| 23 | +trait UserManagerTrait |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var ObjectRepository |
| 27 | + */ |
| 28 | + private $userRepository; |
| 29 | + |
| 30 | + public function __construct(string $name = null, ?ObjectRepository $userRepository = null) |
| 31 | + { |
| 32 | + parent::__construct($name); |
| 33 | + |
| 34 | + $this->userRepository = $userRepository; |
| 35 | + |
| 36 | + if (null === $userRepository) { |
| 37 | + @trigger_error(sprintf( |
| 38 | + 'Omitting or passing null as argument 2 for "%s()" is deprecated since hackzilla/ticket-bundle 3.x.', |
| 39 | + __METHOD__ |
| 40 | + ), E_USER_DEPRECATED); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private function findUser(string $username): ?UserInterface |
| 45 | + { |
| 46 | + if (null !== $this->userRepository) { |
| 47 | + return $this->userRepository->findBy(['username' => $username]); |
| 48 | + } |
| 49 | + |
| 50 | + if (!$this->getContainer()->has('fos_user.user_manager')) { |
| 51 | + throw new \RuntimeException(sprintf('Command "%s" requires the service "fos_user.user_manager". Is "friendsofsymfony/user-bundle" installed and enabled?', $this->getName())); |
| 52 | + } |
| 53 | + |
| 54 | + return $this->getContainer()->get('fos_user.user_manager')->findUserByUsername($username); |
| 55 | + } |
| 56 | +} |
0 commit comments