Completed
Pull Request — master (#583)
by Rafał
09:54
created

PaywallSecuredRuleApplicator::apply()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 4
nc 3
nop 2
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 2018 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 2018 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\Rule\Applicator;
18
19
use SWP\Bundle\ContentBundle\Model\ArticleInterface;
20
use SWP\Component\Rule\Applicator\AbstractRuleApplicator;
21
use SWP\Component\Rule\Model\RuleSubjectInterface;
22
use SWP\Component\Rule\Model\RuleInterface;
23
use Symfony\Component\OptionsResolver\OptionsResolver;
24
25
final class PaywallSecuredRuleApplicator extends AbstractRuleApplicator
26
{
27
    /**
28
     * @var array
29
     */
30
    private $supportedKeys = ['paywallSecured'];
31
32
    public function apply(RuleInterface $rule, RuleSubjectInterface $subject): void
33
    {
34
        $configuration = $this->validateRuleConfiguration($rule->getConfiguration());
35
36
        if (empty($configuration) || !$this->isAllowedType($subject)) {
37
            return;
38
        }
39
40
        if ($isPaywallSecured = (bool) $configuration['paywallSecured']) {
41
            $subject->setPaywallSecured($isPaywallSecured);
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 setPaywallSecured() 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...
42
43
            $this->logger->info(sprintf(
44
                'Configuration: paywallSecured for "%s" rule has been applied!',
45
                $rule->getExpression()
46
            ));
47
        }
48
    }
49
50
    public function isSupported(RuleSubjectInterface $subject): bool
51
    {
52
        return $subject instanceof ArticleInterface && 'article' === $subject->getSubjectType();
53
    }
54
55
    private function validateRuleConfiguration(array $configuration): array
56
    {
57
        $resolver = new OptionsResolver();
58
        $this->configureOptions($resolver);
59
60
        return $this->resolveConfig($resolver, $configuration);
61
    }
62
63 View Code Duplication
    private function configureOptions(OptionsResolver $resolver): void
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...
64
    {
65
        $resolver->setDefaults([
66
            $this->supportedKeys[0] => false,
67
        ]);
68
        $resolver->setDefined($this->supportedKeys[0]);
69
    }
70
71
    private function isAllowedType(RuleSubjectInterface $subject): bool
72
    {
73
        if (!$subject instanceof ArticleInterface) {
74
            $this->logger->warning(sprintf(
75
                '"%s" is not supported by "%s" rule applicator!',
76
                is_object($subject) ? get_class($subject) : gettype($subject),
77
                get_class($this)
78
            ));
79
80
            return false;
81
        }
82
83
        return true;
84
    }
85
}
86