Passed
Push — master ( 4b954d...b01812 )
by Gabor
03:38
created

AbstractAuthAdapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 4
crap 1
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 5.6
6
 *
7
 * @copyright 2012 - 2016 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
13
namespace WebHemi\Adapter\Auth;
14
15
use WebHemi\Auth\Result;
16
use WebHemi\Auth\AuthStorageInterface;
17
use WebHemi\Config\ConfigInterface;
18
use WebHemi\Data\Entity\DataEntityInterface;
19
use WebHemi\Data\Storage\DataStorageInterface;
20
21
/**
22
 * Class AbstractAuthAdapter
23
 */
24
abstract class AbstractAuthAdapter implements AuthAdapterInterface
25
{
26
    /** @var Result */
27
    private $authResult;
28
    /** @var AuthStorageInterface */
29
    private $authStorage;
30
    /** @var DataStorageInterface */
31
    private $dataStorage;
32
    /** @var ConfigInterface */
33
    protected $configuration;
34
35
    /**
36
     * AbstractAuthAdapter constructor.
37
     *
38
     * @param ConfigInterface      $configuration
39
     * @param Result               $authResultPrototype
40
     * @param AuthStorageInterface $authStorage
41
     * @param DataStorageInterface $dataStorage
42
     */
43 3
    public function __construct(
44
        ConfigInterface $configuration,
45
        Result $authResultPrototype,
46
        AuthStorageInterface $authStorage,
47
        DataStorageInterface $dataStorage
48
    ) {
49 3
        $this->configuration = $configuration->getData('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 AuthStorageInterface
59
     */
60 1
    protected function getAuthStorage()
61
    {
62 1
        return $this->authStorage;
63
    }
64
65
    /**
66
     * Gets the data storage instance. (e.g.: UserStorage)
67
     *
68
     * @return DataStorageInterface
69
     */
70 2
    protected function getDataStorage()
71
    {
72 2
        return $this->dataStorage;
73
    }
74
75
    /**
76
     * Gets a new instance of the auth result container.
77
     *
78
     * @return Result
79
     */
80 2
    protected function getAuthResult()
81
    {
82 2
        return clone $this->authResult;
83
    }
84
85
    /**
86
     * Authenticates the user.
87
     *
88
     * @return Result
89
     */
90
    abstract public function authenticate();
91
92
    /**
93
     * Checks whether the user is authenticated or not.
94
     *
95
     * @return bool
96
     */
97 2
    public function hasIdentity()
98
    {
99 2
        return $this->authStorage->getIdentity() instanceof DataEntityInterface;
100
    }
101
102
    /**
103
     * Sets the authenticated user.
104
     *
105
     * @param DataEntityInterface $dataEntity
106
     * @return AbstractAuthAdapter
107
     */
108 1
    public function setIdentity(DataEntityInterface $dataEntity)
109
    {
110 1
        $this->authStorage->setIdentity($dataEntity);
111
112 1
        return $this;
113
    }
114
115
    /**
116
     * Gets the authenticated user's entity.
117
     *
118
     * @return null|string|DataEntityInterface
119
     */
120 2
    public function getIdentity()
121
    {
122 2
        return $this->authStorage->getIdentity();
123
    }
124
125
    /**
126
     * Clears the session.
127
     *
128
     * @return AbstractAuthAdapter
129
     */
130 1
    public function clearIdentity()
131
    {
132 1
        $this->authStorage->clearIdentity();
133 1
        return $this;
134
    }
135
}
136