Completed
Push — master ( 390a97...c4c478 )
by Daniel
9s
created

CollectionsAgent   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 140
Duplicated Lines 13.57 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 7
dl 19
loc 140
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A supports() 0 4 1
A getCapabilities() 19 19 1
A find() 0 14 3
A save() 0 4 1
A delete() 0 4 1
A query() 0 8 1
A queryCount() 0 4 1
A getIdentifier() 0 13 3
A setParent() 0 4 1
A doQuery() 0 20 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Bridge\ObjectAgent\Doctrine\Collections;
6
7
use Doctrine\Common\Collections\Criteria;
8
use Psi\Component\ObjectAgent\AgentInterface;
9
use Psi\Component\ObjectAgent\Capabilities;
10
use Psi\Component\ObjectAgent\Exception\BadMethodCallException;
11
use Psi\Component\ObjectAgent\Exception\ObjectNotFoundException;
12
use Psi\Component\ObjectAgent\Query\Comparison;
13
use Psi\Component\ObjectAgent\Query\Query;
14
15
class CollectionsAgent implements AgentInterface
16
{
17
    private $store;
18
19
    public function __construct(Store $store)
20
    {
21
        $this->store = $store;
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function supports(string $classFqn): bool
28
    {
29
        return $this->store->hasCollection($classFqn);
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 View Code Duplication
    public function getCapabilities(): Capabilities
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...
36
    {
37
        return Capabilities::create([
38
            'can_set_parent' => false,
39
            'can_query_count' => true,
40
            'supported_comparators' => [
41
                Comparison::EQUALS,
42
                Comparison::NOT_EQUALS,
43
                Comparison::LESS_THAN,
44
                Comparison::LESS_THAN_EQUAL,
45
                Comparison::GREATER_THAN,
46
                Comparison::GREATER_THAN_EQUAL,
47
                Comparison::IN,
48
                Comparison::NOT_IN,
49
                Comparison::CONTAINS,
50
                Comparison::NULL,
51
            ],
52
        ]);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function find($identifier, string $classFqn = null)
59
    {
60
        if (null === $classFqn) {
61
            throw BadMethodCallException::classArgumentIsMandatory(__CLASS__);
62
        }
63
64
        $object = $this->store->find($classFqn, $identifier);
65
66
        if (null === $object) {
67
            throw ObjectNotFoundException::forClassAndIdentifier($classFqn, $identifier);
68
        }
69
70
        return $object;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function save($object)
77
    {
78
        // do nothing ...
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function delete($object)
85
    {
86
        $this->store->delete($object);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function query(Query $query): \Traversable
93
    {
94
        return $this->doQuery(
95
            $query,
96
            $query->getFirstResult(),
97
            $query->getMaxResults()
98
        );
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function queryCount(Query $query): int
105
    {
106
        return count($this->doQuery($query, 0));
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function getIdentifier($object)
113
    {
114
        foreach ($this->store->getCollection(get_class($object)) as $identifier => $element) {
115
            if ($element === $object) {
116
                return $identifier;
117
            }
118
        }
119
120
        throw new \RuntimeException(sprintf(
121
            'Could not find identifier for object of class "%s"',
122
            get_class($object)
123
        ));
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function setParent($object, $parent)
130
    {
131
        throw BadMethodCallException::setParentNotSupported(__CLASS__);
132
    }
133
134
    private function doQuery(Query $query, int $firstResult = null, int $maxResults = null)
135
    {
136
        $collection = $this->store->getCollection($query->getClassFqn());
137
138
        $doctrineExpression = null;
139
        if ($query->hasExpression()) {
140
            $expressionBuilder = Criteria::expr();
141
            $visitor = new CollectionsVisitor($expressionBuilder);
142
            $doctrineExpression = $visitor->dispatch($query->getExpression());
143
        }
144
145
        $criteria = new Criteria(
146
            $doctrineExpression,
147
            $query->getOrderings(),
148
            $firstResult,
149
            $maxResults
150
        );
151
152
        return $collection->matching($criteria);
153
    }
154
}
155