Failed Conditions
Push — master ( b8b413...1117cf )
by Adrien
06:48
created

FileRepository   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 72.72%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 34
ccs 8
cts 11
cp 0.7272
rs 10
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
        $webTypes = [ProductTypeType::BOTH, ProductTypeType::DIGITAL];
32 1
        if ($user->getWebTemporaryAccess() || in_array($user->getSubscriptionType(), $webTypes, true)) {
33
            $webTypesSql = implode(',', array_map(function (string $val): string {
34
                return $this->getEntityManager()->getConnection()->quote($val);
35
            }, $webTypes));
36
37
            return '
38
SELECT file.id FROM file
39
INNER JOIN product p ON file.product_id = p.id AND p.is_active AND p.type IN (' . $webTypesSql . ') 
40
';
41
        }
42
43 1
        return '-1';
44
    }
45
}
46