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\ContentBundle\ArticleEvents; |
20
|
|
|
use SWP\Bundle\ContentBundle\Event\ArticleEvent; |
21
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleInterface; |
22
|
|
|
use SWP\Component\Rule\Applicator\AbstractRuleApplicator; |
23
|
|
|
use SWP\Component\Rule\Model\RuleSubjectInterface; |
24
|
|
|
use SWP\Component\Rule\Model\RuleInterface; |
25
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
26
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
27
|
|
|
|
28
|
|
View Code Duplication |
final class PublishArticleToAppleNewsRuleApplicator extends AbstractRuleApplicator |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var EventDispatcherInterface |
32
|
|
|
*/ |
33
|
|
|
private $eventDispatcher; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $supportedKeys = ['isPublishedToAppleNews']; |
39
|
|
|
|
40
|
|
|
public function __construct(EventDispatcherInterface $eventDispatcher) |
41
|
|
|
{ |
42
|
|
|
$this->eventDispatcher = $eventDispatcher; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function apply(RuleInterface $rule, RuleSubjectInterface $subject) |
46
|
|
|
{ |
47
|
|
|
$configuration = $this->validateRuleConfiguration($rule->getConfiguration()); |
48
|
|
|
|
49
|
|
|
if (empty($configuration) || !$this->isAllowedType($subject)) { |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ($isPublishedToAppleNews = (bool) $configuration[$this->supportedKeys[0]]) { |
54
|
|
|
$subject->setPublishedToAppleNews($isPublishedToAppleNews); |
|
|
|
|
55
|
|
|
$this->eventDispatcher->dispatch(ArticleEvents::PUBLISH, new ArticleEvent($subject, null, ArticleEvents::PUBLISH)); |
|
|
|
|
56
|
|
|
|
57
|
|
|
$this->logger->info(sprintf( |
58
|
|
|
'Configuration: "%s" for "%s" rule has been applied!', |
59
|
|
|
json_encode($configuration, JSON_THROW_ON_ERROR, 512), |
60
|
|
|
$rule->getExpression() |
61
|
|
|
)); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function isSupported(RuleSubjectInterface $subject) |
69
|
|
|
{ |
70
|
|
|
return $subject instanceof ArticleInterface && 'article' === $subject->getSubjectType(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function validateRuleConfiguration(array $configuration) |
74
|
|
|
{ |
75
|
|
|
$resolver = new OptionsResolver(); |
76
|
|
|
$this->configureOptions($resolver, $configuration); |
77
|
|
|
|
78
|
|
|
return $this->resolveConfig($resolver, $configuration); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function configureOptions(OptionsResolver $resolver, array $configuration) |
82
|
|
|
{ |
83
|
|
|
$resolver->setDefaults([ |
84
|
|
|
$this->supportedKeys[0] => false, |
85
|
|
|
]); |
86
|
|
|
$resolver->setDefined(array_keys($configuration)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function isAllowedType(RuleSubjectInterface $subject) |
90
|
|
|
{ |
91
|
|
|
if (!$subject instanceof ArticleInterface) { |
92
|
|
|
$this->logger->warning(sprintf( |
93
|
|
|
'"%s" is not supported by "%s" rule applicator!', |
94
|
|
|
is_object($subject) ? get_class($subject) : gettype($subject), |
95
|
|
|
get_class($this) |
96
|
|
|
)); |
97
|
|
|
|
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return true; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: