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

PackageRuleApplicator   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 106
Duplicated Lines 13.21 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 9
dl 14
loc 106
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
C apply() 0 28 7
A isSupported() 14 14 4
A validateDestinationConfig() 0 8 1
A validateRuleConfiguration() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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