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 |
|
|
|
|
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
|
|
|
|
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.