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

Authenticator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 6
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Services\Skautis;
4
5
use Nette\SmartObject;
6
use Nette\Security\IAuthenticator;
7
use Nette\Security\Identity;
8
use App\Repositories\SocialLoginRepository;
9
use App\Repositories\PersonRepository;
10
use App\Repositories\UserRepository;
11
use App\Services\UserService;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, App\Services\Skautis\UserService.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
use App\Services\SkautIS\UserService as SkautisUserService;
13
use App\Services\SkautIS\AuthService as SkautisAuthService;
14
15
class Authenticator implements IAuthenticator
16
{
17
	use SmartObject;
18
19
	/**
20
	 * @var SocialLoginRepository
21
	 */
22
	private $socialLoginRepository;
23
24
	/**
25
	 * @var PersonRepository
26
	 */
27
	private $personRepository;
28
29
	/**
30
	 * @var UserRepository
31
	 */
32
	private $userRepository;
33
34
    /**
35
     * @var AuthService
36
     */
37
	private $skautisAuthService;
38
39
    /**
40
     * @var \App\Services\SkautIS\UserService
41
     */
42
	private $skautisUserService;
43
44
    /**
45
     * @var UserService
46
     */
47
	private $userService;
48
49
	public function __construct(
50
		SocialLoginRepository $socialLoginRepository,
51
		PersonRepository $personRepository,
52
		UserRepository $userRepository,
53
		SkautisAuthService $skautisAuthService,
54
		SkautisUserService $skautisUserService,
55
		UserService $userService
56
	) {
57
		$this->socialLoginRepository = $socialLoginRepository;
58
		$this->personRepository = $personRepository;
59
		$this->userRepository = $userRepository;
60
		$this->skautisAuthService = $skautisAuthService;
61
		$this->skautisUserService = $skautisUserService;
62
		$this->userService = $userService;
63
	}
64
65
	public function authenticate(array $credentials): Identity
66
	{
67
		$credentials = $credentials[0];
68
69
		$this->skautisAuthService->setInit($credentials);
70
		$this->guardSkautisLoggedIn();
71
		$userDetail = $this->skautisUserService->getPersonalDetail();
72
		$token = $userDetail->ID;
73
74
        $user = $this->userService->findByProviderAndToken('skautis', $token);
75
		if(!$user) {
76
			$userDetail = $this->skautisUserService->getPersonalDetail();
77
			$user = $this->userService->createAccount($token, $userDetail);
78
		}
79
80
		return new Identity($user->id, $user->role,  ['username' => $user->nick]);
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...
81
	}
82
83
	/**
84
	 * @throws AuthenticationException
85
	 */
86
	protected function guardSkautisLoggedIn()
87
	{
88
		if (!$this->skautisUserService->isLoggedIn()) {
89
			throw new AuthenticationException('Nemáte platné přihlášení do skautISu!');
90
		}
91
	}
92
93
}
94