Helper   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 53
dl 0
loc 90
ccs 56
cts 56
cp 1
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A throwIfDenied() 0 5 2
A hydrate() 0 16 5
A paginate() 0 16 2
A aggregatedFields() 0 40 4
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 Application\Model\User;
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 31
    public static function throwIfDenied(AbstractModel $model, string $privilege): void
21
    {
22 31
        $acl = new Acl();
23 31
        if (!$acl->isCurrentUserAllowed($model, $privilege)) {
24 6
            throw new Exception($acl->getLastDenialMessage());
25
        }
26
    }
27
28 5
    public static function paginate(array $pagination, QueryBuilder $query): array
29
    {
30 5
        $offset = max(0, $pagination['offset']);
31 5
        $pageIndex = max(0, $pagination['pageIndex']);
32 5
        $pageSize = max(0, $pagination['pageSize']);
33
34 5
        $paginator = new Paginator($query);
35 5
        $paginator
36 5
            ->getQuery()
37 5
            ->setFirstResult($offset ?: $pageSize * $pageIndex)
38 5
            ->setMaxResults($pageSize);
39
40 5
        $pagination['length'] = fn () => $paginator->count();
41 5
        $pagination['items'] = $paginator->getIterator();
42
43 5
        return $pagination;
44
    }
45
46 19
    public static function hydrate(AbstractModel $object, array $input): void
47
    {
48
        // Be sure to set owner last, so that it might propagate user status and discount
49 19
        if ($object instanceof User && array_key_exists('owner', $input)) {
50 1
            $owner = $input['owner'];
51 1
            unset($input['owner']);
52 1
            $input['owner'] = $owner;
53
        }
54
55 19
        foreach ($input as $name => $value) {
56 18
            if ($value instanceof EntityID) {
57 7
                $value = $value->getEntity();
58
            }
59
60 18
            $setter = 'set' . ucfirst($name);
61 18
            $object->$setter($value);
62
        }
63
    }
64
65
    /**
66
     * Returns aggregated fields (as scalar) for the given QueryBuilder.
67
     */
68 5
    public static function aggregatedFields(string $class, QueryBuilder $qb): array
69
    {
70 5
        $result = [];
71
72 5
        if ($class === Booking::class) {
73 1
            $qb->resetDQLPart('select')
74 1
                ->resetDQLPart('orderBy')
75 1
                ->addSelect('SUM(booking1.participantCount) AS totalParticipantCount')
76 1
                ->addSelect('SUM(bookable.periodicPrice) AS totalPeriodicPrice')
77 1
                ->addSelect('SUM(bookable.initialPrice) AS totalInitialPrice')
78 1
                ->leftJoin('booking1.bookable', 'bookable');
79
80 1
            $result = $qb->getQuery()->getResult()[0] ?? [
81 1
                'totalParticipantCount' => null,
82 1
                'totalPeriodicPrice' => null,
83 1
                'totalInitialPrice' => null,
84
            ];
85 4
        } elseif ($class === Bookable::class) {
86 1
            $qb->resetDQLPart('select')
87 1
                ->resetDQLPart('orderBy')
88 1
                ->addSelect('SUM(bookable1.purchasePrice) AS totalPurchasePrice')
89 1
                ->addSelect('SUM(bookable1.periodicPrice) AS totalPeriodicPrice')
90 1
                ->addSelect('SUM(bookable1.initialPrice) AS totalInitialPrice');
91
92 1
            $result = $qb->getQuery()->getResult()[0] ?? [
93 1
                'totalPurchasePrice' => null,
94 1
                'totalPeriodicPrice' => null,
95 1
                'totalInitialPrice' => null,
96
            ];
97 3
        } elseif ($class === TransactionLine::class) {
98 1
            $qb->resetDQLPart('select')
99 1
                ->resetDQLPart('orderBy')
100 1
                ->addSelect('SUM(transactionLine1.balance) AS totalBalance');
101
102 1
            $result = $qb->getQuery()->getResult()[0] ?? [
103 1
                'totalBalance' => null,
104
            ];
105
        }
106
107 5
        return $result;
108
    }
109
}
110