Passed
Push — master ( 0d72e8...44be60 )
by Gabor
04:58
created

AbstractServiceAdapter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 2
dl 0
loc 113
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A getAuthStorage() 0 4 1
A getDataStorage() 0 4 1
A getNewAuthResultInstance() 0 4 1
authenticate() 0 1 ?
A setIdentity() 0 6 1
A hasIdentity() 0 4 1
A getIdentity() 0 4 1
A clearIdentity() 0 5 1
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\Auth\ServiceAdapter;
15
16
use WebHemi\Auth;
17
use WebHemi\Configuration\ServiceInterface as ConfigurationInterface;
18
use WebHemi\Data\Entity\User\UserEntity;
19
use WebHemi\Data\Storage\User\UserStorage;
20
21
/**
22
 * Class AbstractServiceAdapter
23
 */
24
abstract class AbstractServiceAdapter implements Auth\ServiceInterface
25
{
26
    /** @var Auth\ResultInterface */
27
    private $authResult;
28
    /** @var Auth\StorageInterface */
29
    private $authStorage;
30
    /** @var UserStorage */
31
    private $dataStorage;
32
    /** @var ConfigurationInterface */
33
    protected $configuration;
34
35
    /**
36
     * AbstractServiceAdapter constructor.
37
     *
38
     * @param ConfigurationInterface $configuration
39
     * @param Auth\ResultInterface   $authResultPrototype
40
     * @param Auth\StorageInterface  $authStorage
41
     * @param UserStorage            $dataStorage
42
     */
43 3
    public function __construct(
44
        ConfigurationInterface $configuration,
45
        Auth\ResultInterface $authResultPrototype,
46
        Auth\StorageInterface $authStorage,
47
        UserStorage $dataStorage
48
    ) {
49 3
        $this->configuration = $configuration->getConfig('auth');
50 3
        $this->authResult = $authResultPrototype;
51 3
        $this->authStorage = $authStorage;
52 3
        $this->dataStorage = $dataStorage;
53 3
    }
54
55
    /**
56
     * Gets the auth storage instance. (e.g.: AuthSessionStorage)
57
     *
58
     * @return Auth\StorageInterface
59
     */
60 1
    protected function getAuthStorage() : Auth\StorageInterface
61
    {
62 1
        return $this->authStorage;
63
    }
64
65
    /**
66
     * Gets the data storage instance. (e.g.: UserStorage)
67
     *
68
     * @return UserStorage
69
     */
70 2
    protected function getDataStorage() : UserStorage
71
    {
72 2
        return $this->dataStorage;
73
    }
74
75
    /**
76
     * Gets a new instance of the auth result container.
77
     *
78
     * @return Auth\ResultInterface
79
     */
80 2
    protected function getNewAuthResultInstance() : Auth\ResultInterface
81
    {
82 2
        return clone $this->authResult;
83
    }
84
85
    /**
86
     * Authenticates the user.
87
     *
88
     * @param Auth\CredentialInterface $credential
89
     * @return Auth\ResultInterface
90
     */
91
    abstract public function authenticate(Auth\CredentialInterface $credential) : Auth\ResultInterface;
92
93
    /**
94
     * Sets the authenticated user.
95
     *
96
     * @param UserEntity $dataEntity
97
     * @return Auth\ServiceInterface
98
     */
99 2
    public function setIdentity(UserEntity $dataEntity) : Auth\ServiceInterface
100
    {
101 2
        $this->authStorage->setIdentity($dataEntity);
102
103 2
        return $this;
104
    }
105
106
    /**
107
     * Checks whether the user is authenticated or not.
108
     *
109
     * @return bool
110
     */
111 2
    public function hasIdentity() : bool
112
    {
113 2
        return $this->authStorage->hasIdentity();
114
    }
115
116
    /**
117
     * Gets the authenticated user's entity.
118
     *
119
     * @return UserEntity|null
120
     */
121 2
    public function getIdentity() : ? UserEntity
122
    {
123 2
        return $this->authStorage->getIdentity();
124
    }
125
126
    /**
127
     * Clears the session.
128
     *
129
     * @return Auth\ServiceInterface
130
     */
131 1
    public function clearIdentity() : Auth\ServiceInterface
132
    {
133 1
        $this->authStorage->clearIdentity();
134 1
        return $this;
135
    }
136
}
137