Failed Conditions
Push — master ( 81605b...f7b62b )
by Sam
09:20
created

FileRepository   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 34
rs 10
ccs 10
cts 11
cp 0.9091
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A getAccessibleSubQuery() 0 23 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Repository;
6
7
use Application\DBAL\Types\ProductTypeType;
8
use Application\Model\User;
9
10
class FileRepository extends AbstractRepository implements LimitedAccessSubQueryInterface
11
{
12
    /**
13
     * Returns pure SQL to get ID of all objects that are accessible to given user.
14
     *
15
     * Give access to users that have flag webTemporaryAccess or to users that have web subscription (digital/both)
16
     *
17
     * @param null|User $user
18
     *
19
     * @return string
20
     */
21 4
    public function getAccessibleSubQuery(?User $user): string
22
    {
23 4
        if (!$user) {
24 1
            return '-1';
25
        }
26
27 3
        if (in_array($user->getRole(), [User::ROLE_FACILITATOR, User::ROLE_ADMINISTRATOR], true)) {
28 2
            return $this->getAllIdsQuery();
29
        }
30
31 1
        if (!$user->getWebTemporaryAccess() && !in_array($user->getSubscriptionType(), [ProductTypeType::BOTH, ProductTypeType::DIGITAL], true)) {
32
            $webTypesSql = '(' .
33 1
                $this->getEntityManager()->getConnection()->quote(ProductTypeType::BOTH) . ', ' .
34 1
                $this->getEntityManager()->getConnection()->quote(ProductTypeType::DIGITAL) .
35 1
                ')';
36
37
            return '
38
SELECT file.id FROM file
39 1
INNER JOIN product p ON file.product_id = p.id AND p.is_active and p.type IN ' . $webTypesSql . '
40
';
41
        }
42
43
        return '';
44
    }
45
}
46