|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @copyright 2013 - 2016 Cross Solution <http://cross-solution.de> |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** */ |
|
11
|
|
|
namespace Core\Repository; |
|
12
|
|
|
|
|
13
|
|
|
use Core\Entity\EntityInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Enables an implementing repository to handle draftable entities. |
|
17
|
|
|
* |
|
18
|
|
|
* @method EntityInterface create(array $data = [], bool $persist = false) |
|
19
|
|
|
* |
|
20
|
|
|
* @author Mathias Gelhausen <[email protected]> |
|
21
|
|
|
* @since 0.26 |
|
22
|
|
|
*/ |
|
23
|
|
|
trait DraftableEntityAwareTrait |
|
24
|
|
|
{ |
|
25
|
|
View Code Duplication |
public function findBy(array $criteria, array $sort = null, $limit = null, $skip = null) |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
if (!array_key_exists('isDraft', $criteria)) { |
|
28
|
|
|
$criteria['isDraft'] = false; |
|
29
|
|
|
} elseif (null === $criteria['isDraft']) { |
|
30
|
|
|
unset($criteria['isDraft']); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
|
34
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
35
|
|
|
return parent::findBy($criteria, $sort, $limit, $skip); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Find entities in draft mode. |
|
40
|
|
|
* |
|
41
|
|
|
* Sets the key 'isDraft' with the value true and proxies to parent::findBy() |
|
42
|
|
|
* |
|
43
|
|
|
* @param array $criteria |
|
44
|
|
|
* @param array|null $sort |
|
45
|
|
|
* @param null|int $limit |
|
46
|
|
|
* @param null|int $skip |
|
47
|
|
|
* |
|
48
|
|
|
* @return \MongoCursor |
|
49
|
|
|
*/ |
|
50
|
|
|
public function findDraftsBy(array $criteria, array $sort = null, $limit = null, $skip = null) |
|
51
|
|
|
{ |
|
52
|
|
|
$criteria['isDraft'] = true; |
|
53
|
|
|
|
|
54
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
|
55
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
56
|
|
|
return parent::findBy($criteria, $sort, $limit, $skip); |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
View Code Duplication |
public function findOneBy(array $criteria) |
|
|
|
|
|
|
60
|
|
|
{ |
|
61
|
|
|
if (!array_key_exists('isDraft', $criteria)) { |
|
62
|
|
|
$criteria['isDraft'] = false; |
|
63
|
|
|
} elseif (null === $criteria['isDraft']) { |
|
64
|
|
|
unset($criteria['isDraft']); |
|
65
|
|
|
} |
|
66
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
|
67
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
68
|
|
|
return parent::findOneBy($criteria); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Finds one entity in draft mode. |
|
73
|
|
|
* |
|
74
|
|
|
* Sets the key 'isDraft' to true and proxies to parent::findOneBy() |
|
75
|
|
|
* |
|
76
|
|
|
* @param array $criteria |
|
77
|
|
|
* |
|
78
|
|
|
* @return \MongoCursor |
|
79
|
|
|
*/ |
|
80
|
|
|
public function findOneDraftBy(array $criteria) |
|
81
|
|
|
{ |
|
82
|
|
|
$criteria['isDraft'] = true; |
|
83
|
|
|
|
|
84
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
|
85
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
86
|
|
|
return parent::findOneBy($criteria); |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Creates a query builder. |
|
91
|
|
|
* |
|
92
|
|
|
* Prepopulate the constraint for 'isDraft' according to $findDrafts: |
|
93
|
|
|
* - true: queryBuilder will return only drafts |
|
94
|
|
|
* - false: queryBuilder will return only non drafts. |
|
95
|
|
|
* - null: queryBuilder will return all entities matching the additional constraints. |
|
96
|
|
|
* |
|
97
|
|
|
* @param bool|null $findDrafts |
|
98
|
|
|
* |
|
99
|
|
|
* @return \Doctrine\MongoDB\Query\Builder |
|
100
|
|
|
*/ |
|
101
|
|
View Code Duplication |
public function createQueryBuilder($findDrafts = false) |
|
|
|
|
|
|
102
|
|
|
{ |
|
103
|
|
|
/* @var \Doctrine\MongoDB\Query\Builder $qb */ |
|
104
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
|
105
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
106
|
|
|
$qb = parent::createQueryBuilder(); |
|
107
|
|
|
|
|
108
|
|
|
if (null !== $findDrafts) { |
|
109
|
|
|
$qb->field('isDraft')->equals($findDrafts); |
|
110
|
|
|
} |
|
111
|
|
|
return $qb; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Creates an entity in draft mode. |
|
116
|
|
|
* |
|
117
|
|
|
* @param array $data |
|
|
|
|
|
|
118
|
|
|
* @param bool $persist |
|
119
|
|
|
* |
|
120
|
|
|
* @return EntityInterface |
|
121
|
|
|
*/ |
|
122
|
|
|
public function createDraft(array $data = null, $persist = false) |
|
123
|
|
|
{ |
|
124
|
|
|
$data['isDraft'] = true; |
|
125
|
|
|
|
|
126
|
|
|
return $this->create($data, $persist); |
|
127
|
|
|
} |
|
128
|
|
|
} |
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.