1 | <?php |
||
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( |
|
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) |
|
109 | |||
110 | 4 | private function validateRuleConfiguration(array $configuration) |
|
123 | |||
124 | 4 | private function configureOptions(OptionsResolver $resolver) |
|
132 | |||
133 | 4 | private function isAllowedType(RuleSubjectInterface $subject) |
|
147 | } |
||
148 |