Completed
Pull Request — 1.x (#634)
by
unknown
15:05
created

Db::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace ZfcUser\Authentication\Storage;
4
5
use Zend\Authentication\Storage;
6
use Zend\Authentication\Storage\StorageInterface;
7
use Zend\ServiceManager\ServiceManagerAwareInterface;
8
use Zend\ServiceManager\ServiceManager;
9
use ZfcUser\Mapper\UserInterface as UserMapper;
10
11
class Db implements Storage\StorageInterface
12
{
13
    /**
14
     * @var StorageInterface
15
     */
16
    protected $storage;
17
18
    /**
19
     * @var UserMapper
20
     */
21
    protected $mapper;
22
23
    /**
24
     * @var mixed
25
     */
26
    protected $resolvedIdentity;
27
28
    /**
29
     * @var ServiceManager
30
     */
31
    protected $serviceManager;
32
33
    public function __construct(ServiceManager $serviceManager)
34
    {
35
        $this->serviceManager = $serviceManager;
36
    }
37
38
    /**
39
     * Returns true if and only if storage is empty
40
     *
41
     * @throws \Zend\Authentication\Exception\InvalidArgumentException If it is impossible to determine whether
42
     * storage is empty or not
43
     * @return boolean
44
     */
45
    public function isEmpty()
46
    {
47
        if ($this->getStorage()->isEmpty()) {
48
            return true;
49
        }
50
        $identity = $this->getStorage()->read();
51
        if ($identity === null) {
52
            $this->clear();
53
            return true;
54
        }
55
56
        return false;
57
    }
58
59
    /**
60
     * Returns the contents of storage
61
     *
62
     * Behavior is undefined when storage is empty.
63
     *
64
     * @throws \Zend\Authentication\Exception\InvalidArgumentException If reading contents from storage is impossible
65
     * @return mixed
66
     */
67
    public function read()
68
    {
69
        if (null !== $this->resolvedIdentity) {
70
            return $this->resolvedIdentity;
71
        }
72
73
        $identity = $this->getStorage()->read();
74
75
        if (is_int($identity) || is_scalar($identity)) {
76
            $identity = $this->getMapper()->findById($identity);
77
        }
78
79
        if ($identity) {
80
            $this->resolvedIdentity = $identity;
81
        } else {
82
            $this->resolvedIdentity = null;
83
        }
84
85
        return $this->resolvedIdentity;
86
    }
87
88
    /**
89
     * Writes $contents to storage
90
     *
91
     * @param  mixed $contents
92
     * @throws \Zend\Authentication\Exception\InvalidArgumentException If writing $contents to storage is impossible
93
     * @return void
94
     */
95
    public function write($contents)
96
    {
97
        $this->resolvedIdentity = null;
98
        $this->getStorage()->write($contents);
99
    }
100
101
    /**
102
     * Clears contents from storage
103
     *
104
     * @throws \Zend\Authentication\Exception\InvalidArgumentException If clearing contents from storage is impossible
105
     * @return void
106
     */
107
    public function clear()
108
    {
109
        $this->resolvedIdentity = null;
110
        $this->getStorage()->clear();
111
    }
112
113
    /**
114
     * getStorage
115
     *
116
     * @return Storage\StorageInterface
117
     */
118
    public function getStorage()
119
    {
120
        if (null === $this->storage) {
121
            $this->setStorage(new Storage\Session);
122
        }
123
        return $this->storage;
124
    }
125
126
    /**
127
     * setStorage
128
     *
129
     * @param Storage\StorageInterface $storage
130
     * @access public
131
     * @return Db
132
     */
133
    public function setStorage(Storage\StorageInterface $storage)
134
    {
135
        $this->storage = $storage;
136
        return $this;
137
    }
138
139
    /**
140
     * getMapper
141
     *
142
     * @return UserMapper
143
     */
144
    public function getMapper()
145
    {
146
        if (null === $this->mapper) {
147
            $this->mapper = $this->getServiceManager()->get('zfcuser_user_mapper');
148
        }
149
        return $this->mapper;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->mapper; of type object|array adds the type array to the return on line 149 which is incompatible with the return type documented by ZfcUser\Authentication\Storage\Db::getMapper of type ZfcUser\Mapper\UserInterface.
Loading history...
150
    }
151
152
    /**
153
     * setMapper
154
     *
155
     * @param UserMapper $mapper
156
     * @return Db
157
     */
158
    public function setMapper(UserMapper $mapper)
159
    {
160
        $this->mapper = $mapper;
161
        return $this;
162
    }
163
164
    /**
165
     * Retrieve service manager instance
166
     *
167
     * @return ServiceManager
168
     */
169
    public function getServiceManager()
170
    {
171
        return $this->serviceManager;
172
    }
173
174
    /**
175
     * Set service manager instance
176
     *
177
     * @param ServiceManager $locator
178
     * @return void
179
     */
180
    public function setServiceManager(ServiceManager $serviceManager)
181
    {
182
        $this->serviceManager = $serviceManager;
183
    }
184
}
185