Passed
Push — master ( fdf037...88d356 )
by Adrien
07:14
created

getSubscriptionLastReviewNumber()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 10
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 17
ccs 10
cts 10
cp 1
crap 2
rs 9.9332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Repository;
6
7
use Application\Model\AbstractModel;
8
use Application\Model\Organization;
9
use Application\Model\User;
10
use Ecodev\Felix\Repository\LimitedAccessSubQuery;
11
12
class ProductRepository 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
     * - product is active
20
     * - he is facilitator or administrator
21
     *
22
     * @param null|User $user
23
     */
24 11
    public function getAccessibleSubQuery(?\Ecodev\Felix\Model\User $user): string
25
    {
26 11
        if ($user && in_array($user->getRole(), [User::ROLE_FACILITATOR, User::ROLE_ADMINISTRATOR], true)) {
27 4
            return $this->getAllIdsQuery();
28
        }
29
30 7
        return 'SELECT id FROM product WHERE product.is_active';
31
    }
32
33
    // Set random sorting on all products
34
    public function randomizeSorting(): void
35
    {
36
        $count = $this->getCount();
37
38
        if ($count) {
39
            $connection = $this->getEntityManager()->getConnection();
40
            $connection->executeUpdate('UPDATE ' . $this->getClassMetadata()->getTableName() . ' SET sorting = FLOOR(1 + RAND() * ?)', [$count]);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Connection::executeUpdate() has been deprecated: Use {@link executeStatement()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

40
            /** @scrutinizer ignore-deprecated */ $connection->executeUpdate('UPDATE ' . $this->getClassMetadata()->getTableName() . ' SET sorting = FLOOR(1 + RAND() * ?)', [$count]);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
41
        }
42
    }
43
44
    /**
45
     * @param Organization|User $hasSubscriptionLastReview
46
     */
47 3
    public function getSubscriptionLastReviewNumber(AbstractModel $hasSubscriptionLastReview): ?int
48
    {
49 3
        $class = get_class($hasSubscriptionLastReview);
50 3
        $table = $this->getEntityManager()->getClassMetadata($class)->getTableName();
51
52 3
        $connection = $this->getEntityManager()->getConnection();
53
54 3
        $sql = "SELECT review_number FROM product INNER JOIN $table ON $table.subscription_last_review_id = product.id AND $table.id = " . $hasSubscriptionLastReview->getId();
55 3
        $result = $connection->fetchOne($sql);
56
57 3
        if (is_numeric($result)) {
58 2
            $result = (int) $result;
59
        } else {
60 2
            $result = null;
61
        }
62
63 3
        return $result;
64
    }
65
}
66