1 | <?php declare(strict_types=1); |
||||
2 | |||||
3 | namespace Bone\SocialAuth\Service; |
||||
4 | |||||
5 | use Bone\Server\SessionAwareInterface; |
||||
6 | use Bone\Server\Traits\HasSessionTrait; |
||||
7 | use Bone\SocialAuth\Provider\CustomOauth2Provider; |
||||
0 ignored issues
–
show
|
|||||
8 | use DateTime; |
||||
9 | use Del\Entity\User; |
||||
10 | use Del\Factory\CountryFactory; |
||||
11 | use Del\Person\Entity\Person; |
||||
12 | use Del\Service\UserService; |
||||
13 | use Del\Value\User\State; |
||||
14 | use Exception; |
||||
15 | use Hybridauth\Hybridauth; |
||||
16 | use Hybridauth\Adapter\AdapterInterface; |
||||
17 | use Hybridauth\User\Profile; |
||||
18 | |||||
19 | class SocialAuthService implements SessionAwareInterface |
||||
20 | { |
||||
21 | use HasSessionTrait; |
||||
22 | |||||
23 | /** @var array $config */ |
||||
24 | private $config; |
||||
25 | |||||
26 | /** @var array $customProviderConfigs */ |
||||
27 | private $customProviderConfigs = []; |
||||
28 | |||||
29 | /** @var UserService $userService */ |
||||
30 | private $userService; |
||||
31 | |||||
32 | /** @var string $uploadsDir */ |
||||
33 | private $uploadsDir; |
||||
34 | |||||
35 | /** @var string $imgDir */ |
||||
36 | private $imgDir; |
||||
37 | |||||
38 | /** @var SocialAuthAdapterFactory $factory */ |
||||
39 | private $factory; |
||||
40 | |||||
41 | /** |
||||
42 | * SocialAuthService constructor. |
||||
43 | * @param array $config |
||||
44 | */ |
||||
45 | 5 | public function __construct(array $config, UserService $userService, string $uploadsDir, string $imgDir, SocialAuthAdapterFactory $factory) |
|||
46 | { |
||||
47 | 5 | $this->config = $config; |
|||
48 | 5 | $this->userService = $userService; |
|||
49 | 5 | $this->uploadsDir = $uploadsDir; |
|||
50 | 5 | $this->imgDir = $imgDir; |
|||
51 | 5 | $this->factory = $factory; |
|||
52 | |||||
53 | 5 | if (array_key_exists('custom', $this->config)) { |
|||
54 | $this->customProviderConfigs = $this->config['custom']['providers']; |
||||
55 | unset ($this->config['custom']); |
||||
56 | } |
||||
57 | } |
||||
58 | |||||
59 | /** |
||||
60 | * @param string $provider |
||||
61 | * @return \Hybridauth\Adapter\AdapterInterface |
||||
62 | * @throws \Hybridauth\Exception\InvalidArgumentException |
||||
63 | * @throws \Hybridauth\Exception\UnexpectedValueException |
||||
64 | */ |
||||
65 | 2 | public function getAuthAdapter(string $provider): AdapterInterface |
|||
66 | { |
||||
67 | 2 | if (array_key_exists($provider, $this->customProviderConfigs)) { |
|||
68 | $this->config['providers'][$provider] = $this->customProviderConfigs[$provider]; |
||||
69 | } |
||||
70 | |||||
71 | 2 | if (array_key_exists($provider, $this->config['providers'])) { |
|||
72 | 1 | return $this->getAdapter($this->config, $provider); |
|||
73 | } |
||||
74 | |||||
75 | 1 | throw new Exception('SocialAuth Adapter not found', 404); |
|||
76 | } |
||||
77 | |||||
78 | /** |
||||
79 | * @param array $config |
||||
80 | * @param string $provider |
||||
81 | * @return AdapterInterface |
||||
82 | */ |
||||
83 | 1 | private function getAdapter(array $config, string $provider): AdapterInterface |
|||
84 | { |
||||
85 | 1 | $config['callback'] .= '/' . strtolower($provider); |
|||
86 | 1 | $hybridauth = $this->factory->factory($config); |
|||
87 | 1 | $adapter = $hybridauth->authenticate($provider); |
|||
88 | |||||
89 | 1 | return $adapter; |
|||
90 | } |
||||
91 | |||||
92 | /** |
||||
93 | * @param Profile $profile |
||||
94 | * @return User |
||||
95 | */ |
||||
96 | 2 | public function logInUser(Profile $profile): User |
|||
97 | { |
||||
98 | 2 | $email = $profile->email; |
|||
99 | |||||
100 | 2 | if($user = $this->userService->findUserByEmail($email)) { |
|||
0 ignored issues
–
show
It seems like
$email can also be of type null ; however, parameter $email of Del\Service\UserService::findUserByEmail() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
101 | 1 | $user->setLastLogin(new DateTime()); |
|||
102 | 1 | $this->userService->saveUser($user); |
|||
103 | } |
||||
104 | |||||
105 | 2 | if (!$user) { |
|||
106 | 1 | $user = $this->createUser($profile); |
|||
107 | } |
||||
108 | |||||
109 | 2 | $this->session->set('user', $user->getId()); |
|||
110 | |||||
111 | 2 | return $user; |
|||
0 ignored issues
–
show
|
|||||
112 | } |
||||
113 | |||||
114 | /** |
||||
115 | * @param Profile $profile |
||||
116 | * @return User |
||||
117 | * @throws \Doctrine\ORM\OptimisticLockException |
||||
118 | */ |
||||
119 | 1 | private function createUser(Profile $profile): User |
|||
120 | { |
||||
121 | 1 | $now = new DateTime(); |
|||
122 | 1 | $user = new User(); |
|||
123 | 1 | $person = new Person(); |
|||
124 | |||||
125 | 1 | $user->setEmail($profile->email); |
|||
0 ignored issues
–
show
It seems like
$profile->email can also be of type null ; however, parameter $email of Del\Entity\BaseUser::setEmail() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
126 | 1 | $user->setLastLogin($now); |
|||
127 | 1 | $user->setRegistrationDate($now); |
|||
128 | 1 | $user->setState(new State(State::STATE_ACTIVATED)); |
|||
129 | 1 | $user->setPerson($person); |
|||
130 | |||||
131 | 1 | if ($profile->photoURL) { |
|||
132 | 1 | $contents = file_get_contents($profile->photoURL); |
|||
133 | 1 | $file = $this->imgDir . md5(microtime()) . '.jpg'; |
|||
134 | 1 | file_put_contents($this->uploadsDir . $file, $contents); |
|||
135 | 1 | $person->setImage($file); |
|||
136 | } |
||||
137 | |||||
138 | 1 | $person->setFirstname($profile->firstName); |
|||
139 | 1 | $person->setLastname($profile->lastName); |
|||
140 | |||||
141 | 1 | $this->userService->getPersonSvc()->savePerson($person); |
|||
142 | 1 | $this->userService->changePassword($user, microtime()); // this saves user too |
|||
143 | |||||
144 | 1 | return $user; |
|||
145 | } |
||||
146 | } |
||||
147 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths