Completed
Push — master ( 4da1f4...8b580e )
by Paweł
08:27
created

PackageRuleApplicator::validateRuleConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Core Bundle.
7
 *
8
 * Copyright 2017 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2017 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\Rule\Applicator;
18
19
use SWP\Bundle\CoreBundle\Model\PackageInterface;
20
use SWP\Bundle\CoreBundle\Model\TenantInterface;
21
use SWP\Bundle\CoreBundle\Rule\Populator\ArticlePopulatorInterface;
22
use SWP\Component\Bridge\Model\ContentInterface;
23
use SWP\Component\Common\Exception\UnexpectedTypeException;
24
use SWP\Component\MultiTenancy\Repository\TenantRepositoryInterface;
25
use SWP\Component\Rule\Applicator\AbstractRuleApplicator;
26
use SWP\Component\Rule\Model\RuleInterface;
27
use SWP\Component\Rule\Model\RuleSubjectInterface;
28
use Symfony\Component\OptionsResolver\OptionsResolver;
29
30
final class PackageRuleApplicator extends AbstractRuleApplicator
31
{
32
    /**
33
     * @var TenantRepositoryInterface
34
     */
35
    private $tenantRepository;
36
37
    /**
38
     * @var ArticlePopulatorInterface
39
     */
40
    private $articlePopulator;
41
42
    /**
43
     * @var array
44
     */
45
    private $supportedKeys = ['destinations'];
46
47
    /**
48
     * PackageRuleApplicator constructor.
49
     *
50
     * @param TenantRepositoryInterface $tenantRepository
51
     * @param ArticlePopulatorInterface $articlePopulator
52
     */
53
    public function __construct(
54
        TenantRepositoryInterface $tenantRepository,
55
        ArticlePopulatorInterface $articlePopulator
56
    ) {
57
        $this->tenantRepository = $tenantRepository;
58
        $this->articlePopulator = $articlePopulator;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function apply(RuleInterface $rule, RuleSubjectInterface $subject)
65
    {
66
        $configuration = $this->validateRuleConfiguration($rule->getConfiguration());
67
68
        if ($subject instanceof PackageInterface && !empty($configuration)) {
69
            if (ContentInterface::STATUS_CANCELED === $subject->getPubStatus()) {
70
                return;
71
            }
72
73
            foreach ($configuration[$this->supportedKeys[0]] as $value) {
74
                if (empty($this->validateDestinationConfig($value))) {
75
                    return;
76
                }
77
            }
78
79
            $destinations = [];
80
            foreach ($configuration[$this->supportedKeys[0]] as $destination) {
81
                $destinations[] = $this->findTenantByCodeOrThrowException((string) $destination['tenant']);
82
            }
83
84
            $this->articlePopulator->populate($subject, $destinations);
85
        }
86
    }
87
88 View Code Duplication
    private function findTenantByCodeOrThrowException(string $code): TenantInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
    {
90
        if (!($tenant = $this->tenantRepository->findOneByCode($code)) instanceof TenantInterface) {
91
            throw UnexpectedTypeException::unexpectedType(
92
                is_object($tenant) ? get_class($tenant) : gettype($tenant),
93
                TenantInterface::class);
94
        }
95
96
        return $tenant;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 View Code Duplication
    public function isSupported(RuleSubjectInterface $subject)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
    {
104
        if (!$subject instanceof PackageInterface && 'package' === $subject->getSubjectType()) {
105
            $this->logger->warning(sprintf(
106
                '"%s" is not supported by "%s" rule applicator!',
107
                is_object($subject) ? get_class($subject) : gettype($subject),
108
                get_class($this)
109
            ));
110
111
            return false;
112
        }
113
114
        return true;
115
    }
116
117
    private function validateDestinationConfig(array $config)
118
    {
119
        $resolver = new OptionsResolver();
120
        $resolver->setDefined('tenant');
121
122
        return $this->resolveConfig($resolver, $config);
123
    }
124
125
    private function validateRuleConfiguration(array $config)
126
    {
127
        $resolver = new OptionsResolver();
128
        $resolver->setDefined($this->supportedKeys[0]);
129
130
        return $this->resolveConfig($resolver, $config);
131
    }
132
}
133