Completed
Pull Request — devel (#146)
by Litera
24:35 queued 19:02
created

UserService::createAccount()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 29
nc 1
nop 2
dl 0
loc 41
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace App\Services;
4
5
use App\Repositories\SocialLoginRepository;
6
use App\Repositories\PersonRepository;
7
use App\Repositories\UserRepository;
8
use App\Entities\CredentialsEntity;
9
10
class UserService
11
{
12
13
	/**
14
	 * @var SocialLoginRepository
15
	 */
16
	protected $socialLoginRepository;
17
18
	/**
19
	 * @var PersonRepository
20
	 */
21
	protected $personRepository;
22
23
	/**
24
	 * @var UserRepository
25
	 */
26
	protected $userRepository;
27
28
	function __construct(
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
29
		SocialLoginRepository $socialLoginRepository,
30
		PersonRepository $personRepository,
31
		UserRepository $userRepository
32
	)
33
	{
34
		$this->setSocialLoginRepository($socialLoginRepository);
35
		$this->setPersonRepository($personRepository);
36
		$this->setUserRepository($userRepository);
37
	}
38
39
	/**
40
	 * @param string $provider
41
	 * @param string $token
42
	 * @return CredentialsEntity
43
	 */
44
	public function findByProviderAndToken(string $provider, string $token)//: ?CredentialsEntity
45
	{
46
		$socialLogin = $this->getSocialLoginRepository()->findByProviderAndToken($provider, $token);
47
48
		$credentials = null;
49
		if($socialLogin) {
50
            $credentials = new CredentialsEntity();
51
            $credentials->id = $socialLogin->user->id;
52
            $credentials->guid = $socialLogin->user->guid;
53
            $credentials->login = $socialLogin->user->login;
54
            $credentials->role = $socialLogin->user->is_admin ? 'admin' : 'guest';
0 ignored issues
show
Bug introduced by
The property role does not seem to exist in App\Entities\CredentialsEntity.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
55
            $credentials->name = $socialLogin->user->person->name;
56
            $credentials->surname = $socialLogin->user->person->surname;
57
            $credentials->nick = $socialLogin->user->person->nick;
58
            $credentials->birthday = $socialLogin->user->person->birthday;
59
            $credentials->email = $socialLogin->user->person->email;
60
        }
61
62
		return $credentials;
63
	}
64
65
	public function createAccount(string $token, $userDetail): CredentialsEntity
66
	{
67
		$newPersonalDetail = [
68
			'name'        => $userDetail->FirstName,
69
			'surname'     => $userDetail->LastName,
70
			'nick'        => $userDetail->NickName,
71
			'birthday'    => $userDetail->Birthday,
72
			'email'       => $userDetail->Email,
73
			'street'      => $userDetail->Street,
74
			'city'        => $userDetail->City,
75
			'postal_code' => $userDetail->Postcode,
76
		];
77
78
		$newPerson = $this->getPersonRepository()->create($newPersonalDetail);
79
80
		$newUser = [
81
			'login' => $newPerson->email,
82
			'person' => $newPerson->id,
83
		];
84
85
		$newUser = $this->getUserRepository()->create($newUser);
86
87
		$newSocialLogin = [
88
			'user'     => $newUser->id,
89
			'token'    => $token,
90
			'provider' => 'skautis'
91
		];
92
93
		$this->getSocialLoginRepository()->create($newSocialLogin);
94
95
		$credentials = new CredentialsEntity();
96
		$credentials->id = $newUser->id;
97
		$credentials->guid = $newUser->guid;
98
		$credentials->login = $newUser->login;
99
		$credentials->name = $newPerson->name;
100
		$credentials->surname = $newPerson->surname;
101
		$credentials->nick = $newPerson->nick;
102
		$credentials->role = 'guest';
0 ignored issues
show
Bug introduced by
The property role does not seem to exist in App\Entities\CredentialsEntity.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
103
104
		return $credentials;
105
	}
106
107
	/**
108
	 * @return PersonRepository
109
	 */
110
	protected function getPersonRepository(): PersonRepository
111
	{
112
		return $this->personRepository;
113
	}
114
115
	/**
116
	 * @param  PersonRepository $repository
117
	 * @return self
118
	 */
119
	protected function setPersonRepository(PersonRepository $repository): self
120
	{
121
		$this->personRepository = $repository;
122
123
		return $this;
124
	}
125
126
	/**
127
	 * @return UserRepository
128
	 */
129
	public function getUserRepository(): UserRepository
130
	{
131
		return $this->userRepository;
132
	}
133
134
	/**
135
	 * @param  UserRepository $userRepository
0 ignored issues
show
Documentation introduced by
There is no parameter named $userRepository. Did you maybe mean $repository?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
136
	 * @return self
137
	 */
138
	public function setUserRepository(UserRepository $repository): self
139
	{
140
		$this->userRepository = $repository;
141
142
		return $this;
143
	}
144
145
	/**
146
	 * @param  SocialLoginRepository $repository
147
	 * @return $this
148
	 */
149
	protected function setSocialLoginRepository(SocialLoginRepository $repository): self
150
	{
151
		$this->socialLoginRepository = $repository;
152
153
		return $this;
154
	}
155
156
	/**
157
	 * @return SocialLoginRepository
158
	 */
159
	protected function getSocialLoginRepository(): SocialLoginRepository
160
	{
161
		return $this->socialLoginRepository;
162
	}
163
}
164