Completed
Push — master ( f6198e...300d99 )
by Paweł
53:20
created

ArticleRuleApplicator::apply()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 37
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7.2694

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 14
cts 17
cp 0.8235
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 19
nc 8
nop 2
crap 7.2694
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\Model\RouteInterface;
20
use SWP\Bundle\ContentBundle\Provider\RouteProviderInterface;
21
use SWP\Bundle\ContentBundle\Service\ArticleServiceInterface;
22
use SWP\Component\Rule\Applicator\RuleApplicatorInterface;
23
use SWP\Component\Rule\Model\RuleSubjectInterface;
24
use SWP\Component\Rule\Model\RuleInterface;
25
use Symfony\Component\OptionsResolver\OptionsResolver;
26
27
final class ArticleRuleApplicator implements RuleApplicatorInterface
28
{
29
    /**
30
     * @var RouteProviderInterface
31
     */
32
    private $routeProvider;
33
34
    /**
35
     * @var LoggerInterface
36
     */
37
    private $logger;
38
39
    /**
40
     * @var ArticleServiceInterface
41
     */
42
    private $articleService;
43
44
    /**
45
     * @var array
46
     */
47
    private $supportedKeys = ['route', 'templateName', 'published'];
48
49
    /**
50
     * ArticleRuleApplicator constructor.
51
     *
52
     * @param RouteProviderInterface  $routeProvider
53
     * @param LoggerInterface         $logger
54
     * @param ArticleServiceInterface $articleService
55 11
     */
56
    public function __construct(
57
        RouteProviderInterface $routeProvider,
58
        LoggerInterface $logger,
59
        ArticleServiceInterface $articleService
60 11
    ) {
61 11
        $this->routeProvider = $routeProvider;
62 11
        $this->logger = $logger;
63 11
        $this->articleService = $articleService;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68 5
     */
69
    public function apply(RuleInterface $rule, RuleSubjectInterface $subject)
70 5
    {
71
        $configuration = $this->validateRuleConfiguration($rule->getConfiguration());
72 5
73
        if (!$this->isAllowedType($subject) || empty($configuration)) {
74
            return;
75
        }
76
77 5
        /* @var ArticleInterface $subject */
78 3
        if (isset($configuration['route'])) {
79
            $route = $this->routeProvider->getOneById($configuration['route']);
80 3
81
            if (null === $route) {
82
                $this->logger->warning('Route not found! Make sure the rule defines an existing route!');
83
84
                return;
85
            }
86 3
87
            $subject->setRoute($route);
88
89 5
            if (RouteInterface::TYPE_CONTENT === $route->getType()) {
90
                $route->setContent($subject);
91 5
            }
92 2
        }
93
94
        $subject->setTemplateName($configuration['templateName']);
95 4
96 4
        if ((bool) $configuration['published']) {
97
            $this->articleService->publish($subject);
98 4
        }
99
100 4
        $this->logger->info(sprintf(
101
            'Configuration: "%s" for "%s" rule has been applied!',
102
            json_encode($configuration),
103
            $rule->getExpression()
104
        ));
105 5
    }
106
107 5
    /**
108
     * {@inheritdoc}
109
     */
110 5
    public function isSupported(RuleSubjectInterface $subject)
111
    {
112 5
        return $subject instanceof ArticleInterface && 'article' === $subject->getSubjectType();
113 5
    }
114
115
    private function validateRuleConfiguration(array $configuration)
116 5
    {
117
        $resolver = new OptionsResolver();
118
        $this->configureOptions($resolver);
119
120
        try {
121
            return $resolver->resolve($configuration);
122
        } catch (\Exception $e) {
123
            $this->logger->warning($e->getMessage());
124 5
        }
125
126 5
        return [];
127 5
    }
128 5
129
    private function configureOptions(OptionsResolver $resolver)
130 5
    {
131 5
        $resolver->setDefaults([
132
            $this->supportedKeys[1] => null,
133 5
            $this->supportedKeys[2] => false,
134
        ]);
135 5
        $resolver->setDefined($this->supportedKeys[0]);
136
    }
137
138
    private function isAllowedType(RuleSubjectInterface $subject)
139
    {
140
        if (!$subject instanceof ArticleInterface) {
141
            $this->logger->warning(sprintf(
142
                '"%s" is not supported by "%s" rule applicator!',
143
                is_object($subject) ? get_class($subject) : gettype($subject),
144
                get_class($this)
145 5
            ));
146
147
            return false;
148
        }
149
150
        return true;
151
    }
152
}
153