Passed
Push — master ( b97c16...98e719 )
by Dawid
04:21
created

Repository::remove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php declare(strict_types=1);
2
3
namespace Igni\Storage\Driver\MongoDB;
4
5
use Igni\Storage\Exception\RepositoryException;
6
use Igni\Storage\Repository as RepositoryInterface;
7
use Igni\Storage\Manager;
8
use Igni\Storage\Entity;
9
10
abstract class Repository implements RepositoryInterface
11
{
12
    protected $connection;
13
    protected $entityManager;
14
    protected $hydrator;
15
    protected $metaData;
16
17 14
    final public function __construct(Connection $connection, Manager $entityManager)
18
    {
19 14
        $this->connection = $connection;
20 14
        $this->entityManager = $entityManager;
21 14
        $this->metaData = $entityManager->getMetaData($this->getEntityClass());
22 14
        $this->hydrator = $entityManager->getHydrator($this->getEntityClass());
23 14
    }
24
25 2
    public function get($id): Entity
26
    {
27 2
        $cursor = $this->connection->find(
28 2
            $this->metaData->getSource(),
29 2
            ['_id' => $id],
30 2
            ['limit' => 1]
31
        );
32 2
        $cursor->setHydrator($this->hydrator);
33
34 2
        $entity = $cursor->current();
35 2
        $cursor->close();
36
37 2
        if (!$entity instanceof Entity) {
38
            throw RepositoryException::forNotFound($id);
39
        }
40
41 2
        return $entity;
42
    }
43
44
    public function create(Entity $entity): Entity
45
    {
46
        // Support id auto-generation.
47
        $entity->getId();
48
        $data = $this->hydrator->extract($entity);
49
        if (isset($data['id'])) {
50
            $data['_id'] = $data['id'];
51
            unset($data['id']);
52
        }
53
        $this->connection->insert(
54
            $this->metaData->getSource(),
55
            $data
56
        );
57
58
        return $entity;
59
    }
60
61 1
    public function remove(Entity $entity): Entity
62
    {
63 1
        $this->connection->remove(
64 1
            $this->metaData->getSource(),
65 1
            $entity->getId()->getValue()
66
        );
67
68 1
        return $entity;
69
    }
70
71
    public function update(Entity $entity): Entity
72
    {
73
        $this->connection->update(
74
            $this->metaData->getSource(),
75
            $this->hydrator->extract($entity)
76
        );
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Igni\Storage\Entity. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
77
    }
78
79
    abstract public function getEntityClass(): string;
80
}
81