AggregateRootEventException::getEvent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Workana\AsyncJobs\EventDispatching;
3
4
use Exception;
5
use Symfony\Component\EventDispatcher\Event;
6
use Workana\AsyncJobs\Exception\AggregateException;
7
8
/**
9
 * Root of event exception aggregate
10
 *
11
 * @author Carlos Frutos <[email protected]>
12
 */
13
class AggregateRootEventException extends Exception implements AggregateException
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $eventName;
19
20
    /**
21
     * @var Event
22
     */
23
    protected $event;
24
25
    /**
26
     * @var ChildEventException[]
27
     */
28
    protected $children = [];
29
30
    /**
31
     * Creates a new aggregate exception
32
     * 
33
     * @param string $eventName
34
     * @param Event $event
35
     * @param array $failedListeners Array of arrays in form [$listener, $error]
36
     */
37
    public function __construct($eventName, Event $event, array $failedListeners)
38
    {
39
        $this->eventName = (string) $eventName;
40
        $this->event = $event;
41
42
        foreach ($failedListeners as $failedListenerData) {
43
            list($listener, $error) = $failedListenerData;
44
45
            $this->children[] = new AggregateChildEventException($this, $listener, $error);
46
        }
47
48
        parent::__construct(strtr('Failed event :eventName on :count listeners', [
49
            ':eventName' => $this->eventName,
50
            ':count' => count($this->children)
51
        ]));
52
    }
53
54
    /**
55
     * Associated event name
56
     *
57
     * @return string
58
     */
59
    public function getEventName()
60
    {
61
        return $this->eventName;
62
    }
63
64
    /**
65
     * Associated event
66
     *
67
     * @return Event
68
     */
69
    public function getEvent()
70
    {
71
        return $this->event;
72
    }
73
74
    /**
75
     * Children exceptions
76
     *
77
     * @return AggregateChildEventException
78
     */
79
    public function getChildren()
80
    {
81
        return $this->children;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->children; (Workana\AsyncJobs\EventD...g\ChildEventException[]) is incompatible with the return type declared by the interface Workana\AsyncJobs\Except...eException::getChildren of type Workana\AsyncJobs\Exception\Exception[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
82
    }
83
}