Completed
Push — master ( 2d6521...17de1c )
by Kristof
10s
created

AuthorizedCommandBus::setContext()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\CommandHandling;
4
5
use Broadway\CommandHandling\CommandBusInterface;
6
use Broadway\Domain\Metadata;
7
use CultuurNet\UDB3\Offer\Commands\AuthorizableCommandInterface;
8
use CultuurNet\UDB3\Security\CommandAuthorizationException;
9
use CultuurNet\UDB3\Security\SecurityInterface;
10
use CultuurNet\UDB3\Security\UserIdentificationInterface;
11
use Psr\Log\LoggerAwareInterface;
12
use Psr\Log\LoggerInterface;
13
14
class AuthorizedCommandBus extends CommandBusDecoratorBase implements AuthorizedCommandBusInterface, LoggerAwareInterface, ContextAwareInterface
15
{
16
    /**
17
     * @var Metadata
18
     */
19
    protected $metadata;
20
21
    /**
22
     * @var UserIdentificationInterface
23
     */
24
    private $userIdentification;
25
26
    /**
27
     * @var SecurityInterface
28
     */
29
    private $security;
30
31
    /**
32
     * AuthorizedCommandBus constructor.
33
     * @param CommandBusInterface $decoratee
34
     * @param UserIdentificationInterface $userIdentification
35
     * @param SecurityInterface $security
36
     */
37
    public function __construct(
38
        CommandBusInterface $decoratee,
39
        UserIdentificationInterface $userIdentification,
40
        SecurityInterface $security
41
    ) {
42
        parent::__construct($decoratee);
43
44
        $this->userIdentification = $userIdentification;
45
        $this->security = $security;
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function dispatch($command)
52
    {
53
        if ($command instanceof AuthorizableCommandInterface) {
54
            $authorized = $this->isAuthorized($command);
55
        } else {
56
            $authorized = true;
57
        }
58
59
        if ($authorized) {
60
            parent::dispatch($command);
61
        } else {
62
            throw new CommandAuthorizationException(
63
                $this->userIdentification->getId(),
0 ignored issues
show
Bug introduced by
It seems like $this->userIdentification->getId() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
64
                $command
65
            );
66
        }
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72
    public function isAuthorized(AuthorizableCommandInterface $command)
73
    {
74
        return $this->security->isAuthorized($command);
75
    }
76
77
    /**
78
     * @return UserIdentificationInterface
79
     */
80
    public function getUserIdentification()
81
    {
82
        return $this->userIdentification;
83
    }
84
85
    /**
86
     * Sets a logger instance on the object.
87
     *
88
     * @param LoggerInterface $logger
89
     *
90
     * @return void
91
     */
92
    public function setLogger(LoggerInterface $logger)
93
    {
94
        $this->decoratee->setLogger($logger);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Broadway\CommandHandling\CommandBusInterface as the method setLogger() does only exist in the following implementations of said interface: CultuurNet\UDB3\CommandH...ng\AuthorizedCommandBus, CultuurNet\UDB3\CommandHandling\ResqueCommandBus, CultuurNet\UDB3\CommandH...eContextAwareCommandBus.

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...
95
    }
96
97
    /**
98
     * @param Metadata|null $context
99
     */
100
    public function setContext(Metadata $context = null)
101
    {
102
        $this->metadata = $context;
103
104
        if ($this->decoratee instanceof ContextAwareInterface) {
105
            $this->decoratee->setContext($context);
106
        }
107
    }
108
}
109