Completed
Push — master ( f2b8b4...a21374 )
by Tom
24s queued 11s
created

ObjectRepository   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 84%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 75
ccs 21
cts 25
cp 0.84
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setOptions() 0 10 2
A __construct() 0 4 1
A isEmpty() 0 4 1
A read() 0 9 2
A readKeyOnly() 0 4 1
A write() 0 7 1
A clear() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineModule\Authentication\Storage;
6
7
use DoctrineModule\Options\Authentication as AuthenticationOptions;
8
use Laminas\Authentication\Storage\StorageInterface;
9
10
/**
11
 * This class implements StorageInterface and allow to save the result of an authentication against an object repository
12
 *
13
 * @link    http://www.doctrine-project.org/
14
 */
15
class ObjectRepository implements StorageInterface
16
{
17
    /** @var AuthenticationOptions */
18
    protected $options;
19
20
    /**
21
     * @param mixed[]|AuthenticationOptions $options
22
     */
23 4
    public function setOptions($options) : ObjectRepository
24
    {
25 4
        if (! $options instanceof AuthenticationOptions) {
26 1
            $options = new AuthenticationOptions($options);
27
        }
28
29 4
        $this->options = $options;
30
31 4
        return $this;
32
    }
33
34
    /**
35
     * Constructor
36
     *
37
     * @param mixed[]|AuthenticationOptions $options
38
     */
39 4
    public function __construct($options = [])
40
    {
41 4
        $this->setOptions($options);
42 4
    }
43
44 1
    public function isEmpty() : bool
45
    {
46 1
        return $this->options->getStorage()->isEmpty();
47
    }
48
49
    /**
50
     * This function assumes that the storage only contains identifier values (which is the case if
51
     * the ObjectRepository authentication adapter is used).
52
     */
53 1
    public function read() : ?object
54
    {
55 1
        $identity = $this->options->getStorage()->read();
56 1
        if ($identity) {
57 1
            return $this->options->getObjectRepository()->find($identity);
58
        }
59
60
        return null;
61
    }
62
63
    /**
64
     * Will return the key of the identity. If only the key is needed, this avoids an
65
     * unnecessary db call
66
     *
67
     * @return mixed
68
     */
69 1
    public function readKeyOnly()
70
    {
71 1
        return $identity = $this->options->getStorage()->read();
0 ignored issues
show
Unused Code introduced by
$identity is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
72
    }
73
74
    /**
75
     * @param mixed $identity
76
     */
77 1
    public function write($identity) : void
78
    {
79 1
        $metadataInfo     = $this->options->getClassMetadata();
80 1
        $identifierValues = $metadataInfo->getIdentifierValues($identity);
81
82 1
        $this->options->getStorage()->write($identifierValues);
83 1
    }
84
85
    public function clear() : void
86
    {
87
        $this->options->getStorage()->clear();
88
    }
89
}
90