Completed
Push — master ( b659c5...c65770 )
by Michael
10:22
created

EveApiEventEmitterTrait::getEmitterEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 3
crap 1
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-2017 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-2017 Michael Cummings
32
 * @license   LGPL-3.0+
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 \DomainException
62
     * @throws \InvalidArgumentException
63
     * @throws \LogicException
64
     * @throws \UnexpectedValueException
65
     */
66 4
    protected function emitEvents(
67
        EveApiReadWriteInterface $data,
68
        string $eventSuffix,
69
        string $eventPrefix = 'Yapeal.EveApi'
70
    ): bool
71
    {
72 4
        $yem = $this->getYem();
73 4
        $eventNames = $this->getEmitterEvents($data, $eventSuffix, $eventPrefix);
74 4
        $event = null;
75
        /**
76
         * @var bool $sufficientlyHandled
77
         */
78 4
        $sufficientlyHandled = false;
79 4
        foreach ($eventNames as $eventName) {
80 4
            $yem->triggerLogEvent('Yapeal.Log.log', Logger::DEBUG, $this->getEmittingEventMessage($data, $eventName));
81 4
            $event = $yem->triggerEveApiEvent($eventName, $data);
82 4
            $data = $event->getData();
83 4
            if ($event->hasBeenHandled()) {
84
                $yem->triggerLogEvent('Yapeal.Log.log',
85
                    Logger::INFO,
86
                    $this->getWasHandledEventMessage($data, $eventName));
87
                $sufficientlyHandled = true;
88
                break;
89
            }
90 4
            if ($event->isSufficientlyHandled()) {
91
                $yem->triggerLogEvent('Yapeal.Log.log',
92
                    Logger::INFO,
93
                    $this->getSufficientlyHandledEventMessage($data, $eventName));
94 4
                $sufficientlyHandled = true;
95
            }
96
        }
97 4
        if (null === $event || false === $sufficientlyHandled) {
98 4
            $yem->triggerLogEvent('Yapeal.Log.log',
99 4
                Logger::NOTICE,
100 4
                $this->getNonHandledEventMessage($data, $eventSuffix));
101 4
            return false;
102
        }
103
        return true;
104
    }
105
    /**
106
     * @param EveApiReadWriteInterface $data
107
     * @param string                   $eventSuffix
108
     * @param string                   $eventPrefix
109
     *
110
     * @return array|string[]
111
     * @throws \LogicException
112
     */
113 4
    private function getEmitterEvents(EveApiReadWriteInterface $data, string $eventSuffix, string $eventPrefix): array
114
    {
115
        // Prefix.Section.Api.Suffix, Prefix.Api.Suffix,
116
        // Prefix.Section.Suffix, Prefix.Suffix
117
        /**
118
         * @var string[] $eventNames
119
         */
120 4
        $eventNames = explode(',',
121 4
            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',
122 4
                ucfirst($data->getEveApiSectionName()),
123 4
                $data->getEveApiName(),
124
                $eventPrefix,
125
                $eventSuffix));
126 4
        return $eventNames;
127
    }
128
}
129