AggregateChildEventException   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 95
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
B calculateListenerName() 0 18 6
A getEventName() 0 4 1
A getEvent() 0 4 1
A getListener() 0 4 1
A getListenerName() 0 4 1
A getWrappedError() 0 4 1
1
<?php
2
namespace Workana\AsyncJobs\EventDispatching;
3
4
use Exception;
5
use ReflectionFunction;
6
7
/**
8
 * Aggregate child of event exception
9
 *
10
 * @author Carlos Frutos <[email protected]>
11
 */
12
class AggregateChildEventException extends Exception
13
{
14
    /**
15
     * @var AggregateRootEventException
16
     */
17
    protected $parent;
18
19
    /**
20
     * @var Callable
21
     */
22
    protected $listener;
23
24
    /**
25
     * @var string
26
     */
27
    protected $listenerName;
28
29
    /**
30
     * @var Throwable|Exception
31
     */
32
    protected $wrappedError;
33
34
    /**
35
     * @var AggregateRootEventException $parent
36
     * @var Callable $listener
37
     * @var Exception|Throwable $wrappedError
38
     */
39
    public function __construct(
40
        AggregateRootEventException $parent,
41
        Callable $listener,
42
        $wrappedError
43
    ) {
44
        $this->parent = $parent;
45
        $this->listener = $listener;
46
        $this->listenerName = $this->calculateListenerName($listener);
47
        $this->wrappedError = $wrappedError;
48
49
        parent::__construct(strtr('Failed event :eventName on listener :listenerName with message: :errorMessage', [
50
            ':eventName' => $this->parent->getEventName(),
51
            ':listenerName' => $this->listenerName,
52
            ':errorMessage' => $this->wrappedError->getMessage(),
53
        ]));
54
    }
55
56
    /**
57
     * Calculate listener name
58
     *
59
     * @param Callable $listener
60
     *
61
     * @return string
62
     */
63
    private function calculateListenerName(Callable $listener)
64
    {
65
        if (is_array($listener)) {
66
            $class = is_object($listener[0]) ? get_class($listener[0]) : (string) $listener[0];
67
68
            return implode('::', [$class, $listener[1]]);
69
        } elseif ($listener instanceof Closure) {
0 ignored issues
show
Bug introduced by
The class Workana\AsyncJobs\EventDispatching\Closure does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
70
            $reflection = new ReflectionFunction($callable);
0 ignored issues
show
Bug introduced by
The variable $callable does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
71
72
            return "[closure]#{$reflection->getFileName()}:{$reflection->getStartLine()}";
73
        } elseif (is_string($listener)) {
74
            return $listener;
75
        } elseif (is_object($listener)) {
76
            $class = get_class($listener);
0 ignored issues
show
Unused Code introduced by
$class is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
77
78
            return "[invokable]#{class}";
79
        }
80
    }
81
82
    public function getEventName()
83
    {
84
        return $this->parent->getEventName();
85
    }
86
87
    public function getEvent()
88
    {
89
        return $this->parent->getEvent();
90
    }
91
92
    public function getListener()
93
    {
94
        return $this->listener;
95
    }
96
97
    public function getListenerName()
98
    {
99
        return $this->listenerName;
100
    }
101
102
    public function getWrappedError()
103
    {
104
        return $this->wrappedError;
105
    }
106
}