|
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 Laminas\Diagnostics\Result\Success; |
|
17
|
|
|
use Sonata\NotificationBundle\Consumer\ConsumerEvent; |
|
18
|
|
|
use Sonata\NotificationBundle\Exception\HandlingException; |
|
19
|
|
|
use Sonata\NotificationBundle\Model\Message; |
|
20
|
|
|
use Sonata\NotificationBundle\Model\MessageInterface; |
|
21
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
22
|
|
|
|
|
23
|
|
|
class RuntimeBackend implements BackendInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var EventDispatcherInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $dispatcher; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(EventDispatcherInterface $dispatcher) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->dispatcher = $dispatcher; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
|
|
public function publish(MessageInterface $message) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->handle($message, $this->dispatcher); |
|
41
|
|
|
|
|
42
|
|
|
return $message; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
|
|
public function create($type, array $body) |
|
49
|
|
|
{ |
|
50
|
|
|
$message = new Message(); |
|
51
|
|
|
$message->setType($type); |
|
52
|
|
|
$message->setBody($body); |
|
53
|
|
|
$message->setState(MessageInterface::STATE_OPEN); |
|
54
|
|
|
|
|
55
|
|
|
return $message; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
|
|
public function createAndPublish($type, array $body) |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->publish($this->create($type, $body)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getIterator() |
|
70
|
|
|
{ |
|
71
|
|
|
return new \EmptyIterator(); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritdoc} |
|
76
|
|
|
*/ |
|
77
|
|
|
public function initialize(): void |
|
78
|
|
|
{ |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
|
|
public function cleanup(): void |
|
85
|
|
|
{ |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritdoc} |
|
90
|
|
|
*/ |
|
91
|
|
|
public function handle(MessageInterface $message, EventDispatcherInterface $dispatcher): void |
|
92
|
|
|
{ |
|
93
|
|
|
$event = new ConsumerEvent($message); |
|
94
|
|
|
|
|
95
|
|
|
try { |
|
96
|
|
|
$dispatcher->dispatch($event, $message->getType()); |
|
97
|
|
|
|
|
98
|
|
|
$message->setCompletedAt(new \DateTime()); |
|
99
|
|
|
$message->setState(MessageInterface::STATE_DONE); |
|
100
|
|
|
} catch (\Exception $e) { |
|
101
|
|
|
$message->setCompletedAt(new \DateTime()); |
|
102
|
|
|
$message->setState(MessageInterface::STATE_ERROR); |
|
103
|
|
|
|
|
104
|
|
|
throw new HandlingException('Error while handling a message: '.$e->getMessage(), 0, $e); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* {@inheritdoc} |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getStatus() |
|
112
|
|
|
{ |
|
113
|
|
|
return new Success('Runtime backend health check', 'Ok (Runtime)'); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.