Passed
Push — master ( d59596...9d2c64 )
by Adrien
27:21 queued 13:39
created

Helper::hydrate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 2
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
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\Order;
10
use Application\Model\OrderLine;
11
use Doctrine\ORM\QueryBuilder;
12
use Doctrine\ORM\Tools\Pagination\Paginator;
13
use Ecodev\Felix\Api\Exception;
14
use GraphQL\Doctrine\Definition\EntityID;
15
16
abstract class Helper
17
{
18 14
    public static function throwIfDenied(AbstractModel $model, string $privilege): void
19
    {
20 14
        $acl = new Acl();
21 14
        if (!$acl->isCurrentUserAllowed($model, $privilege)) {
22 2
            throw new Exception($acl->getLastDenialMessage());
23
        }
24
    }
25
26 4
    public static function paginate(array $pagination, QueryBuilder $query): array
27
    {
28 4
        $offset = max(0, $pagination['offset']);
29 4
        $pageIndex = max(0, $pagination['pageIndex']);
30 4
        $pageSize = max(0, $pagination['pageSize']);
31
32 4
        $paginator = new Paginator($query);
33 4
        $paginator
34 4
            ->getQuery()
35 4
            ->setFirstResult($offset ?: $pageSize * $pageIndex)
36 4
            ->setMaxResults($pageSize);
37
38 4
        $pagination['length'] = $paginator->count();
39 4
        $pagination['items'] = $paginator->getIterator();
40
41 4
        return $pagination;
42
    }
43
44 4
    public static function hydrate(AbstractModel $object, array $input): void
45
    {
46 4
        foreach ($input as $name => $value) {
47 4
            if ($value instanceof EntityID) {
48 1
                $value = $value->getEntity();
49
            }
50
51 4
            $setter = 'set' . ucfirst($name);
52 4
            $object->$setter($value);
53
        }
54
    }
55
56
    /**
57
     * Returns aggregated fields (as scalar) for the given QueryBuilder.
58
     */
59 3
    public static function aggregatedFields(string $class, QueryBuilder $qb): array
60
    {
61 3
        $result = [];
62
63 3
        if ($class === OrderLine::class) {
64
            $qb->resetDQLPart('select')
65
                ->resetDQLPart('orderBy')
66
                ->addSelect('SUM(orderLine1.balanceCHF) AS totalBalanceCHF')
67
                ->addSelect('SUM(orderLine1.balanceEUR) AS totalBalanceEUR')
68
                ->addSelect('SUM(orderLine1.quantity) AS totalQuantity');
69
70
            $result = $qb->getQuery()->getResult()[0] ?? [
71
                'totalBalanceCHF' => null,
72
                'totalBalanceEUR' => null,
73
                'totalQuantity' => null,
74
            ];
75 3
        } elseif ($class === Order::class) {
76
            $qb->resetDQLPart('select')
77
                ->resetDQLPart('orderBy')
78
                ->addSelect('SUM(order1.balanceCHF) AS totalBalanceCHF')
79
                ->addSelect('SUM(order1.balanceEUR) AS totalBalanceEUR');
80
81
            $result = $qb->getQuery()->getResult()[0] ?? [
82
                'totalBalanceCHF' => null,
83
                'totalBalanceEUR' => null,
84
            ];
85
        }
86
87 3
        return $result;
88
    }
89
}
90