1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Netgen\InformationCollection\Doctrine\Repository; |
6
|
|
|
|
7
|
|
|
use DateTimeImmutable; |
8
|
|
|
use Doctrine\ORM\EntityRepository; |
9
|
|
|
use Doctrine\ORM\NonUniqueResultException; |
10
|
|
|
use Doctrine\ORM\NoResultException; |
11
|
|
|
use Doctrine\ORM\ORMException; |
12
|
|
|
use Doctrine\ORM\ORMInvalidArgumentException; |
13
|
|
|
use eZ\Publish\API\Repository\Values\Content\Content; |
14
|
|
|
use eZ\Publish\API\Repository\Values\User\User; |
15
|
|
|
use Netgen\InformationCollection\API\Exception\RemoveCollectionFailedException; |
16
|
|
|
use Netgen\InformationCollection\API\Exception\RetrieveCountException; |
17
|
|
|
use Netgen\InformationCollection\API\Exception\StoringCollectionFailedException; |
18
|
|
|
use Netgen\InformationCollection\Doctrine\Entity\EzInfoCollection; |
19
|
|
|
|
20
|
|
|
class EzInfoCollectionRepository extends EntityRepository |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Get new \Netgen\InformationCollection\Doctrine\Entity\EzInfoCollection instance. |
24
|
|
|
* |
25
|
|
|
* @return \Netgen\InformationCollection\Doctrine\Entity\EzInfoCollection |
26
|
|
|
*/ |
27
|
|
|
public function getInstance() |
28
|
|
|
{ |
29
|
|
|
return new EzInfoCollection(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function loadCollection(int $collectionId) |
33
|
|
|
{ |
34
|
|
|
$collection = $this->findOneBy(['id' => $collectionId]); |
35
|
|
|
|
36
|
|
|
if ($collection instanceof EzInfoCollection) { |
37
|
|
|
return $collection; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getFirstCollection(int $contentId): EzInfoCollection |
42
|
|
|
{ |
43
|
|
|
$collection = $this->findOneBy( |
44
|
|
|
[ |
45
|
|
|
'contentObjectId' => $contentId, |
46
|
|
|
], |
47
|
|
|
[ |
48
|
|
|
'created' => 'ASC', |
49
|
|
|
] |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
if ($collection instanceof EzInfoCollection) { |
53
|
|
|
return $collection; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getLastCollection(int $contentId): EzInfoCollection |
58
|
|
|
{ |
59
|
|
|
$collection = $this->findOneBy( |
60
|
|
|
[ |
61
|
|
|
'contentObjectId' => $contentId, |
62
|
|
|
], |
63
|
|
|
[ |
64
|
|
|
'created' => 'DESC', |
65
|
|
|
] |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
if ($collection instanceof EzInfoCollection) { |
69
|
|
|
return $collection; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function createNewFromValues(Content $content, User $user): EzInfoCollection |
74
|
|
|
{ |
75
|
|
|
$ezInfo = $this->getInstance(); |
76
|
|
|
$dt = new DateTimeImmutable(); |
77
|
|
|
|
78
|
|
|
$ezInfo->setContentObjectId($content->contentInfo->id); |
79
|
|
|
$ezInfo->setUserIdentifier($user->login); |
80
|
|
|
$ezInfo->setCreatorId($user->id); |
81
|
|
|
$ezInfo->setCreated($dt->getTimestamp()); |
82
|
|
|
$ezInfo->setModified($dt->getTimestamp()); |
83
|
|
|
|
84
|
|
|
return $ezInfo; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Save object. |
89
|
|
|
* |
90
|
|
|
* @param \Netgen\InformationCollection\Doctrine\Entity\EzInfoCollection $informationCollection |
91
|
|
|
* |
92
|
|
|
* @throws StoringCollectionFailedException |
93
|
|
|
*/ |
94
|
|
|
public function save(EzInfoCollection $informationCollection) |
95
|
|
|
{ |
96
|
|
|
try { |
97
|
|
|
$this->_em->persist($informationCollection); |
98
|
|
|
$this->_em->flush($informationCollection); |
99
|
|
|
} catch (ORMException | ORMInvalidArgumentException $e) { |
100
|
|
|
throw new StoringCollectionFailedException('', ''); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param array $collections |
106
|
|
|
* |
107
|
|
|
* @throws RemoveCollectionFailedException |
108
|
|
|
*/ |
109
|
|
|
public function remove(array $collections) |
110
|
|
|
{ |
111
|
|
|
try { |
112
|
|
|
foreach ($collections as $collection) { |
113
|
|
|
$this->_em->remove($collection); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$this->_em->flush(); |
117
|
|
|
} catch (ORMException | ORMInvalidArgumentException $e) { |
118
|
|
|
throw new RemoveCollectionFailedException('', ''); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function findByContentId($contentId) |
123
|
|
|
{ |
124
|
|
|
$qb = $this->createQueryBuilder('ezc'); |
125
|
|
|
|
126
|
|
|
return $qb->select('ezc') |
127
|
|
|
->where('ezc.contentObjectId = :content') |
128
|
|
|
->setParameter('content', $contentId) |
129
|
|
|
->getQuery() |
130
|
|
|
->getResult(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function findByContentIdOlderThan($contentId, DateTimeImmutable $date) |
134
|
|
|
{ |
135
|
|
|
$qb = $this->createQueryBuilder('ezc'); |
136
|
|
|
|
137
|
|
|
return $qb->select('ezc') |
138
|
|
|
->where('ezc.contentObjectId = :content') |
139
|
|
|
->setParameter('content', $contentId) |
140
|
|
|
->andWhere($qb->expr()->lte('ezc.created', $date->getTimestamp())) |
141
|
|
|
->getQuery() |
142
|
|
|
->getResult(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function getChildrenCount($contentId) |
146
|
|
|
{ |
147
|
|
|
try { |
148
|
|
|
return (int) $this->createQueryBuilder('ezc') |
149
|
|
|
->andWhere('ezc.contentObjectId = :contentId') |
150
|
|
|
->setParameter('contentId', $contentId) |
151
|
|
|
->select('COUNT(ezc) as children_count') |
152
|
|
|
->getQuery() |
153
|
|
|
->getSingleScalarResult(); |
154
|
|
|
} catch (NonUniqueResultException | NoResultException $e) { |
155
|
|
|
throw new RetrieveCountException('', ''); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|