Passed
Push — develop ( 4090f6...ac2b4d )
by BENARD
09:57
created

AbstractBadgeStrategy   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 36
c 1
b 0
f 0
dl 0
loc 132
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getRuleId() 0 23 2
A companyHasGlobalRule() 0 5 1
A getTranslator() 0 3 1
A insertBaseRule() 0 31 2
A __construct() 0 3 1
A extractRuleProductType() 0 15 3
1
<?php
2
3
namespace VideoGamesRecords\CoreBundle\Manager\Strategy\Badge;
4
5
use Symfony\Contracts\Translation\TranslatorInterface;
6
use VideoGamesRecords\CoreBundle\Contracts\BadgeInterface;
7
8
abstract class AbstractBadgeStrategy implements BadgeInterface
9
{
10
    private TranslatorInterface $translator;
11
12
    /**
13
     * @param TranslatorInterface $translator
14
     */
15
    public function __construct(TranslatorInterface $translator)
16
    {
17
        $this->translator = $translator;
18
    }
19
20
    /**
21
     * @return TranslatorInterface
22
     */
23
    protected function getTranslator(): TranslatorInterface
24
    {
25
        return $this->translator;
26
    }
27
28
29
    /**
30
     * @param Contribution $rule
31
     *
32
     * @return string
33
     *
34
     * @throws \UnexpectedValueException
35
     */
36
    protected function extractRuleProductType(Contribution $rule): string
0 ignored issues
show
Bug introduced by
The type VideoGamesRecords\CoreBu...tegy\Badge\Contribution was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
37
    {
38
        $ruleProductType = $rule->getProductType();
39
40
        if (!array_key_exists($ruleProductType, $this->productTypeMapping)) {
0 ignored issues
show
Bug Best Practice introduced by
The property productTypeMapping does not exist on VideoGamesRecords\CoreBu...e\AbstractBadgeStrategy. Did you maybe forget to declare it?
Loading history...
41
            throw new \UnexpectedValueException(sprintf('Could not extract product type from %s', $ruleProductType));
42
        }
43
44
        $category = $this->productTypeMapping[$ruleProductType];
45
46
        if (!array_key_exists($category, $this->productCategoryMapping)) {
0 ignored issues
show
Bug Best Practice introduced by
The property productCategoryMapping does not exist on VideoGamesRecords\CoreBu...e\AbstractBadgeStrategy. Did you maybe forget to declare it?
Loading history...
47
            throw new \UnexpectedValueException(sprintf('Could not get type for category %s', $category));
48
        }
49
50
        return $this->productCategoryMapping[$category];
51
    }
52
53
    /**
54
     * @param int    $companyId
55
     * @param bool   $isInternal
56
     * @param string $ruleType
57
     * @param string $campaignType
58
     * @param string $productType
59
     *
60
     * @return int
61
     *
62
     * @throws DuplicateGlobalRuleException
63
     */
64
    protected function insertBaseRule(
65
        int $companyId,
66
        bool $isInternal,
67
        string $ruleType,
68
        string $campaignType = self::TYPE_ALL,
0 ignored issues
show
Bug introduced by
The constant VideoGamesRecords\CoreBu...BadgeStrategy::TYPE_ALL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
69
        string $productType = self::TYPE_ALL
70
    ): int
71
    {
72
        try {
73
74
            $this->connection->executeStatement(
0 ignored issues
show
Bug Best Practice introduced by
The property connection does not exist on VideoGamesRecords\CoreBu...e\AbstractBadgeStrategy. Did you maybe forget to declare it?
Loading history...
75
                'INSERT INTO rule_company
76
                    (company_id, campaign_type, product_type, is_internal, rule_type)
77
                    VALUES (:companyId, :campaignType, :productType,  :isInternal, :ruleType)',
78
                [
79
                    'companyId'    => $companyId,
80
                    'campaignType' => $campaignType,
81
                    'productType'  => $productType,
82
                    'isInternal'   => $isInternal,
83
                    'ruleType'     => $ruleType,
84
                ],
85
                [
86
                    'isInternal' => 'boolean',
87
                ]
88
            );
89
90
        } catch (UniqueConstraintViolationException $e) {
0 ignored issues
show
Bug introduced by
The type VideoGamesRecords\CoreBu...raintViolationException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
91
            throw new DuplicateGlobalRuleException(sprintf('Same rule already exists for company %d', $companyId));
0 ignored issues
show
Bug introduced by
The type VideoGamesRecords\CoreBu...cateGlobalRuleException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
92
        }
93
94
        return (int) $this->connection->lastInsertId();
95
    }
96
97
    /**
98
     * @param int         $companyId
99
     * @param string      $campaignType
100
     * @param string      $productType
101
     * @param string|null $ruleType
102
     *
103
     * @return int
104
     */
105
    protected function getRuleId(int $companyId, string $campaignType, string $productType, ?string $ruleType): int
106
    {
107
        $sql = 'SELECT
108
            id
109
            FROM rule_company
110
            WHERE
111
                company_id = :companyId
112
                AND campaign_type = :campaignType
113
                AND product_type = :productType
114
        ';
115
116
        $params = [
117
            'companyId'    => $companyId,
118
            'campaignType' => $campaignType,
119
            'productType'  => $productType,
120
        ];
121
122
        if ($ruleType !== null) {
123
            $sql .= ' AND rule_type = :ruleType';
124
            $params['ruleType'] = $ruleType;
125
        }
126
127
        return (int) $this->connection->executeQuery($sql, $params)->fetchOne();
0 ignored issues
show
Bug Best Practice introduced by
The property connection does not exist on VideoGamesRecords\CoreBu...e\AbstractBadgeStrategy. Did you maybe forget to declare it?
Loading history...
128
    }
129
130
    /**
131
     * @param int $companyId
132
     *
133
     * @return bool
134
     */
135
    protected function companyHasGlobalRule(int $companyId, string $ruleType = null): bool
136
    {
137
        $ruleId = $this->getRuleId($companyId, self::TYPE_ALL, self::TYPE_ALL, $ruleType);
0 ignored issues
show
Bug introduced by
The constant VideoGamesRecords\CoreBu...BadgeStrategy::TYPE_ALL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
138
139
        return $ruleId > 0;
140
    }
141
}