Completed
Pull Request — 1.x (#641)
by
unknown
15:00
created

Db   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 8
Bugs 2 Features 0
Metric Value
wmc 18
lcom 1
cbo 4
dl 0
loc 169
rs 10
c 8
b 2
f 0

10 Methods

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