1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Application\Api; |
6
|
|
|
|
7
|
|
|
use Application\Acl\Acl; |
8
|
|
|
use Application\Model\AbstractModel; |
9
|
|
|
use Application\Model\Bookable; |
10
|
|
|
use Application\Model\Booking; |
11
|
|
|
use Application\Model\TransactionLine; |
12
|
|
|
use Doctrine\ORM\Query; |
13
|
|
|
use Doctrine\ORM\QueryBuilder; |
14
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator; |
15
|
|
|
use Ecodev\Felix\Api\Exception; |
16
|
|
|
use GraphQL\Doctrine\Definition\EntityID; |
17
|
|
|
|
18
|
|
|
abstract class Helper |
19
|
|
|
{ |
20
|
21 |
|
public static function throwIfDenied(AbstractModel $model, string $privilege): void |
21
|
|
|
{ |
22
|
21 |
|
$acl = new Acl(); |
23
|
21 |
|
if (!$acl->isCurrentUserAllowed($model, $privilege)) { |
24
|
4 |
|
throw new Exception($acl->getLastDenialMessage()); |
25
|
|
|
} |
26
|
17 |
|
} |
27
|
|
|
|
28
|
5 |
|
public static function paginate(array $pagination, QueryBuilder $query): array |
29
|
|
|
{ |
30
|
5 |
|
$offset = $pagination['offset'] ?? 0; |
31
|
5 |
|
$pageIndex = $pagination['pageIndex']; |
32
|
5 |
|
$pageSize = $pagination['pageSize']; |
33
|
|
|
|
34
|
5 |
|
$paginator = new Paginator($query); |
35
|
|
|
$paginator |
36
|
5 |
|
->getQuery() |
37
|
5 |
|
->setFirstResult($offset ? $offset : $pageSize * $pageIndex) |
38
|
5 |
|
->setMaxResults($pageSize); |
39
|
|
|
|
40
|
5 |
|
$pagination['length'] = $paginator->count(); |
41
|
5 |
|
$pagination['items'] = $paginator->getIterator(); |
42
|
|
|
|
43
|
5 |
|
return $pagination; |
44
|
|
|
} |
45
|
|
|
|
46
|
15 |
|
public static function hydrate(AbstractModel $object, array $input): void |
47
|
|
|
{ |
48
|
15 |
|
foreach ($input as $name => $value) { |
49
|
14 |
|
if ($value instanceof EntityID) { |
50
|
5 |
|
$value = $value->getEntity(); |
51
|
|
|
} |
52
|
|
|
|
53
|
14 |
|
$setter = 'set' . ucfirst($name); |
54
|
14 |
|
$object->$setter($value); |
55
|
|
|
} |
56
|
15 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns aggregated fields (as scalar) for the given QueryBuilder |
60
|
|
|
*/ |
61
|
5 |
|
public static function aggregatedFields(string $class, QueryBuilder $qb): array |
62
|
|
|
{ |
63
|
5 |
|
$result = []; |
64
|
|
|
|
65
|
5 |
|
if ($class === Booking::class) { |
66
|
1 |
|
$qb->resetDQLPart('select') |
67
|
1 |
|
->resetDQLPart('orderBy') |
68
|
1 |
|
->addSelect('SUM(booking1.participantCount) AS totalParticipantCount') |
69
|
1 |
|
->addSelect('SUM(bookable.periodicPrice) AS totalPeriodicPrice') |
70
|
1 |
|
->addSelect('SUM(bookable.initialPrice) AS totalInitialPrice') |
71
|
1 |
|
->leftJoin('booking1.bookable', 'bookable'); |
72
|
|
|
|
73
|
1 |
|
$result = $qb->getQuery()->getResult()[0]; |
74
|
4 |
|
} elseif ($class === Bookable::class) { |
75
|
1 |
|
$qb->resetDQLPart('select') |
76
|
1 |
|
->resetDQLPart('orderBy') |
77
|
1 |
|
->addSelect('SUM(bookable1.purchasePrice) AS totalPurchasePrice') |
78
|
1 |
|
->addSelect('SUM(bookable1.periodicPrice) AS totalPeriodicPrice') |
79
|
1 |
|
->addSelect('SUM(bookable1.initialPrice) AS totalInitialPrice'); |
80
|
|
|
|
81
|
1 |
|
$result = $qb->getQuery()->getResult()[0]; |
82
|
3 |
|
} elseif ($class === TransactionLine::class) { |
83
|
1 |
|
$qb->resetDQLPart('select') |
84
|
1 |
|
->resetDQLPart('orderBy') |
85
|
1 |
|
->addSelect('SUM(transactionLine1.balance) AS totalBalance'); |
86
|
|
|
|
87
|
1 |
|
$result = $qb->getQuery()->getResult()[0]; |
88
|
|
|
} |
89
|
|
|
|
90
|
5 |
|
return $result; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|