Completed
Push — 1.3 ( ea96f1...08f6de )
by Kamil
13:44
created

TotalOfItemsFromTaxonRuleUpdater   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 40
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A updateAfterDeletingTaxon() 0 16 3
A removePromotionRuleIfNecessary() 0 12 3
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Component\Core\Promotion\Updater\Rule;
15
16
use Sylius\Component\Core\Model\TaxonInterface;
17
use Sylius\Component\Core\Promotion\Checker\Rule\TotalOfItemsFromTaxonRuleChecker;
18
use Sylius\Component\Promotion\Model\PromotionRuleInterface;
19
use Sylius\Component\Resource\Repository\RepositoryInterface;
20
21
final class TotalOfItemsFromTaxonRuleUpdater implements TaxonAwareRuleUpdaterInterface
22
{
23
    /** @var RepositoryInterface */
24
    private $promotionRuleRepository;
25
26
    public function __construct(RepositoryInterface $promotionRuleRepository)
27
    {
28
        $this->promotionRuleRepository = $promotionRuleRepository;
29
    }
30
31
    public function updateAfterDeletingTaxon(TaxonInterface $taxon): array
32
    {
33
        $updatedPromotionCodes = [];
34
        $promotionRules = $this->promotionRuleRepository->findBy(['type' => TotalOfItemsFromTaxonRuleChecker::TYPE]);
35
36
        /** @var PromotionRuleInterface $promotionRule */
37
        foreach ($promotionRules as $promotionRule) {
38
            $promotionCode = $this->removePromotionRuleIfNecessary($promotionRule, $taxon->getCode());
39
40
            if (null !== $promotionCode) {
41
                $updatedPromotionCodes[] = $promotionRule->getPromotion()->getCode();
42
            }
43
        }
44
45
        return $updatedPromotionCodes;
46
    }
47
48
    private function removePromotionRuleIfNecessary(PromotionRuleInterface $promotionRule, string $taxonCode): ?string
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
49
    {
50
        foreach ($promotionRule->getConfiguration() as $configuration) {
51
            if ($taxonCode === $configuration['taxon']) {
52
                $this->promotionRuleRepository->remove($promotionRule);
53
54
                return $promotionRule->getPromotion()->getCode();
55
            }
56
        }
57
58
        return null;
59
    }
60
}
61