Passed
Push — master ( a8b983...5b3176 )
by Adrien
10:48
created

OrderLineRepository::getAccessibleSubQuery()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
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 1
dl 0
loc 11
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\Repository;
6
7
use Application\Model\Order;
8
use Application\Model\Product;
9
use Application\Model\User;
10
use Doctrine\ORM\Query\Expr\Join;
11
use Doctrine\ORM\QueryBuilder;
12
use Ecodev\Felix\Repository\LimitedAccessSubQuery;
13
14
class OrderLineRepository extends AbstractRepository implements LimitedAccessSubQuery
15
{
16
    /**
17
     * Returns pure SQL to get ID of all objects that are accessible to given user.
18
     *
19
     * @param null|User $user
20
     */
21 8
    public function getAccessibleSubQuery(?\Ecodev\Felix\Model\User $user): string
22
    {
23 8
        if (!$user) {
24 1
            return '-1';
25
        }
26
27 7
        if (in_array($user->getRole(), [User::ROLE_FACILITATOR, User::ROLE_ADMINISTRATOR], true)) {
28 3
            return $this->getAllIdsQuery();
29
        }
30
31 4
        return $this->getAllIdsForOwnerQuery($user);
32
    }
33
34 1
    public function createPurchaseQueryBuilder(array $filters, array $sortings): QueryBuilder
35
    {
36 1
        $qbProduct = _types()->createFilteredQueryBuilder(Product::class, $filters, []);
37
38 1
        $qb = $this->createQueryBuilder('orderLine')
39 1
            ->addSelect('product')
40 1
            ->innerJoin('orderLine.order', 'o', Join::WITH, 'o.status = :status AND o.owner = :user')
41 1
            ->innerJoin('orderLine.product', 'product', Join::WITH, 'product.isActive = TRUE')
42 1
            ->andWhere('orderLine.product IN (' . $qbProduct->getDQL() . ')')
43 1
            ->setParameter('status', Order::STATUS_VALIDATED)
44 1
            ->setParameter('user', User::getCurrent());
45
46
        // Apply sort on products
47 1
        foreach ($sortings as $sorting) {
48 1
            $qb->addOrderBy('product.' . $sorting['field'], $sorting['order']);
49
        }
50
51
        /** @var \Doctrine\ORM\Query\Parameter $parameter */
52 1
        foreach ($qbProduct->getParameters() as $parameter) {
53
            $qb->setParameter($parameter->getName(), $parameter->getValue(), $parameter->getType());
54
        }
55
56 1
        return $qb;
57
    }
58
}
59