Passed
Push — master ( b8255a...26b0ba )
by Adrien
08:35
created

()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 0
loc 19
ccs 10
cts 10
cp 1
crap 1
rs 9.9666
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Repository;
6
7
class OrganizationRepository extends AbstractRepository
8
{
9
    /**
10
     * Apply the organizations accesses to all users
11
     *
12
     * - If a user email matches the org pattern and the org access is better, then user will get the org access
13
     * - If many organizations match then only the best one is used
14
     */
15 10
    public function applyOrganizationAccesses(): void
16
    {
17 10
        $connection = $this->getEntityManager()->getConnection();
18
19
        $sqlUpgrade = <<<STRING
20 10
                UPDATE user
21
                INNER JOIN (
22
                    SELECT user.email, MAX(product.review_number) AS bestReviewNumber
23
                    FROM organization
24
                    INNER JOIN product ON organization.subscription_last_review_id = product.id
25
                    INNER JOIN user ON user.email REGEXP organization.pattern
26
                    GROUP BY user.email
27
                ) AS bestMatch ON user.email = bestMatch.email
28
                INNER JOIN product AS orgProduct ON orgProduct.review_number = bestMatch.bestReviewNumber
29
                LEFT JOIN product AS userProduct ON userProduct.id = user.subscription_last_review_id
30
                SET
31
                    user.subscription_last_review_id = IF(userProduct.id IS NULL OR orgProduct.review_number > userProduct.review_number, orgProduct.id, userProduct.id),
32
                    user.subscription_type = IF(user.subscription_type IN ('paper', 'both'), 'both', 'digital')
33
            STRING;
34
35 10
        $connection->executeUpdate($sqlUpgrade);
36 10
    }
37
}
38