Completed
Push — master ( da73fe...897cdb )
by Andrii
03:46
created

CommandBus::buildTactician()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
crap 2
1
<?php
2
3
namespace hiapi\components;
4
5
use League\Tactician\Handler\CommandHandlerMiddleware;
6
use League\Tactician\CommandBus as Worker;
7
8
class CommandBus extends \yii\base\Component
9
{
10
    public $extractor;
11
    public $locator;
12
    public $inflector;
13
    public $middlewares = [];
14
15
    protected $bus;
16
17
    protected function buildTactician()
18
    {
19
        $handler = Yii::createObject(CommandHandlerMiddleware::class, [
20
            $this->getExtractor(),
0 ignored issues
show
Documentation Bug introduced by
The method getExtractor does not exist on object<hiapi\components\CommandBus>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
21
            $this->getLocator(),
22
            $this->getInflector(),
0 ignored issues
show
Documentation Bug introduced by
The method getInflector does not exist on object<hiapi\components\CommandBus>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
23
        ]);
24
25
        $this->registerHandler($handler);
0 ignored issues
show
Unused Code introduced by
The call to the method hiapi\components\CommandBus::registerHandler() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
26
27
        return new Worker($this->middlewares);
28
    }
29
30
    public function getLocator()
31
    {
32
        if (is_array($this->locator)) {
33
            $this->locator = Yii::createObject($this->locator);
34
        }
35
36
        return $this->locator;
37
    }
38
39
    public function registerHandler($middleware)
0 ignored issues
show
Unused Code introduced by
The parameter $middleware is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
    {
41
        foreach ($this->middlewares as $middleware) {
0 ignored issues
show
Unused Code introduced by
This foreach statement is empty and can be removed.

This check looks for foreach loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
42
        }
43
    }
44
45
46
    public function handle($command)
47
    {
48
        return $this->getBus()->handle($command);
0 ignored issues
show
Documentation Bug introduced by
The method getBus does not exist on object<hiapi\components\CommandBus>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
49
    }
50
51
    public function getTactician()
52
    {
53
        if ($this->bus === null) {
54
            $this->bus = $this->buildTactician();
55
        }
56
57
        return $this->bus;
58
    }
59
}
60