Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

Helper/Security/OAuth/OAuthUserCreator.php (1 issue)

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\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
    public function __construct(EntityManagerInterface $em, $hostedDomains, $userClass, OAuthUserFinderInterface $userFinder)
32
    {
33
        $this->em = $em;
34
        $this->hostedDomains = $hostedDomains;
35
        $this->userClass = $userClass;
36
        $this->userFinder = $userFinder;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getOrCreateUser($email, $googleId)
43
    {
44
        if ($this->isConfiguredDomain($email)) {
45
            $user = $this->userFinder->findUserByGoogleSignInData($email, $googleId);
46
47
            if (!$user instanceof $this->userClass) {
48
                //User not present in database, create new one
49
                /** @var User $user */
50
                $user = new $this->userClass();
51
                $user->setUsername($email);
52
                $user->setEmail($email);
53
                $user->setPlainPassword($googleId.$email.time());
54
                $user->setEnabled(true);
55
                $user->setAdminLocale('en');
56
                $user->setPasswordChanged(true);
57
            }
58
59
            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...
60
                /** @var GroupInterface $group */
61
                $group = $this->em->getRepository('KunstmaanAdminBundle:Group')->findOneBy(['name' => $accessLevel]);
62
                if (null !== $group) {
63
                    $user->addGroup($group);
64
                }
65
            }
66
            $user->setGoogleId($googleId);
67
68
            // Persist
69
            $this->em->persist($user);
70
            $this->em->flush();
71
        }
72
73
        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 View Code Duplication
    private function getAccessLevels($email)
85
    {
86
        foreach ($this->hostedDomains as $hostedDomain) {
87
            if (preg_match('/'.$hostedDomain['domain_name'].'$/', $email)) {
88
                return $hostedDomain['access_levels'];
89
            }
90
        }
91
92
        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 View Code Duplication
    private function isConfiguredDomain($email)
103
    {
104
        foreach ($this->hostedDomains as $hostedDomain) {
105
            if (preg_match('/'.$hostedDomain['domain_name'].'$/', $email)) {
106
                return true;
107
            }
108
        }
109
110
        return false;
111
    }
112
}
113