1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Contains Mediator class. |
5
|
|
|
* |
6
|
|
|
* PHP version 7.0+ |
7
|
|
|
* |
8
|
|
|
* LICENSE: |
9
|
|
|
* This file is part of Yet Another Php Eve Api Library also know as Yapeal |
10
|
|
|
* which can be used to access the Eve Online API data and place it into a |
11
|
|
|
* database. |
12
|
|
|
* Copyright (C) 2015-2016 Michael Cummings |
13
|
|
|
* |
14
|
|
|
* This program is free software: you can redistribute it and/or modify it |
15
|
|
|
* under the terms of the GNU Lesser General Public License as published by the |
16
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your |
17
|
|
|
* option) any later version. |
18
|
|
|
* |
19
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT |
20
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
22
|
|
|
* for more details. |
23
|
|
|
* |
24
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
25
|
|
|
* along with this program. If not, see |
26
|
|
|
* <http://spdx.org/licenses/LGPL-3.0.html>. |
27
|
|
|
* |
28
|
|
|
* You should be able to find a copy of this license in the COPYING-LESSER.md |
29
|
|
|
* file. A copy of the GNU GPL should also be available in the COPYING.md file. |
30
|
|
|
* |
31
|
|
|
* @copyright 2015-2016 Michael Cummings |
32
|
|
|
* @license http://www.gnu.org/copyleft/lesser.html GNU LGPL |
33
|
|
|
* @author Michael Cummings <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
namespace Yapeal\Event; |
36
|
|
|
|
37
|
|
|
use EventMediator\AbstractContainerMediator; |
38
|
|
|
use EventMediator\ContainerMediatorInterface; |
39
|
|
|
use EventMediator\EventInterface; |
40
|
|
|
use Yapeal\Container\Container; |
41
|
|
|
use Yapeal\Container\ContainerInterface; |
42
|
|
|
use Yapeal\Log\Logger; |
43
|
|
|
use Yapeal\Xml\EveApiReadWriteInterface; |
44
|
|
|
|
45
|
|
|
/** @noinspection LongInheritanceChainInspection */ |
46
|
|
|
/** |
47
|
|
|
* Class Mediator |
48
|
|
|
*/ |
49
|
|
|
class Mediator extends AbstractContainerMediator implements MediatorInterface |
50
|
|
|
{ |
51
|
|
|
/** |
52
|
|
|
* @param ContainerInterface|null $serviceContainer |
53
|
|
|
* |
54
|
|
|
* @throws \InvalidArgumentException |
55
|
|
|
* @throws \RuntimeException |
56
|
|
|
*/ |
57
|
|
|
public function __construct(ContainerInterface $serviceContainer = null) |
58
|
|
|
{ |
59
|
|
|
$this->setServiceContainer($serviceContainer); |
60
|
|
|
} |
61
|
|
|
/** |
62
|
|
|
* This is used to bring in the service container that will be used. |
63
|
|
|
* |
64
|
|
|
* Though not required it would be considered best practice for this method |
65
|
|
|
* to create a new instance of the container when given null. Another good |
66
|
|
|
* practice is to call this method from the class constructor to allow |
67
|
|
|
* easier testing. |
68
|
|
|
* |
69
|
|
|
* @param ContainerInterface|null $value |
70
|
|
|
* |
71
|
|
|
* @return ContainerMediatorInterface Fluent interface. |
72
|
|
|
* @throws \InvalidArgumentException |
73
|
|
|
* @throws \RuntimeException |
74
|
|
|
* |
75
|
|
|
* @link http://pimple.sensiolabs.org/ Pimple |
76
|
|
|
*/ |
77
|
|
|
public function setServiceContainer($value = null): ContainerMediatorInterface |
78
|
|
|
{ |
79
|
|
|
if (null === $value) { |
80
|
|
|
$value = new Container(); |
81
|
|
|
} |
82
|
|
|
if (!$value instanceof ContainerInterface) { |
83
|
|
|
$mess = sprintf('Must be an instance of ContainerInterface but was given %s', |
84
|
|
|
gettype($value)); |
85
|
|
|
throw new \InvalidArgumentException($mess); |
86
|
|
|
} |
87
|
|
|
$this->serviceContainer = $value; |
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
/** |
91
|
|
|
* @param string $eventName |
|
|
|
|
92
|
|
|
* @param EveApiReadWriteInterface $data |
93
|
|
|
* @param EveApiEventInterface $event |
94
|
|
|
* |
95
|
|
|
* @return EventInterface|EveApiEventInterface |
96
|
|
|
* @throws \DomainException |
97
|
|
|
* @throws \InvalidArgumentException |
98
|
|
|
*/ |
99
|
|
|
public function triggerEveApiEvent( |
100
|
|
|
$eventName, |
101
|
|
|
EveApiReadWriteInterface $data, |
102
|
|
|
EveApiEventInterface $event = null |
103
|
|
|
): EventInterface { |
104
|
|
|
if (null === $event) { |
105
|
|
|
$event = new EveApiEvent(); |
106
|
|
|
} |
107
|
|
|
$event->setData($data); |
108
|
|
|
return $this->trigger($eventName, $event); |
109
|
|
|
} |
110
|
|
|
/** @noinspection MoreThanThreeArgumentsInspection */ |
111
|
|
|
/** |
112
|
|
|
* @param string $eventName |
|
|
|
|
113
|
|
|
* @param mixed $level |
114
|
|
|
* @param string $message |
115
|
|
|
* @param array $context |
116
|
|
|
* @param LogEventInterface $event |
117
|
|
|
* |
118
|
|
|
* @return EventInterface |
119
|
|
|
* @throws \DomainException |
120
|
|
|
* @throws \InvalidArgumentException |
121
|
|
|
*/ |
122
|
|
|
public function triggerLogEvent( |
123
|
|
|
$eventName, |
124
|
|
|
$level = Logger::DEBUG, |
125
|
|
|
$message = '', |
126
|
|
|
array $context = [], |
127
|
|
|
LogEventInterface $event = null |
128
|
|
|
): EventInterface { |
129
|
|
|
if (null === $event) { |
130
|
|
|
$event = new LogEvent(); |
131
|
|
|
} |
132
|
|
|
$event->setLevel($level) |
133
|
|
|
->setMessage($message) |
134
|
|
|
->setContext($context); |
135
|
|
|
return $this->trigger($eventName, $event); |
136
|
|
|
} |
137
|
|
|
/** @noinspection GenericObjectTypeUsageInspection */ |
138
|
|
|
/** |
139
|
|
|
* This method is used any time the mediator need to get the actual instance |
140
|
|
|
* of the class for an event. |
141
|
|
|
* |
142
|
|
|
* Normal will only be called during actual trigger of an event since lazy |
143
|
|
|
* loading is used. |
144
|
|
|
* |
145
|
|
|
* @param string $serviceName |
146
|
|
|
* |
147
|
|
|
* @return object |
148
|
|
|
* @throws \LogicException |
149
|
|
|
*/ |
150
|
|
|
public function getServiceByName(string $serviceName) |
151
|
|
|
{ |
152
|
|
|
return $this->getServiceContainer()[$serviceName]; |
153
|
|
|
} |
154
|
|
|
/** |
155
|
|
|
* Used to get the service container. |
156
|
|
|
* |
157
|
|
|
* @return ContainerInterface|null |
158
|
|
|
*/ |
159
|
|
|
private function getServiceContainer() |
160
|
|
|
{ |
161
|
|
|
return $this->serviceContainer; |
162
|
|
|
} |
163
|
|
|
/** |
164
|
|
|
* Holds the container instance to be used. |
165
|
|
|
* |
166
|
|
|
* @var ContainerInterface|null $serviceContainer |
167
|
|
|
*/ |
168
|
|
|
private $serviceContainer; |
169
|
|
|
} |
170
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.