1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gbere\SimpleAuth\Command; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
8
|
|
|
use Gbere\SimpleAuth\Entity\Role; |
9
|
|
|
use Gbere\SimpleAuth\Model\UserInterface; |
10
|
|
|
use Gbere\SimpleAuth\Repository\AdminUserRepository; |
11
|
|
|
use Gbere\SimpleAuth\Repository\UserRepository; |
12
|
|
|
use Symfony\Component\Console\Command\Command; |
13
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
14
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
15
|
|
|
use Symfony\Contracts\Service\ServiceSubscriberInterface; |
16
|
|
|
use Symfony\Contracts\Service\ServiceSubscriberTrait; |
17
|
|
|
|
18
|
|
|
abstract class AbstractCommand extends Command implements ServiceSubscriberInterface |
19
|
|
|
{ |
20
|
|
|
use ServiceSubscriberTrait; |
21
|
|
|
|
22
|
|
|
/** @var UserRepository */ |
23
|
|
|
protected $userRepository; |
24
|
|
|
/** @var AdminUserRepository */ |
25
|
|
|
protected $adminUserRepository; |
26
|
|
|
/** @var ValidatorInterface */ |
27
|
|
|
protected $validator; |
28
|
|
|
|
29
|
|
|
public function __construct( |
30
|
|
|
UserRepository $userRepository, |
31
|
|
|
AdminUserRepository $adminUserRepository, |
32
|
|
|
ValidatorInterface $validator, |
33
|
|
|
string $name = null |
34
|
|
|
) { |
35
|
|
|
$this->userRepository = $userRepository; |
36
|
|
|
$this->adminUserRepository = $adminUserRepository; |
37
|
|
|
$this->validator = $validator; |
38
|
|
|
|
39
|
|
|
parent::__construct($name); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function findUserByEmail(string $email): ?UserInterface |
43
|
|
|
{ |
44
|
|
|
return $this->userRepository->findOneBy(['email' => $email]); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function findRoleByName(string $name): ?Role |
48
|
|
|
{ |
49
|
|
|
return $this->getEntityManager()->getRepository(Role::class)->findOneBy(['name' => $name]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return Role[] |
54
|
|
|
*/ |
55
|
|
|
protected function findAllRoles() |
56
|
|
|
{ |
57
|
|
|
return $this->getEntityManager()->getRepository(Role::class)->findAll(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function getEntityManager(): EntityManagerInterface |
61
|
|
|
{ |
62
|
|
|
return $this->container->get(__METHOD__); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function getParameterBag(): ParameterBagInterface |
66
|
|
|
{ |
67
|
|
|
return $this->container->get(__METHOD__); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function isTestEnv(): bool |
71
|
|
|
{ |
72
|
|
|
return 'test' === $this->getParameterBag()->get('kernel.environment'); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|