Passed
Push — master ( c962bb...3cb4da )
by Adrien
09:12
created

Helper   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 75.56%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 41
c 1
b 0
f 0
dl 0
loc 72
ccs 34
cts 45
cp 0.7556
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A hydrate() 0 9 3
A throwIfDenied() 0 5 2
A paginate() 0 16 2
A aggregatedFields() 0 29 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\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 12
    }
26
27 4
    public static function paginate(array $pagination, QueryBuilder $query): array
28
    {
29 4
        $offset = $pagination['offset'] ?? 0;
30 4
        $pageIndex = $pagination['pageIndex'];
31 4
        $pageSize = $pagination['pageSize'];
32
33 4
        $paginator = new Paginator($query);
34
        $paginator
35 4
            ->getQuery()
36 4
            ->setFirstResult($offset ? $offset : $pageSize * $pageIndex)
37 4
            ->setMaxResults($pageSize);
38
39 4
        $pagination['length'] = $paginator->count();
40 4
        $pagination['items'] = $paginator->getIterator();
41
42 4
        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 4
    }
56
57
    /**
58
     * Returns aggregated fields (as scalar) for the given QueryBuilder
59
     */
60 3
    public static function aggregatedFields(string $class, QueryBuilder $qb): array
61
    {
62 3
        $result = [];
63
64 3
        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 2
        } 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 2
        } 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 3
        return $result;
89
    }
90
}
91