Passed
Push — master ( f3b4d6...85dd39 )
by Sam
11:35
created

FileRepository::getAccessibleSubQuery()   B

Complexity

Conditions 10
Paths 37

Size

Total Lines 59
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 10.0751

Importance

Changes 0
Metric Value
eloc 21
c 0
b 0
f 0
dl 0
loc 59
ccs 20
cts 22
cp 0.9091
rs 7.6666
cc 10
nc 37
nop 1
crap 10.0751

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Repository;
6
7
use Application\DBAL\Types\ProductTypeType;
8
use Application\Model\Order;
9
use Application\Model\User;
10
use Ecodev\Felix\Repository\LimitedAccessSubQuery;
11
12
class FileRepository extends AbstractRepository implements LimitedAccessSubQuery
13
{
14
    /**
15
     * Returns pure SQL to get ID of all objects that are accessible to given user.
16
     *
17
     * A user can access a file if at least one of the following condition is true:
18
     *
19
     * - he has flag webTemporaryAccess
20
     * - he has web subscription (digital/both) and the product reviewNumber is included in that subscription
21
     * - he bought the product
22
     * - the product is free and active
23
     *
24
     * @param null|User $user
25
     */
26 5
    public function getAccessibleSubQuery(?\Ecodev\Felix\Model\User $user): string
27
    {
28 5
        if ($user && in_array($user->getRole(), [User::ROLE_FACILITATOR, User::ROLE_ADMINISTRATOR], true)) {
29 2
            return $this->getAllIdsQuery();
30
        }
31
32 3
        $queries = [];
33
34 3
        $connection = $this->getEntityManager()->getConnection();
35 3
        $subscriptionLastReviewNumber = $user ? $user->getSubscriptionLastReviewNumber() : null;
36 3
        $hasSubscription = $user && ProductTypeType::includesDigital($user->getSubscriptionType()) && $subscriptionLastReviewNumber;
37 3
        $digitalTypes = implode(',', array_map(fn (string $val): string => $connection->quote($val), ProductTypeType::getDigitalTypes()));
38
39 3
        if ($user && $user->getWebTemporaryAccess()) {
40
            // Files for webTemporaryAccess
41
            $queries[] = '
42
SELECT product.file_id FROM product
43
WHERE
44
product.is_active
45
AND product.file_id IS NOT NULL
46
AND product.type IN (' . $digitalTypes . ')';
47 3
        } elseif ($hasSubscription) {
48 1
            $allowedReviewNumber = $connection->quote($subscriptionLastReviewNumber);
49
50
            // Files for web subscription
51 1
            $queries[] = '
52
SELECT product.file_id FROM product
53
LEFT JOIN product AS review ON product.review_id = review.id
54
WHERE
55
product.is_active
56
AND product.file_id IS NOT NULL
57 1
AND product.type IN (' . $digitalTypes . ')
58 1
AND (product.review_number <= ' . $allowedReviewNumber . ' OR review.review_number <= ' . $allowedReviewNumber . ')';
59
        }
60
61 3
        if ($user) {
62
            // Files for products that were bought directly
63 2
            $queries[] = '
64
SELECT product.file_id FROM product
65
INNER JOIN order_line ON product.id = order_line.product_id
66
INNER JOIN `order` ON order_line.order_id = `order`.id
67 2
AND `order`.status = ' . $connection->quote(Order::STATUS_VALIDATED) . '
68 2
AND `order`.owner_id =  ' . $user->getId() . '
69
WHERE
70
product.is_active
71
AND product.file_id IS NOT NULL
72
';
73
        }
74
75
        // Free product are accessible to everybody
76 3
        $queries[] = '
77
SELECT product.file_id FROM product
78
WHERE
79
product.is_active
80
AND product.file_id IS NOT NULL
81
AND product.price_per_unit_chf = 0
82
AND product.price_per_unit_eur = 0';
83
84 3
        return implode(' UNION ', $queries);
85
    }
86
}
87