OAuthUserProvider::loadUserByOAuthUserResponse()   B
last analyzed

Complexity

Conditions 9
Paths 34

Size

Total Lines 66
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 66
rs 7.5353
c 0
b 0
f 0
cc 9
nc 34
nop 1

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 Application\Bundle\UserBundle\Security;
4
5
use Application\Bundle\DefaultBundle\Exception\NeedUserDataException;
6
use Application\Bundle\UserBundle\Entity\User;
7
use FOS\UserBundle\Model\UserManagerInterface;
8
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
9
use HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider as BaseClass;
10
use Symfony\Component\DependencyInjection\Container;
11
use Symfony\Component\Security\Core\User\UserChecker;
12
use Symfony\Component\Security\Core\User\UserInterface;
13
14
/**
15
 * Class OAuthUserProvider.
16
 */
17
class OAuthUserProvider extends BaseClass
18
{
19
    private $container;
20
21
    /**
22
     * OAuthUserProvider constructor.
23
     *
24
     * @param UserManagerInterface $userManager
25
     * @param array                $properties
26
     * @param Container            $container
27
     */
28
    public function __construct(UserManagerInterface $userManager, array $properties, $container)
29
    {
30
        parent::__construct($userManager, $properties);
31
32
        $this->container = $container;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function loadUserByOAuthUserResponse(UserResponseInterface $response)
39
    {
40
        $socialID = $response->getUsername();
41
        /** @var User $user */
42
        $user = $socialID ? $this->userManager->findUserBy([$this->getProperty($response) => $socialID]) : null;
43
        $email = $response->getEmail();
44
        if (!$user) {
0 ignored issues
show
introduced by
$user is of type Application\Bundle\UserBundle\Entity\User, thus it always evaluated to true.
Loading history...
45
            $user = $this->userManager->findUserByEmail($email);
46
47
            if (!$user || !$user instanceof UserInterface) {
48
                try {
49
                    $user = $this->userManager->createUser();
50
                    $user->setName($response->getFirstName());
51
                    $user->setSurname($response->getLastName());
52
                    $user->setEmail($email);
53
                    $user->setPlainPassword(md5(uniqid()));
54
                    $user->setEnabled(true);
55
                    $errors = $this->container->get('validator')->validate($user);
56
                    if ($errors->count() > 0) {
57
                        throw new \Exception('need_data');
58
                    }
59
                    $this->userManager->updateUser($user);
60
                } catch (\Exception $e) {
61
                    $needUserData = new NeedUserDataException('needUserData');
62
                    $responseArr = [];
63
                    $responseArr['first_name'] = $response->getFirstName();
64
                    $responseArr['last_name'] = $response->getLastName();
65
                    $responseArr['email'] = $email;
66
67
                    $responseArr = array_merge(
68
                        $responseArr,
69
                        ['socialID' => $socialID],
70
                        ['service' => $response->getResourceOwner()->getName()]
71
                    );
72
                    $needUserData->setResponse($responseArr);
73
                    $this->container->get('session')->getFlashBag()->set('fos_user_success', 'registration.flash.user_need_data');
74
                    throw $needUserData;
75
                }
76
77
                $this->container->get('stfalcon_event.mailer_helper')->sendEasyEmail(
78
                    $this->container->get('translator')->trans('registration.email.subject'),
79
                    '@FOSUser/Registration/email.on_registration.html.twig',
80
                    ['user' => $user],
81
                    $user
82
                );
83
84
                $this->container->get('session')->getFlashBag()->set('fos_user_success', 'registration.flash.user_created');
85
            }
86
            $service = $response->getResourceOwner()->getName();
87
            $socialID = $response->getUsername();
88
            switch ($service) {
89
                case 'google':
90
                    $user->setGoogleID($socialID);
91
                    break;
92
                case 'facebook':
93
                    $user->setFacebookID($socialID);
94
                    break;
95
            }
96
97
            $this->userManager->updateUser($user);
98
        } else {
99
            $checker = new UserChecker();
100
            $checker->checkPreAuth($user);
101
        }
102
103
        return $user;
104
    }
105
}
106