These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* |
||
6 | * This file is part of the Sonata Project package. |
||
7 | * |
||
8 | * (c) Thomas Rabaix <[email protected]> |
||
9 | * |
||
10 | * For the full copyright and license information, please view the LICENSE |
||
11 | * file that was distributed with this source code. |
||
12 | */ |
||
13 | |||
14 | namespace Sonata\NotificationBundle\Backend; |
||
15 | |||
16 | use Sonata\NotificationBundle\Consumer\ConsumerEvent; |
||
17 | use Sonata\NotificationBundle\Exception\HandlingException; |
||
18 | use Sonata\NotificationBundle\Model\Message; |
||
19 | use Sonata\NotificationBundle\Model\MessageInterface; |
||
20 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
||
21 | use ZendDiagnostics\Result\Success; |
||
22 | |||
23 | class RuntimeBackend implements BackendInterface |
||
24 | { |
||
25 | /** |
||
26 | * @var EventDispatcherInterface |
||
27 | */ |
||
28 | protected $dispatcher; |
||
29 | |||
30 | /** |
||
31 | * @param EventDispatcherInterface $dispatcher |
||
32 | */ |
||
33 | public function __construct(EventDispatcherInterface $dispatcher) |
||
34 | { |
||
35 | $this->dispatcher = $dispatcher; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * {@inheritdoc} |
||
40 | */ |
||
41 | public function publish(MessageInterface $message) |
||
42 | { |
||
43 | $this->handle($message, $this->dispatcher); |
||
44 | |||
45 | return $message; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * {@inheritdoc} |
||
50 | */ |
||
51 | public function create($type, array $body) |
||
52 | { |
||
53 | $message = new Message(); |
||
54 | $message->setType($type); |
||
55 | $message->setBody($body); |
||
56 | $message->setState(MessageInterface::STATE_OPEN); |
||
57 | |||
58 | return $message; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | public function createAndPublish($type, array $body) |
||
65 | { |
||
66 | return $this->publish($this->create($type, $body)); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | public function getIterator() |
||
73 | { |
||
74 | return new \EmptyIterator(); |
||
0 ignored issues
–
show
|
|||
75 | } |
||
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | public function initialize(): void |
||
81 | { |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | public function cleanup(): void |
||
88 | { |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | public function handle(MessageInterface $message, EventDispatcherInterface $dispatcher): void |
||
95 | { |
||
96 | $event = new ConsumerEvent($message); |
||
97 | |||
98 | try { |
||
99 | $dispatcher->dispatch($message->getType(), $event); |
||
100 | |||
101 | $message->setCompletedAt(new \DateTime()); |
||
102 | $message->setState(MessageInterface::STATE_DONE); |
||
103 | } catch (\Exception $e) { |
||
104 | $message->setCompletedAt(new \DateTime()); |
||
105 | $message->setState(MessageInterface::STATE_ERROR); |
||
106 | |||
107 | throw new HandlingException('Error while handling a message: '.$e->getMessage(), 0, $e); |
||
108 | } |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | */ |
||
114 | public function getStatus() |
||
115 | { |
||
116 | return new Success('Runtime backend health check', 'Ok (Runtime)'); |
||
117 | } |
||
118 | } |
||
119 |
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.