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