Completed
Push — master ( 4da1f4...8b580e )
by Paweł
08:27
created

configureOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
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
final class PublishArticleToFBIARuleApplicator extends AbstractRuleApplicator
29
{
30
    /**
31
     * @var EventDispatcherInterface
32
     */
33
    private $eventDispatcher;
34
35
    /**
36
     * @var array
37
     */
38
    private $supportedKeys = ['fbia'];
39
40
    /**
41
     * ArticleRuleApplicator constructor.
42
     *
43
     * @param EventDispatcherInterface $eventDispatcher
44
     */
45
    public function __construct(EventDispatcherInterface $eventDispatcher)
46
    {
47
        $this->eventDispatcher = $eventDispatcher;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function apply(RuleInterface $rule, RuleSubjectInterface $subject)
54
    {
55
        $configuration = $this->validateRuleConfiguration($rule->getConfiguration());
56
57
        if (!$this->isAllowedType($subject) || empty($configuration)) {
58
            return;
59
        }
60
61
        if ($isPublishedFBIA = (bool) $configuration['fbia']) {
62
            $subject->setPublishedFBIA($isPublishedFBIA);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface SWP\Component\Rule\Model\RuleSubjectInterface as the method setPublishedFBIA() does only exist in the following implementations of said interface: SWP\Bundle\CoreBundle\Model\Article.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
63
            $this->eventDispatcher->dispatch(ArticleEvents::PUBLISH, new ArticleEvent($subject, null, ArticleEvents::PUBLISH));
0 ignored issues
show
Compatibility introduced by
$subject of type object<SWP\Component\Rul...l\RuleSubjectInterface> is not a sub-type of object<SWP\Bundle\Conten...Model\ArticleInterface>. It seems like you assume a child interface of the interface SWP\Component\Rule\Model\RuleSubjectInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
64
65
            $this->logger->info(sprintf(
66
                'Configuration: "%s" for "%s" rule has been applied!',
67
                json_encode($configuration),
68
                $rule->getExpression()
69
            ));
70
        }
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function isSupported(RuleSubjectInterface $subject)
77
    {
78
        return $subject instanceof ArticleInterface && 'article' === $subject->getSubjectType();
79
    }
80
81
    private function validateRuleConfiguration(array $configuration)
82
    {
83
        $resolver = new OptionsResolver();
84
        $this->configureOptions($resolver);
85
86
        return $this->resolveConfig($resolver, $configuration);
87
    }
88
89 View Code Duplication
    private function configureOptions(OptionsResolver $resolver)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
    {
91
        $resolver->setDefaults([
92
            $this->supportedKeys[0] => false,
93
        ]);
94
        $resolver->setDefined($this->supportedKeys[0]);
95
    }
96
97
    private function isAllowedType(RuleSubjectInterface $subject)
98
    {
99
        if (!$subject instanceof ArticleInterface) {
100
            $this->logger->warning(sprintf(
101
                '"%s" is not supported by "%s" rule applicator!',
102
                is_object($subject) ? get_class($subject) : gettype($subject),
103
                get_class($this)
104
            ));
105
106
            return false;
107
        }
108
109
        return true;
110
    }
111
}
112