Db::getMapper()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace LmcUser\Authentication\Storage;
4
5
use Interop\Container\ContainerInterface;
6
use Laminas\Authentication\Storage;
7
use Laminas\Authentication\Storage\StorageInterface;
8
use Laminas\ServiceManager\ServiceManager;
9
use LmcUser\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
    /**
34
     * Returns true if and only if storage is empty
35
     *
36
     * @throws \Laminas\Authentication\Exception\InvalidArgumentException If it is impossible to determine whether
37
     * storage is empty or not
38
     * @return boolean
39
     */
40
    public function isEmpty()
41
    {
42
        if ($this->getStorage()->isEmpty()) {
43
            return true;
44
        }
45
        $identity = $this->getStorage()->read();
46
        if ($identity === null) {
47
            $this->clear();
48
            return true;
49
        }
50
51
        return false;
52
    }
53
54
    /**
55
     * Returns the contents of storage
56
     *
57
     * Behavior is undefined when storage is empty.
58
     *
59
     * @throws \Laminas\Authentication\Exception\InvalidArgumentException If reading contents from storage is impossible
60
     * @return mixed
61
     */
62
    public function read()
63
    {
64
        if (null !== $this->resolvedIdentity) {
65
            return $this->resolvedIdentity;
66
        }
67
68
        $identity = $this->getStorage()->read();
69
70
        if (is_int($identity) || is_scalar($identity)) {
71
            $identity = $this->getMapper()->findById($identity);
72
        }
73
74
        if ($identity) {
75
            $this->resolvedIdentity = $identity;
76
        } else {
77
            $this->resolvedIdentity = null;
78
        }
79
80
        return $this->resolvedIdentity;
81
    }
82
83
    /**
84
     * Writes $contents to storage
85
     *
86
     * @param  mixed $contents
87
     * @throws \Laminas\Authentication\Exception\InvalidArgumentException If writing $contents to storage is impossible
88
     * @return void
89
     */
90
    public function write($contents)
91
    {
92
        $this->resolvedIdentity = null;
93
        $this->getStorage()->write($contents);
94
    }
95
96
    /**
97
     * Clears contents from storage
98
     *
99
     * @throws \Laminas\Authentication\Exception\InvalidArgumentException If clearing contents from storage is impossible
100
     * @return void
101
     */
102
    public function clear()
103
    {
104
        $this->resolvedIdentity = null;
105
        $this->getStorage()->clear();
106
    }
107
108
    /**
109
     * getStorage
110
     *
111
     * @return Storage\StorageInterface
112
     */
113
    public function getStorage()
114
    {
115
        if (null === $this->storage) {
116
            $this->setStorage(new Storage\Session);
117
        }
118
        return $this->storage;
119
    }
120
121
    /**
122
     * setStorage
123
     *
124
     * @param  Storage\StorageInterface $storage
125
     * @access public
126
     * @return Db
127
     */
128
    public function setStorage(Storage\StorageInterface $storage)
129
    {
130
        $this->storage = $storage;
131
        return $this;
132
    }
133
134
    /**
135
     * getMapper
136
     *
137
     * @return UserMapper
138
     */
139
    public function getMapper()
140
    {
141
        if (null === $this->mapper) {
142
            $this->mapper = $this->getServiceManager()->get('lmcuser_user_mapper');
143
        }
144
        return $this->mapper;
145
    }
146
147
    /**
148
     * setMapper
149
     *
150
     * @param  UserMapper $mapper
151
     * @return Db
152
     */
153
    public function setMapper(UserMapper $mapper)
154
    {
155
        $this->mapper = $mapper;
156
        return $this;
157
    }
158
159
    /**
160
     * Retrieve service manager instance
161
     *
162
     * @return ServiceManager
163
     */
164
    public function getServiceManager()
165
    {
166
        return $this->serviceManager;
167
    }
168
169
    /**
170
     * Set service manager instance
171
     *
172
     * @param  ContainerInterface $locator
0 ignored issues
show
Bug introduced by
There is no parameter named $locator. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
173
     * @return void
174
     */
175
    public function setServiceManager(ContainerInterface $serviceManager)
176
    {
177
        $this->serviceManager = $serviceManager;
0 ignored issues
show
Documentation Bug introduced by
$serviceManager is of type object<Interop\Container\ContainerInterface>, but the property $serviceManager was declared to be of type object<Laminas\ServiceManager\ServiceManager>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
178
    }
179
}
180