|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Contains EveApiEventEmitterTrait Trait. |
|
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 Yapeal\Log\Logger; |
|
38
|
|
|
use Yapeal\Log\MessageBuilderTrait; |
|
39
|
|
|
use Yapeal\Xml\EveApiReadWriteInterface; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Trait EveApiEventEmitterTrait |
|
43
|
|
|
*/ |
|
44
|
|
|
trait EveApiEventEmitterTrait |
|
45
|
|
|
{ |
|
46
|
|
|
use MessageBuilderTrait, YEMAwareTrait; |
|
47
|
|
|
/** |
|
48
|
|
|
* Emits a series of Eve API events and logs the handling of them. |
|
49
|
|
|
* |
|
50
|
|
|
* Events are emitted (triggered) from the most specific 'Prefix.Section.Api.Suffix' through to the least specific |
|
51
|
|
|
* 'Prefix.Suffix' until one of the events returns with hasBeenHandled() === true or there are no more events left |
|
52
|
|
|
* to emit. |
|
53
|
|
|
* |
|
54
|
|
|
* Log events are created for handled, sufficiently handled, and non-handled event. |
|
55
|
|
|
* |
|
56
|
|
|
* @param EveApiReadWriteInterface $data |
|
57
|
|
|
* @param string $eventSuffix |
|
58
|
|
|
* @param string $eventPrefix |
|
59
|
|
|
* |
|
60
|
|
|
* @return bool |
|
61
|
|
|
* @throws \LogicException |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function emitEvents(EveApiReadWriteInterface $data, string $eventSuffix, string $eventPrefix = 'Yapeal.EveApi'): bool |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
$yem = $this->getYem(); |
|
66
|
|
|
$eventNames = $this->getEmitterEvents($data, $eventSuffix, $eventPrefix); |
|
67
|
|
|
$event = null; |
|
68
|
|
|
/** |
|
69
|
|
|
* @var bool $sufficientlyHandled |
|
70
|
|
|
*/ |
|
71
|
|
|
$sufficientlyHandled = false; |
|
72
|
|
|
foreach ($eventNames as $eventName) { |
|
73
|
|
|
$yem->triggerLogEvent('Yapeal.Log.log', Logger::DEBUG, $this->getEmittingEventMessage($data, $eventName)); |
|
74
|
|
|
$event = $yem->triggerEveApiEvent($eventName, $data); |
|
75
|
|
|
$data = $event->getData(); |
|
76
|
|
|
if ($event->hasBeenHandled()) { |
|
77
|
|
|
$yem->triggerLogEvent('Yapeal.Log.log', |
|
78
|
|
|
Logger::INFO, |
|
79
|
|
|
$this->getWasHandledEventMessage($data, $eventName)); |
|
80
|
|
|
$sufficientlyHandled = true; |
|
81
|
|
|
break; |
|
82
|
|
|
} |
|
83
|
|
|
if ($event->isSufficientlyHandled()) { |
|
84
|
|
|
$yem->triggerLogEvent('Yapeal.Log.log', |
|
85
|
|
|
Logger::INFO, |
|
86
|
|
|
$this->getSufficientlyHandledEventMessage($data, $eventName)); |
|
87
|
|
|
$sufficientlyHandled = true; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
if (null === $event || false === $sufficientlyHandled) { |
|
91
|
|
|
$yem->triggerLogEvent('Yapeal.Log.log', |
|
92
|
|
|
Logger::NOTICE, |
|
93
|
|
|
$this->getNonHandledEventMessage($data, $eventSuffix)); |
|
94
|
|
|
return false; |
|
95
|
|
|
} |
|
96
|
|
|
return true; |
|
97
|
|
|
} |
|
98
|
|
|
/** |
|
99
|
|
|
* @param EveApiReadWriteInterface $data |
|
100
|
|
|
* @param string $eventSuffix |
|
101
|
|
|
* @param string $eventPrefix |
|
102
|
|
|
* |
|
103
|
|
|
* @return string[] |
|
104
|
|
|
* @throws \LogicException |
|
105
|
|
|
*/ |
|
106
|
|
|
private function getEmitterEvents(EveApiReadWriteInterface $data, string $eventSuffix, string $eventPrefix): array |
|
107
|
|
|
{ |
|
108
|
|
|
// Prefix.Section.Api.Suffix, Prefix.Api.Suffix, |
|
109
|
|
|
// Prefix.Section.Suffix, Prefix.Suffix |
|
110
|
|
|
/** |
|
111
|
|
|
* @var string[] $eventNames |
|
112
|
|
|
*/ |
|
113
|
|
|
$eventNames = explode(',', |
|
114
|
|
|
sprintf('%3$s.%1$s.%2$s.%4$s,%3$s.%2$s.%4$s,%3$s.%1$s.%4$s,%3$s.%4$s', |
|
115
|
|
|
ucfirst($data->getEveApiSectionName()), |
|
116
|
|
|
$data->getEveApiName(), |
|
117
|
|
|
$eventPrefix, |
|
118
|
|
|
$eventSuffix)); |
|
119
|
|
|
return $eventNames; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.