Failed Conditions
Push — master ( 34a070...6c283c )
by Adrien
05:56
created

Helper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 37
rs 10
c 0
b 0
f 0
ccs 22
cts 22
cp 1
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A paginate() 0 16 2
A throwIfDenied() 0 5 2
A hydrate() 0 9 3
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 Doctrine\ORM\QueryBuilder;
10
use Doctrine\ORM\Tools\Pagination\Paginator;
11
use GraphQL\Doctrine\Definition\EntityID;
12
13
abstract class Helper
14
{
15 5
    public static function throwIfDenied(AbstractModel $model, string $privilege): void
16
    {
17 5
        $acl = new Acl();
18 5
        if (!$acl->isCurrentUserAllowed($model, $privilege)) {
19 1
            throw new Exception($acl->getLastDenialMessage());
20
        }
21 4
    }
22
23 1
    public static function paginate(array $pagination, QueryBuilder $query): array
24
    {
25 1
        $offset = $pagination['offset'] ?? 0;
26 1
        $pageIndex = $pagination['pageIndex'] ?? 0;
27 1
        $pageSize = $pagination['pageSize'];
28
29 1
        $paginator = new Paginator($query);
30
        $paginator
31 1
            ->getQuery()
32 1
            ->setFirstResult($offset ? $offset : $pageSize * $pageIndex)
33 1
            ->setMaxResults($pageSize);
34
35 1
        $pagination['length'] = $paginator->count();
36 1
        $pagination['items'] = $paginator->getIterator();
37
38 1
        return $pagination;
39
    }
40
41 2
    public static function hydrate(AbstractModel $object, array $input): void
42
    {
43 2
        foreach ($input as $name => $value) {
44 2
            if ($value instanceof EntityID) {
45 1
                $value = $value->getEntity();
46
            }
47
48 2
            $setter = 'set' . ucfirst($name);
49 2
            $object->$setter($value);
50
        }
51 2
    }
52
}
53