EntityRepository::findOne()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Created by IntelliJ IDEA.
4
 * User: gerk
5
 * Date: 06.01.17
6
 * Time: 11:50
7
 */
8
9
namespace PeekAndPoke\Component\Slumber\Data;
10
11
use PeekAndPoke\Component\Slumber\Core\Exception\SlumberRuntimeException;
12
use PeekAndPoke\Component\Slumber\Data\Error\DuplicateError;
13
use PeekAndPoke\Component\Slumber\Data\MongoDb\MongoDbUtil;
14
use PeekAndPoke\Component\Slumber\Data\Result\InsertOneResult;
15
use PeekAndPoke\Component\Slumber\Data\Result\SaveOneResult;
16
17
18
/**
19
 * @author Karsten J. Gerber <[email protected]>
20
 */
21
class EntityRepository implements Repository
22
{
23
    /** @var string */
24
    private $name;
25
    /** @var StorageDriver */
26
    private $driver;
27
28
    public function __construct($name, StorageDriver $driver)
29
    {
30
        $this->name   = $name;
31
        $this->driver = $driver;
32
    }
33
34
    /**
35
     * @return string
36
     */
37 4
    public function getName()
38
    {
39 4
        return $this->name;
40
    }
41
42
    /**
43
     * @return \ReflectionClass
44
     */
45
    public function getEntityClass()
46
    {
47
        return $this->driver->getEntityBaseClass();
48
    }
49
50
    /**
51
     * TODO: remove me! It was only used to find a repo by sub-classes ... we no longer need this
52
     * @return string[]
53
     */
54
    public function getEntityClassAliases()
55
    {
56
        return [];
57
    }
58
59
    /**
60
     * @return mixed
61
     */
62
    public function buildIndexes()
63
    {
64
        return $this->driver->buildIndexes();
65
    }
66
67
    /**
68
     * Insert a database entry
69
     *
70
     * @param mixed $subject
71
     *
72
     * @return InsertOneResult
73
     *
74
     * @throws SlumberRuntimeException When the given subject cannot be stored in this repository
75
     * @throws DuplicateError          When the underlying database permits the insert, due to a duplicate key exception
76
     */
77 3 View Code Duplication
    public function insert($subject)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
    {
79 3
        if ($subject === null || ! \is_object($subject)) {
80
            return null;
81
        }
82
83 3
        $this->checkObjectCompatibility($subject);
84
85 3
        return $this->driver->insert($subject);
86
    }
87
88
    /**
89
     * @param mixed $subject
90
     *
91
     * @return SaveOneResult
92
     *
93
     * @throws SlumberRuntimeException When the given subject cannot be stored in this repository
94
     * @throws DuplicateError          When the underlying database permits the insert, due to a duplicate key exception
95
     */
96 25 View Code Duplication
    public function save($subject)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98 25
        if ($subject === null || ! \is_object($subject)) {
99 17
            return null;
100
        }
101
102 25
        $this->checkObjectCompatibility($subject);
103
104 25
        return $this->driver->save($subject);
105
    }
106
107
    /**
108
     * @param $query
109
     *
110
     * @return Cursor
111
     */
112 11
    public function find(array $query = null)
113
    {
114 11
        return $this->driver->find($query);
115
    }
116
117
    /**
118
     * @param array|null $query
119
     *
120
     * @return mixed|null
121
     */
122 19
    public function findOne(array $query = null)
123
    {
124 19
        return $this->driver->findOne($query);
125
    }
126
127
    /**
128
     * @param mixed $id
129
     *
130
     * @return mixed|null
131
     */
132 19
    public function findById($id)
133
    {
134 19
        return $this->findOne([
135 19
            '_id' => MongoDbUtil::ensureMongoId($id),
136
        ]);
137
    }
138
139
    /**
140
     * Find one object by its public reference
141
     *
142
     * @param string $reference
143
     *
144
     * @return mixed|null
145
     */
146 1
    public function findByReference($reference)
147
    {
148 1
        return $this->findOne([
149 1
            'reference' => $reference,
150
        ]);
151
    }
152
153
    /**
154
     * @param $entity
155
     *
156
     * @return Result\RemoveResult
157
     */
158 3
    public function remove($entity)
159
    {
160 3
        return $this->driver->remove($entity);
161
    }
162
163
    /**
164
     * Remove all from this collection
165
     *
166
     * @param array|null $query
167
     *
168
     * @return Result\RemoveResult
169
     */
170 25
    public function removeAll(array $query = null)
171
    {
172 25
        return $this->driver->removeAll($query);
173
    }
174
175
    /**
176
     * @param $subject
177
     *
178
     * @throw SlumberRuntimeException
179
     */
180 28
    private function checkObjectCompatibility($subject)
181
    {
182 28
        $entityClassName = $this->driver->getEntityBaseClass()->name;
183
184 28
        $isOk = \is_object($subject) && $subject instanceof $entityClassName;
185
186 28
        if (! $isOk) {
187
            throw new SlumberRuntimeException(
188
                'Repository ' . \get_class($this) . ' can only store objects of type ' . $entityClassName .
189
                ' but a ' . \get_class($subject) . ' was given!'
190
            );
191
        }
192 28
    }
193
}
194