These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Kunstmaan\AdminBundle\Helper\Security\OAuth; |
||
4 | |||
5 | use Doctrine\ORM\EntityManagerInterface; |
||
6 | use FOS\UserBundle\Model\GroupInterface; |
||
7 | use Kunstmaan\AdminBundle\Entity\User; |
||
8 | |||
9 | class OAuthUserCreator implements OAuthUserCreatorInterface |
||
10 | { |
||
11 | /** @var EntityManagerInterface */ |
||
12 | private $em; |
||
13 | |||
14 | /** @var array */ |
||
15 | private $hostedDomains; |
||
16 | |||
17 | /** @var string */ |
||
18 | private $userClass; |
||
19 | |||
20 | /** @var OAuthUserFinderInterface */ |
||
21 | private $userFinder; |
||
22 | |||
23 | /** |
||
24 | * OAuthUserCreator constructor. |
||
25 | * |
||
26 | * @param EntityManagerInterface $em |
||
27 | * @param array $hostedDomains |
||
28 | * @param string $userClass |
||
29 | * @param OAuthUserFinderInterface $userFinder |
||
30 | */ |
||
31 | 4 | public function __construct(EntityManagerInterface $em, $hostedDomains, $userClass, OAuthUserFinderInterface $userFinder) |
|
32 | { |
||
33 | 4 | $this->em = $em; |
|
34 | 4 | $this->hostedDomains = $hostedDomains; |
|
35 | 4 | $this->userClass = $userClass; |
|
36 | 4 | $this->userFinder = $userFinder; |
|
37 | 4 | } |
|
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | 3 | public function getOrCreateUser($email, $googleId) |
|
43 | { |
||
44 | 3 | if ($this->isConfiguredDomain($email)) { |
|
45 | 2 | $user = $this->userFinder->findUserByGoogleSignInData($email, $googleId); |
|
46 | |||
47 | 2 | if (!$user instanceof $this->userClass) { |
|
48 | //User not present in database, create new one |
||
49 | /** @var User $user */ |
||
50 | 1 | $user = new $this->userClass(); |
|
51 | 1 | $user->setUsername($email); |
|
52 | 1 | $user->setEmail($email); |
|
53 | 1 | $user->setPlainPassword($googleId.$email.time()); |
|
54 | 1 | $user->setEnabled(true); |
|
55 | 1 | $user->setAdminLocale('en'); |
|
56 | 1 | $user->setPasswordChanged(true); |
|
57 | } |
||
58 | |||
59 | 2 | foreach ($this->getAccessLevels($email) as $accessLevel) { |
|
0 ignored issues
–
show
|
|||
60 | /** @var GroupInterface $group */ |
||
61 | 2 | $group = $this->em->getRepository('KunstmaanAdminBundle:Group')->findOneBy(['name' => $accessLevel]); |
|
62 | 2 | if (null !== $group) { |
|
63 | 2 | $user->addGroup($group); |
|
64 | } |
||
65 | } |
||
66 | 2 | $user->setGoogleId($googleId); |
|
67 | |||
68 | // Persist |
||
69 | 2 | $this->em->persist($user); |
|
70 | 2 | $this->em->flush(); |
|
71 | } |
||
72 | |||
73 | 3 | return isset($user) ? $user : null; |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * This method returns the access level coupled with the domain of the given email |
||
78 | * If the given domain name has not been configured this function will return null |
||
79 | * |
||
80 | * @param string $email |
||
81 | * |
||
82 | * @return string[]|null |
||
83 | */ |
||
84 | 3 | View Code Duplication | private function getAccessLevels($email) |
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
85 | { |
||
86 | 3 | foreach ($this->hostedDomains as $hostedDomain) { |
|
87 | 3 | if (preg_match('/'.$hostedDomain['domain_name'].'$/', $email)) { |
|
88 | 2 | return $hostedDomain['access_levels']; |
|
89 | } |
||
90 | } |
||
91 | |||
92 | 1 | return null; |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * This method returns wether a domain for the given email has been configured |
||
97 | * |
||
98 | * @param string $email |
||
99 | * |
||
100 | * @return bool |
||
101 | */ |
||
102 | 3 | View Code Duplication | private function isConfiguredDomain($email) |
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
103 | { |
||
104 | 3 | foreach ($this->hostedDomains as $hostedDomain) { |
|
105 | 3 | if (preg_match('/'.$hostedDomain['domain_name'].'$/', $email)) { |
|
106 | 2 | return true; |
|
107 | } |
||
108 | } |
||
109 | |||
110 | 1 | return false; |
|
111 | } |
||
112 | } |
||
113 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.