Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

Helper/Security/OAuth/OAuthUserCreator.php (3 issues)

Upgrade to new PHP Analysis Engine

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\Group;
8
use Kunstmaan\AdminBundle\Entity\User;
9
10
class OAuthUserCreator implements OAuthUserCreatorInterface
11
{
12
    /** @var EntityManagerInterface */
13
    private $em;
14
15
    /** @var array */
16
    private $hostedDomains;
17
18
    /** @var string */
19
    private $userClass;
20
21
    /** @var OAuthUserFinderInterface */
22
    private $userFinder;
23
24
    /**
25
     * OAuthUserCreator constructor.
26
     *
27
     * @param array  $hostedDomains
28
     * @param string $userClass
29
     */
30 4
    public function __construct(EntityManagerInterface $em, $hostedDomains, $userClass, OAuthUserFinderInterface $userFinder)
31
    {
32 4
        $this->em = $em;
33 4
        $this->hostedDomains = $hostedDomains;
34 4
        $this->userClass = $userClass;
35 4
        $this->userFinder = $userFinder;
36 4
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 3
    public function getOrCreateUser($email, $googleId)
42
    {
43 3
        if ($this->isConfiguredDomain($email)) {
44 2
            $user = $this->userFinder->findUserByGoogleSignInData($email, $googleId);
45
46 2
            if (!$user instanceof $this->userClass) {
47
                //User not present in database, create new one
48
                /** @var User $user */
49 1
                $user = new $this->userClass();
50 1
                $user->setUsername($email);
51 1
                $user->setEmail($email);
52 1
                $user->setPlainPassword($googleId . $email . time());
53 1
                $user->setEnabled(true);
54 1
                $user->setAdminLocale('en');
55 1
                $user->setPasswordChanged(true);
56
            }
57
58 2
            foreach ($this->getAccessLevels($email) as $accessLevel) {
0 ignored issues
show
The expression $this->getAccessLevels($email) of type array<integer,string>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. 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:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
59
                /** @var GroupInterface $group */
60 2
                $group = $this->em->getRepository(Group::class)->findOneBy(['name' => $accessLevel]);
61 2
                if (null !== $group) {
62 2
                    $user->addGroup($group);
63
                }
64
            }
65 2
            $user->setGoogleId($googleId);
66
67
            // Persist
68 2
            $this->em->persist($user);
69 2
            $this->em->flush();
70
        }
71
72 3
        return isset($user) ? $user : null;
73
    }
74
75
    /**
76
     * This method returns the access level coupled with the domain of the given email
77
     * If the given domain name has not been configured this function will return null
78
     *
79
     * @param string $email
80
     *
81
     * @return string[]|null
82
     */
83 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...
84
    {
85 3
        foreach ($this->hostedDomains as $hostedDomain) {
86 3
            if (preg_match('/' . $hostedDomain['domain_name'] . '$/', $email)) {
87 2
                return $hostedDomain['access_levels'];
88
            }
89
        }
90
91 1
        return null;
92
    }
93
94
    /**
95
     * This method returns wether a domain for the given email has been configured
96
     *
97
     * @param string $email
98
     *
99
     * @return bool
100
     */
101 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...
102
    {
103 3
        foreach ($this->hostedDomains as $hostedDomain) {
104 3
            if (preg_match('/' . $hostedDomain['domain_name'] . '$/', $email)) {
105 2
                return true;
106
            }
107
        }
108
109 1
        return false;
110
    }
111
}
112