Completed
Push — develop ( e95d13...02ce7e )
by
unknown
09:48
created

SnapshotRepository::merge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2017 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Core\Repository;
12
13
use Core\Entity\Collection\ArrayCollection;
14
use Core\Entity\EntityInterface;
15
use Core\Entity\Hydrator\EntityHydrator;
16
use Core\Entity\SnapshotAttributesProviderInterface;
17
use Core\Entity\SnapshotInterface;
18
use Doctrine\Common\Collections\Collection;
19
use Doctrine\ODM\MongoDB\DocumentRepository;
20
use Zend\Hydrator\HydratorInterface;
21
22
/**
23
 * ${CARET}
24
 * 
25
 * @author Mathias Gelhausen <[email protected]>
26
 * @todo write test 
27
 */
28
class SnapshotRepository extends DocumentRepository
29
{
30
    /**
31
     *
32
     *
33
     * @var HydratorInterface
34
     */
35
    protected $hydrator;
36
37
    /**
38
     *
39
     *
40
     * @var HydratorInterface
41
     */
42
    protected $sourceHydrator;
43
44
    /**
45
     *
46
     *
47
     * @var array
48
     */
49
    protected $snapshotAttributes = [];
50
51
    /**
52
     * @param \Zend\Hydrator\HydratorInterface $hydrator
53
     *
54
     * @return self
55
     */
56
    public function setHydrator($hydrator)
57
    {
58
        $this->hydrator = $hydrator;
59
60
        return $this;
61
    }
62
63
    /**
64
     * @return \Zend\Hydrator\HydratorInterface
65
     */
66
    public function getHydrator()
67
    {
68
        if (!$this->hydrator) {
69
            $this->setHydrator(new EntityHydrator());
70
        }
71
        return $this->hydrator;
72
    }
73
74
    /**
75
     * @param \Zend\Hydrator\HydratorInterface $sourceHydrator
76
     *
77
     * @return self
78
     */
79
    public function setSourceHydrator($sourceHydrator)
80
    {
81
        $this->sourceHydrator = $sourceHydrator;
82
83
        return $this;
84
    }
85
86
    /**
87
     * @return \Zend\Hydrator\HydratorInterface
88
     */
89
    public function getSourceHydrator()
90
    {
91
        if (!$this->sourceHydrator) {
92
            return $this->getHydrator();
93
        }
94
95
        return $this->sourceHydrator;
96
    }
97
98
    /**
99
     * @param array $snapshotAttributes
100
     *
101
     * @return self
102
     */
103
    public function setSnapshotAttributes($snapshotAttributes)
104
    {
105
        $this->snapshotAttributes = $snapshotAttributes;
106
107
        return $this;
108
    }
109
110
    /**
111
     * @return array
112
     */
113
    public function getSnapshotAttributes()
114
    {
115
        return $this->snapshotAttributes;
116
    }
117
118
119
120
    public function create(EntityInterface $source, $persist = true)
121
    {
122
123
        $snapshot = $this->getDocumentName();
124
        $snapshot = new $snapshot($source);
125
126
        $this->copy($source, $snapshot);
127
128
        if ($persist) {
129
            $this->store($snapshot);
130
        }
131
132
        return $snapshot;
133
    }
134
135
    protected function copy($source, $target, $attributes = null)
136
    {
137
        if (!$attributes) {
138
            if ($source instanceOf SnapshotAttributesProviderInterface) {
139
                $attributes = $source->getSnapshotAttributes();
140
            } else if ($target instanceOf SnapshotAttributesProviderInterface) {
141
                $attributes = $target->getSnapshotAttributes();
142
            } else {
143
                $attributes = $this->getSnapshotAttributes();
144
            }
145
        }
146
147
        $data = $this->getSourceHydrator()->extract(clone $source);
148
        $data = array_intersect_key($data, array_flip($attributes));
149
        $this->getHydrator()->hydrate($data, $target);
150
    }
151
152
    public function merge(SnapshotInterface $snapshot)
153
    {
154
        $this->checkEntityType($snapshot);
155
156
        $entity = $snapshot->getSnapshotMeta()->getEntity();
0 ignored issues
show
Bug introduced by
The method getEntity does only exist in Core\Entity\SnapshotMeta, but not in Core\Entity\SnapshotInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
157
158
        $this->copy($snapshot, $entity);
159
160
        return $entity;
161
    }
162
163
    public function findLatest($sourceId, $isDraft = false)
164
    {
165
        return $this->createQueryBuilder()
166
          ->field('snapshotMeta.entity.$id')->equals(new \MongoId($sourceId))
167
          ->field('snapshotMeta.isDraft')->equals($isDraft)
168
          ->sort('snapshotMeta.dateCreated.date', 'desc')
169
          ->limit(1)
170
          ->getQuery()
171
          ->getSingleResult();
172
173
    }
174
175
    public function findBySourceId($sourceId, $includeDrafts = false)
176
    {
177
        $criteria = ['snapshotMeta.entity.$id' => $sourceId];
178
179
        if (!$includeDrafts) {
180
            $criteria['snapshotMeta.isDraft'] = false;
181
        }
182
183
        return $this->findBy($criteria);
184
    }
185
186
    /**
187
     * @param $entity
188
     * @throws \InvalidArgumentException
189
     * @return self
190
     */
191
    public function store($entity)
192
    {
193
        $this->checkEntityType($entity);
194
        $this->dm->persist($entity);
195
        $this->dm->flush($entity);
196
197
        return $this;
198
    }
199
200
    public function remove($entity)
201
    {
202
        $this->checkEntityType($entity);
203
        $this->dm->remove($entity);
204
205
        return $this;
206
    }
207
208
    public function removeAll($sourceId)
0 ignored issues
show
Unused Code introduced by
The parameter $sourceId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
209
    {
210
211
    }
212
213
    protected function checkEntityType($entity)
214
    {
215
        if ( !is_a($entity,  $this->getDocumentName()) ) {
216
            throw new \InvalidArgumentException(sprintf(
217
                'Entity must be of type %s but recieved %s instead',
218
                $this->getDocumentName(),
219
                get_class($entity)
220
            ));
221
        }
222
223
    }
224
225
    protected function extract($source, array $attributes = [])
226
    {
227
        $hydrator = $this->getSourceHydrator();
228
        $data     = $hydrator->extract($source);
229
        $hydrate  = [];
230
231
        if (empty($attributes)) {
232
            $attributes = array_keys($data);
233
        }
234
235
        foreach ($attributes as $key => $spec) {
236
            if (is_numeric($key)) {
237
                $key = $spec;
238
                $spec = null;
239
            }
240
241
            if ($data[$key] instanceOf EntityInterface) {
242
                $hydrate[$key] = clone $data[$key];
243
244
            } else if ($data[$key] instanceOf Collection) {
245
                $collection = new ArrayCollection();
246
                foreach ($data[$key] as $item) {
247
                    $collection->add(clone $item);
248
                }
249
                $hydrate[$key] = $collection;
250
251
            } else {
252
                $hydrate[$key] = $data[$key];
253
            }
254
        }
255
256
        return $hydrate;
257
    }
258
}