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

OrganizationRepository   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 29
ccs 4
cts 4
cp 1
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A applyOrganizationAccesses() 0 21 1
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