Completed
Push — master ( 3ef484...ea5778 )
by Paweł
08:46
created

PackageRuleApplicator::apply()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 15
nc 7
nop 2
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\Factory\PublishActionFactoryInterface;
20
use SWP\Bundle\CoreBundle\Model\PackageInterface;
21
use SWP\Bundle\CoreBundle\Rule\PublishDestinationResolverInterface;
22
use SWP\Bundle\CoreBundle\Service\ArticlePublisherInterface;
23
use SWP\Component\Bridge\Model\ContentInterface;
24
use SWP\Component\Rule\Applicator\AbstractRuleApplicator;
25
use SWP\Component\Rule\Model\RuleInterface;
26
use SWP\Component\Rule\Model\RuleSubjectInterface;
27
use Symfony\Component\OptionsResolver\OptionsResolver;
28
29
final class PackageRuleApplicator extends AbstractRuleApplicator
30
{
31
    /**
32
     * @var ArticlePublisherInterface
33
     */
34
    private $articlePublisher;
35
36
    /**
37
     * @var PublishDestinationResolverInterface
38
     */
39
    private $publishDestinationResolver;
40
41
    /**
42
     * @var PublishActionFactoryInterface
43
     */
44
    private $publishActionFactory;
45
46
    /**
47
     * @var array
48
     */
49
    private $supportedKeys = ['destinations'];
50
51
    /**
52
     * PackageRuleApplicator constructor.
53
     *
54
     * @param ArticlePublisherInterface           $articlePublisher
55
     * @param PublishDestinationResolverInterface $publishDestinationResolver
56
     * @param PublishActionFactoryInterface       $publishActionFactory
57
     */
58
    public function __construct(
59
        ArticlePublisherInterface $articlePublisher,
60
        PublishDestinationResolverInterface $publishDestinationResolver,
61
        PublishActionFactoryInterface $publishActionFactory
62
    ) {
63
        $this->articlePublisher = $articlePublisher;
64
        $this->publishDestinationResolver = $publishDestinationResolver;
65
        $this->publishActionFactory = $publishActionFactory;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function apply(RuleInterface $rule, RuleSubjectInterface $subject)
72
    {
73
        $configuration = $this->validateRuleConfiguration($rule->getConfiguration());
74
75
        if ($subject instanceof PackageInterface && !empty($configuration)) {
76
            if (ContentInterface::STATUS_CANCELED === $subject->getPubStatus()) {
77
                return;
78
            }
79
80
            foreach ($configuration[$this->supportedKeys[0]] as $value) {
81
                if (empty($this->validateDestinationConfig($value))) {
82
                    return;
83
                }
84
            }
85
86
            $destinations = [];
87
            foreach ($configuration[$this->supportedKeys[0]] as $destination) {
88
                $destinations[] = $this->publishDestinationResolver->resolve(
89
                    $destination['tenant'],
90
                    $destination['route']
91
                );
92
            }
93
94
            $publishAction = $this->publishActionFactory->createWithDestinations($destinations);
95
96
            $this->articlePublisher->publish($subject, $publishAction);
97
        }
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 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...
104
    {
105
        if (!$subject instanceof PackageInterface && 'package' === $subject->getSubjectType()) {
106
            $this->logger->warning(sprintf(
107
                '"%s" is not supported by "%s" rule applicator!',
108
                is_object($subject) ? get_class($subject) : gettype($subject),
109
                get_class($this)
110
            ));
111
112
            return false;
113
        }
114
115
        return true;
116
    }
117
118
    private function validateDestinationConfig(array $config)
119
    {
120
        $resolver = new OptionsResolver();
121
        $resolver->setDefined('tenant');
122
        $resolver->setDefined('route');
123
124
        return $this->resolveConfig($resolver, $config);
125
    }
126
127
    private function validateRuleConfiguration(array $config)
128
    {
129
        $resolver = new OptionsResolver();
130
        $resolver->setDefined($this->supportedKeys[0]);
131
132
        return $this->resolveConfig($resolver, $config);
133
    }
134
}
135