Completed
Push — master ( 586166...fecb59 )
by Paweł
47:58
created

ArticleRuleApplicator::validateRuleConfiguration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.3149

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 4
cts 7
cp 0.5714
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
crap 2.3149
1
<?php
2
3
/**
4
 * This file is part of the Superdesk Web Publisher Content Bundle.
5
 *
6
 * Copyright 2016 Sourcefabric z.ú. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\ContentBundle\Rule\Applicator;
16
17
use Psr\Log\LoggerInterface;
18
use SWP\Bundle\ContentBundle\Model\ArticleInterface;
19
use SWP\Bundle\ContentBundle\Provider\RouteProviderInterface;
20
use SWP\Component\Rule\Applicator\RuleApplicatorInterface;
21
use SWP\Component\Rule\Model\RuleSubjectInterface;
22
use SWP\Component\Rule\Model\RuleInterface;
23
use Symfony\Component\OptionsResolver\OptionsResolver;
24
25
final class ArticleRuleApplicator implements RuleApplicatorInterface
26
{
27
    /**
28
     * @var RouteProviderInterface
29
     */
30
    private $routeProvider;
31
32
    /**
33
     * @var LoggerInterface
34
     */
35
    private $logger;
36
37
    /**
38
     * @var array
39
     */
40
    private $supportedKeys = ['route', 'templateName'];
41
42
    /**
43
     * ArticleRuleApplicator constructor.
44
     *
45
     * @param RouteProviderInterface $routeProvider
46
     * @param LoggerInterface        $logger
47
     */
48 11
    public function __construct(RouteProviderInterface $routeProvider, LoggerInterface $logger)
49
    {
50 11
        $this->routeProvider = $routeProvider;
51 11
        $this->logger = $logger;
52 11
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 2
    public function apply(RuleInterface $rule, RuleSubjectInterface $subject)
58
    {
59 2
        $configuration = $this->validateRuleConfiguration($rule->getConfiguration());
60
61 2
        if (!$this->isAllowedType($subject) || empty($configuration)) {
62
            return;
63
        }
64
65
        /* @var ArticleInterface $subject */
66 2
        if (isset($configuration[$this->supportedKeys[0]])) {
67 2
            $route = $this->routeProvider->getOneById($configuration[$this->supportedKeys[0]]);
68
69 2
            if (null === $route) {
70
                $this->logger->warning('Route not found! Make sure the rule defines an existing route!');
71
72
                return;
73
            }
74
75 2
            $subject->setRoute($route);
76
        }
77
78 2
        $subject->setTemplateName($configuration[$this->supportedKeys[1]]);
79
80 2
        $this->logger->info(sprintf(
81 2
            'Configuration: "%s" for "%s" rule has been applied!',
82
            json_encode($configuration),
83 2
            $rule->getValue()
84
        ));
85 2
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 2
    public function isSupported(RuleSubjectInterface $subject)
91
    {
92 2
        return $subject instanceof ArticleInterface && 'article' === $subject->getSubjectType();
93
    }
94
95 2
    private function validateRuleConfiguration(array $configuration)
96
    {
97 2
        $resolver = new OptionsResolver();
98 2
        $this->configureOptions($resolver);
99
100
        try {
101 2
            return $resolver->resolve($configuration);
102
        } catch (\Exception $e) {
103
            $this->logger->warning($e->getMessage());
104
        }
105
106
        return [];
107
    }
108
109 2
    private function configureOptions(OptionsResolver $resolver)
110
    {
111 2
        $resolver->setDefaults([$this->supportedKeys[1] => null]);
112 2
        $resolver->setDefined($this->supportedKeys[0]);
113 2
    }
114
115 2
    private function isAllowedType(RuleSubjectInterface $subject)
116
    {
117 2
        if (!$subject instanceof ArticleInterface) {
118
            $this->logger->warning(sprintf(
119
                '"%s" is not supported by "%s" rule applicator!',
120
                is_object($subject) ? get_class($subject) : gettype($subject),
121
                get_class($this)
122
            ));
123
124
            return false;
125
        }
126
127 2
        return true;
128
    }
129
}
130