Passed
Push — develop ( a295c8...769ac9 )
by Mario
02:41
created

IndividualProvider::loadUserByOAuthUserResponse()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 68
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 49
nc 7
nop 1
dl 0
loc 68
rs 8.1793
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace AppBundle\Security\Provider\OAuth;
4
5
use AppBundle\Entity\OAuth;
6
use AppBundle\Service\OAuthService;
7
use AppBundle\Service\RegistrationService;
8
use Ds\Component\Config\Service\ConfigService;
9
use Ds\Component\Identity\Model\Identity;
10
use Exception;
11
use FOS\UserBundle\Model\UserManagerInterface;
12
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
13
use HWI\Bundle\OAuthBundle\Security\Core\Exception\AccountNotLinkedException;
14
use HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider;
15
use Symfony\Component\Security\Core\User\UserInterface;
16
17
/**
18
 * Class IndividualProvider
19
 */
20
class IndividualProvider extends FOSUBUserProvider
21
{
22
    /**
23
     * @var \AppBundle\Service\OAuthService
24
     */
25
    protected $oAuthService;
26
27
    /**
28
     * @var \AppBundle\Service\RegistrationService
29
     */
30
    protected $registrationService;
31
32
    /**
33
     * @var \Ds\Component\Config\Service\ConfigService
34
     */
35
    protected $configService;
36
37
    /**
38
     * Constructor
39
     *
40
     * @param \FOS\UserBundle\Model\UserManagerInterface $userManager
41
     * @param array $properties
42
     * @param \AppBundle\Service\OAuthService $oAuthService
43
     * @param \AppBundle\Service\RegistrationService $registrationService
44
     * @param \Ds\Component\Config\Service\ConfigService $configService
45
     */
46
    public function __construct(UserManagerInterface $userManager, array $properties, OAuthService $oAuthService, RegistrationService $registrationService, ConfigService $configService)
47
    {
48
        parent::__construct($userManager, $properties);
49
        $this->oAuthService = $oAuthService;
50
        $this->registrationService = $registrationService;
51
        $this->configService = $configService;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function loadUserByOAuthUserResponse(UserResponseInterface $response)
58
    {
59
        $oAuth = $this->oAuthService->getRepository()->findOneBy([
60
            'type' => $response->getResourceOwner()->getName(),
61
            'identifier' => $response->getUsername()
62
        ]);
63
64
        if ($oAuth) {
65
            $oAuth->setToken($response->getAccessToken());
66
            $user = $oAuth->getUser();
67
        } else {
68
            $email = $response->getEmail();
69
70
            if (null === $email) {
71
                throw new AccountNotLinkedException('Email is not defined.');
72
            }
73
74
            $user = $this->userManager->findUserByUsernameOrEmail($response->getEmail());
75
76
            if ($user) {
0 ignored issues
show
introduced by
$user is of type FOS\UserBundle\Model\UserInterface, thus it always evaluated to true.
Loading history...
77
                throw new AccountNotLinkedException('Username is not available.');
78
            }
79
80
            $owner = substr($response->getResourceOwner()->getName(), 11);
81
            $data = [
82
                '%email%' => $response->getEmail(),
83
                '%firstName%' => $response->getFirstName(),
84
                '%lastName%' => $response->getLastName()
85
            ];
86
87
            switch ($owner) {
88
                case 'github':
89
                    break;
90
91
                case 'google':
92
                    break;
93
94
                case 'twitter':
95
                    break;
96
            }
97
98
            $registration = $this->registrationService->createInstance();
99
            $registration
100
                ->setOwner($this->configService->get('app.registration.individual.owner.type'))
101
                ->setOwnerUuid($this->configService->get('app.registration.individual.owner.uuid'))
102
                ->setIdentity(Identity::INDIVIDUAL)
103
                ->setUsername($email)
104
                ->setPassword(sha1(uniqid().microtime()))
105
                ->setData(json_decode(strtr($this->configService->get('app.registration.individual.data.'.$owner), $data), true));
106
            $manager = $this->registrationService->getManager();
107
            $manager->persist($registration);
108
            $manager->flush();
109
            $user = $registration->getUser();
110
            $oAuth = new OAuth;
111
            $oAuth
112
                ->setUser($user)
113
                ->setType($response->getResourceOwner()->getName())
114
                ->setIdentifier($response->getUsername())
115
                ->setToken($response->getAccessToken())
116
                ->setOwner($registration->getOwner())
117
                ->setOwnerUuid($registration->getOwnerUuid());
118
        }
119
120
        $manager = $this->oAuthService->getManager();
121
        $manager->persist($oAuth);
122
        $manager->flush();
123
124
        return $user;
125
    }
126
127
    /**
128
     * {@inheritDoc}
129
     */
130
    public function connect(UserInterface $user, UserResponseInterface $response)
131
    {
132
        throw new Exception('Not yet implemented.');
133
    }
134
135
    /**
136
     * {@inheritDoc}
137
     */
138
    public function disconnect(UserInterface $user, UserResponseInterface $response)
139
    {
140
        throw new Exception('Not yet implemented.');
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146
    public function refreshUser(UserInterface $user)
147
    {
148
        throw new Exception('Not yet implemented.');
149
    }
150
}
151