Completed
Push — develop ( 62651e...ddc457 )
by
unknown
09:17
created

SnapshotRepository::setHydrator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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 Doctrine\Common\Collections\Collection;
18
use Doctrine\ODM\MongoDB\DocumentRepository;
19
use Zend\Hydrator\HydratorInterface;
20
21
/**
22
 * ${CARET}
23
 * 
24
 * @author Mathias Gelhausen <[email protected]>
25
 * @todo write test 
26
 */
27
class SnapshotRepository extends DocumentRepository
28
{
29
    /**
30
     *
31
     *
32
     * @var HydratorInterface
33
     */
34
    protected $hydrator;
35
36
    /**
37
     *
38
     *
39
     * @var HydratorInterface
40
     */
41
    protected $sourceHydrator;
42
43
    /**
44
     *
45
     *
46
     * @var array
47
     */
48
    protected $snapshotAttributes = [];
49
50
    /**
51
     * @param \Zend\Hydrator\HydratorInterface $hydrator
52
     *
53
     * @return self
54
     */
55
    public function setHydrator($hydrator)
56
    {
57
        $this->hydrator = $hydrator;
58
59
        return $this;
60
    }
61
62
    /**
63
     * @return \Zend\Hydrator\HydratorInterface
64
     */
65
    public function getHydrator()
66
    {
67
        if (!$this->hydrator) {
68
            $this->setHydrator(new EntityHydrator());
69
        }
70
        return $this->hydrator;
71
    }
72
73
    /**
74
     * @param \Zend\Hydrator\HydratorInterface $sourceHydrator
75
     *
76
     * @return self
77
     */
78
    public function setSourceHydrator($sourceHydrator)
79
    {
80
        $this->sourceHydrator = $sourceHydrator;
81
82
        return $this;
83
    }
84
85
    /**
86
     * @return \Zend\Hydrator\HydratorInterface
87
     */
88
    public function getSourceHydrator()
89
    {
90
        if (!$this->sourceHydrator) {
91
            return $this->getHydrator();
92
        }
93
94
        return $this->sourceHydrator;
95
    }
96
97
    /**
98
     * @param array $snapshotAttributes
99
     *
100
     * @return self
101
     */
102
    public function setSnapshotAttributes($snapshotAttributes)
103
    {
104
        $this->snapshotAttributes = $snapshotAttributes;
105
106
        return $this;
107
    }
108
109
    /**
110
     * @return array
111
     */
112
    public function getSnapshotAttributes()
113
    {
114
        return $this->snapshotAttributes;
115
    }
116
117
118
119
    public function create(EntityInterface $source, $persist = true)
120
    {
121
122
        $snapshot = $this->getDocumentName();
123
        $snapshot = new $snapshot($source);
124
        $attributes = $snapshot instanceOf SnapshotAttributesProviderInterface
125
                    ? $snapshot->getSnapshotAttributes()
126
                    : $this->getSnapshotAttributes();
127
128
129
        $data = $this->getSourceHydrator()->extract(clone $source);
130
        $data = array_intersect_key($data, array_flip($attributes));
131
        $snapshot = $this->getHydrator()->hydrate($data, $snapshot);
132
133
        if ($persist) {
134
            $this->store($snapshot);
135
        }
136
137
        return $snapshot;
138
    }
139
140
    public function findLatest($sourceId, $isDraft = false)
141
    {
142
        return $this->createQueryBuilder()
143
          ->field('snapshotMeta.entity.$id')->equals(new \MongoId($sourceId))
144
          ->field('snapshotMeta.isDraft')->equals($isDraft)
145
          ->sort('snapshotMeta.dateCreated.date', 'desc')
146
          ->limit(1)
147
          ->getQuery()
148
          ->getSingleResult();
149
150
    }
151
152
    public function findBySourceId($sourceId, $includeDrafts = false)
153
    {
154
        $criteria = ['snapshotMeta.entity.$id' => $sourceId];
155
156
        if (!$includeDrafts) {
157
            $criteria['snapshotMeta.isDraft'] = false;
158
        }
159
160
        return $this->findBy($criteria);
161
    }
162
163
    /**
164
     * @param $entity
165
     * @throws \InvalidArgumentException
166
     * @return self
167
     */
168
    public function store($entity)
169
    {
170
        $this->checkEntityType($entity);
171
        $this->dm->persist($entity);
172
        $this->dm->flush($entity);
173
174
        return $this;
175
    }
176
177
    public function remove($entity)
178
    {
179
        $this->checkEntityType($entity);
180
        $this->dm->remove($entity);
181
182
        return $this;
183
    }
184
185
    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...
186
    {
187
188
    }
189
190
    protected function checkEntityType($entity)
191
    {
192
        if ( !is_a($entity,  $this->getDocumentName()) ) {
193
            throw new \InvalidArgumentException(sprintf(
194
                'Entity must be of type %s but recieved %s instead',
195
                $this->getDocumentName(),
196
                get_class($entity)
197
            ));
198
        }
199
200
    }
201
202
    protected function extract($source, array $attributes = [])
203
    {
204
        $hydrator = $this->getSourceHydrator();
205
        $data     = $hydrator->extract($source);
206
        $hydrate  = [];
207
208
        if (empty($attributes)) {
209
            $attributes = array_keys($data);
210
        }
211
212
        foreach ($attributes as $key => $spec) {
213
            if (is_numeric($key)) {
214
                $key = $spec;
215
                $spec = null;
216
            }
217
218
            if ($data[$key] instanceOf EntityInterface) {
219
                $hydrate[$key] = clone $data[$key];
220
221
            } else if ($data[$key] instanceOf Collection) {
222
                $collection = new ArrayCollection();
223
                foreach ($data[$key] as $item) {
224
                    $collection->add(clone $item);
225
                }
226
                $hydrate[$key] = $collection;
227
228
            } else {
229
                $hydrate[$key] = $data[$key];
230
            }
231
        }
232
233
        return $hydrate;
234
    }
235
}