Failed Conditions
Push — master ( 1176d7...a379d1 )
by Adrien
16:06
created

Helper::aggregatedFields()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

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