-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathFormDataRepository.php
More file actions
164 lines (136 loc) · 4.53 KB
/
Copy pathFormDataRepository.php
File metadata and controls
164 lines (136 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
declare(strict_types=1);
namespace PunktDe\Form\Persistence\Domain\Repository;
/*
* (c) 2020 punkt.de GmbH - Karlsruhe, Germany - https://punkt.de
* All rights reserved.
*/
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Persistence\Doctrine\Repository;
use Neos\Flow\Persistence\Exception\InvalidQueryException;
use Neos\Flow\Persistence\QueryInterface;
use Neos\Flow\Persistence\QueryResultInterface;
use PunktDe\Form\Persistence\Authorization\Service\SiteAccessibilityService;
use PunktDe\Form\Persistence\Domain\Model\FormData;
/**
* @Flow\Scope("singleton")
*/
class FormDataRepository extends Repository
{
/**
* @Flow\Inject
* @var SiteAccessibilityService
*/
protected $siteAccesibilityService;
protected $defaultOrderings = [
'date' => QueryInterface::ORDER_DESCENDING,
];
/**
* @var string[]
*/
protected $accessibleSites = null;
public function initializeObject(): void
{
$this->accessibleSites = $this->initializeAccessibleSites();
}
/**
* @return iterable
*/
public function findAllUniqueForms(): iterable
{
$subSelectQb = $this->createQueryBuilder('form')
->select('form.Persistence_Object_Identifier')
->groupBy('form.formIdentifier')
->addGroupBy('form.hash');
$queryBuilder = $this->createQueryBuilder('form');
$query = $queryBuilder
->addSelect('count(form) AS entryCount')
->addSelect('MAX(form.date) as latestDate')
->andWhere(
$queryBuilder->expr()->in('form.Persistence_Object_Identifier', $subSelectQb->getQuery()->getSQL())
)
->orderBy('latestDate', 'DESC')
->getQuery();
\Neos\Flow\var_dump($query->getSQL(), __METHOD__ . ':' . __LINE__);
die();
}
/**
* @param string $formIdentifier
* @param string $hash
* @return QueryResultInterface
* @throws InvalidQueryException
*/
public function findByFormIdentifierAndHash(string $formIdentifier, string $hash): QueryResultInterface
{
$query = $this->createQuery();
return $query->matching(
$query->logicalAnd(
$query->equals('formIdentifier', $formIdentifier),
$query->equals('hash', $hash),
$query->in('siteName', $this->accessibleSites)
)
)->execute();
}
public function removeByFormIdentifierAndHash(string $formIdentifier, string $hash): void
{
foreach ($this->findByFormIdentifierAndHash($formIdentifier, $hash) as $formData) {
$this->remove($formData);
}
}
/**
* @param string $formIdentifier
* @param string $data
* @return FormData|null
* @throws InvalidQueryException
*/
public function findFormDataWithIdentifierContainingData(string $formIdentifier, string $data): ?FormData
{
$query = $this->createQuery();
$result = $query->matching(
$query->logicalAnd(
$query->equals('formIdentifier', $formIdentifier),
$query->like('formData', '%' . $data . '%'),
$query->in('siteName', $this->accessibleSites)
)
)->execute()->getFirst();
/** @var FormData $result */
return $result;
}
public function createQueryBuilder($alias, $indexBy = null)
{
$queryBuilder = parent::createQueryBuilder($alias, $indexBy);
if (count($this->accessibleSites) > 0) {
return $queryBuilder->andWhere($queryBuilder->expr()->in('form.siteName', $this->accessibleSites));
}
return $queryBuilder->andWhere('2 = 1');
}
/**
* @return string[]
*/
public function getAccessibleSites(): ?array
{
return $this->accessibleSites;
}
protected function initializeAccessibleSites(): array
{
return array_filter(
$this->findAllUniqueSiteNames(),
function (string $site) {
return $this->siteAccesibilityService->isSiteAccessible($site);
}
);
}
protected function findAllUniqueSiteNames(): array
{
$queryBuilder = parent::createQueryBuilder('form');
$formDataGroupedBySite = $queryBuilder
->groupBy('form.siteName')
->getQuery()->execute();
$sites = [];
/** @var FormData $formData */
foreach ($formDataGroupedBySite as $formData) {
$sites[] = $formData->getSiteName();
}
return $sites;
}
}