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

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

duplicate/similar methods.

Duplication Informational

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) {
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)
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
        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)
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
        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