Passed
Push — master ( 25de29...525d98 )
by Sam
10:52
created

Helper::excelExportField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 14
ccs 6
cts 7
cp 0.8571
crap 2.0116
rs 10
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\Repository\ExportExcelInterface;
13
use Doctrine\ORM\Query;
14
use Doctrine\ORM\QueryBuilder;
15
use Doctrine\ORM\Tools\Pagination\Paginator;
16
use GraphQL\Doctrine\Definition\EntityID;
17
18
abstract class Helper
19
{
20 18
    public static function throwIfDenied(AbstractModel $model, string $privilege): void
21
    {
22 18
        $acl = new Acl();
23 18
        if (!$acl->isCurrentUserAllowed($model, $privilege)) {
24 3
            throw new Exception($acl->getLastDenialMessage());
25
        }
26 15
    }
27
28 5
    public static function paginate(array $pagination, QueryBuilder $query): array
29
    {
30 5
        $offset = $pagination['offset'] ?? 0;
31 5
        $pageIndex = $pagination['pageIndex'] ?? 0;
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
     * @param string $class
62
     * @param QueryBuilder $qb
63
     *
64
     * @return array
65
     */
66 5
    public static function aggregatedFields(string $class, QueryBuilder $qb): array
67
    {
68 5
        $result = [];
69
70 5
        if ($class === Booking::class) {
71 1
            $qb->resetDQLPart('select')
72 1
                ->resetDQLPart('orderBy')
73 1
                ->addSelect('SUM(booking1.participantCount) AS totalParticipantCount')
74 1
                ->addSelect('SUM(bookable.periodicPrice) AS totalPeriodicPrice')
75 1
                ->addSelect('SUM(bookable.initialPrice) AS totalInitialPrice')
76 1
                ->leftJoin('booking1.bookable', 'bookable');
77
78 1
            $result = $qb->getQuery()->getResult()[0];
79 4
        } elseif ($class === Bookable::class) {
80 1
            $qb->resetDQLPart('select')
81 1
                ->resetDQLPart('orderBy')
82 1
                ->addSelect('SUM(bookable1.purchasePrice) AS totalPurchasePrice')
83 1
                ->addSelect('SUM(bookable1.periodicPrice) AS totalPeriodicPrice')
84 1
                ->addSelect('SUM(bookable1.initialPrice) AS totalInitialPrice');
85
86 1
            $result = $qb->getQuery()->getResult()[0];
87 3
        } elseif ($class === TransactionLine::class) {
88 1
            $qb->resetDQLPart('select')
89 1
                ->resetDQLPart('orderBy')
90 1
                ->addSelect('SUM(transactionLine1.balance) AS totalBalance');
91
92 1
            $result = $qb->getQuery()->getResult()[0];
93
        }
94
95 5
        return $result;
96
    }
97
98
    /**
99
     * Lazy resolve the Excel export of the listing query
100
     *
101
     * @param string $class
102
     * @param QueryBuilder $qb
103
     *
104
     * @return array
105
     */
106 5
    public static function excelExportField(string $class, QueryBuilder $qb): array
107
    {
108 5
        $result = [];
109
110 5
        $repository = _em()->getRepository($class);
111
112 5
        if ($repository instanceof ExportExcelInterface) {
113 1
            $query = $qb->getQuery();
114
            $result['excelExport'] = function () use ($query, $repository): string {
115
                return $repository->exportExcel($query);
116
            };
117
        }
118
119 5
        return $result;
120
    }
121
}
122