Completed
Push — master ( 2cff5f...81de03 )
by Michael
03:19
created

EveApiEventEmitterTrait::emitEvents()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 45
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 11
Bugs 0 Features 4
Metric Value
c 11
b 0
f 4
dl 0
loc 45
rs 8.439
cc 6
eloc 31
nc 8
nop 3
1
<?php
2
/**
3
 * Contains EveApiEventEmitterTrait Trait.
4
 *
5
 * PHP version 5.5
6
 *
7
 * LICENSE:
8
 * This file is part of Yet Another Php Eve Api Library also know as Yapeal
9
 * which can be used to access the Eve Online API data and place it into a
10
 * database.
11
 * Copyright (C) 2015-2016 Michael Cummings
12
 *
13
 * This program is free software: you can redistribute it and/or modify it
14
 * under the terms of the GNU Lesser General Public License as published by the
15
 * Free Software Foundation, either version 3 of the License, or (at your
16
 * option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful, but WITHOUT
19
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
21
 * for more details.
22
 *
23
 * You should have received a copy of the GNU Lesser General Public License
24
 * along with this program. If not, see
25
 * <http://www.gnu.org/licenses/>.
26
 *
27
 * You should be able to find a copy of this license in the LICENSE.md file. A
28
 * copy of the GNU GPL should also be available in the GNU-GPL.md file.
29
 *
30
 * @copyright 2015-2016 Michael Cummings
31
 * @license   http://www.gnu.org/copyleft/lesser.html GNU LGPL
32
 * @author    Michael Cummings <[email protected]>
33
 */
34
namespace Yapeal\Event;
35
36
use Yapeal\Log\Logger;
37
use Yapeal\Log\MessageBuilderTrait;
38
use Yapeal\Xml\EveApiReadWriteInterface;
39
40
/**
41
 * Trait EveApiEventEmitterTrait
42
 */
43
trait EveApiEventEmitterTrait
44
{
45
    use MessageBuilderTrait;
46
    /**
47
     * @param MediatorInterface $value
48
     *
49
     * @return self Fluent interface.
50
     */
51
    public function setYem(MediatorInterface $value)
52
    {
53
        $this->yem = $value;
54
        return $this;
55
    }
56
    /**
57
     * Emits a series of Eve API events and logs the handling of them.
58
     *
59
     * Events are emitted (triggered) from the most specific 'Prefix.Section.Api.Suffix' through to the least specific
60
     * 'Prefix.Suffix' until one of the events returns with hasBeenHandled() === true or there are no more events left
61
     * to emit.
62
     *
63
     * Log events are created for handled, sufficiently handled, and non-handled event.
64
     *
65
     * @param EveApiReadWriteInterface $data
66
     * @param string                   $eventSuffix
67
     * @param string                   $eventPrefix
68
     *
69
     * @return bool
70
     * @throws \LogicException
71
     */
72
    protected function emitEvents(EveApiReadWriteInterface $data, $eventSuffix, $eventPrefix = 'Yapeal.EveApi')
0 ignored issues
show
Coding Style introduced by
function emitEvents() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
73
    {
74
        $yem = $this->getYem();
75
        // Prefix.Section.Api.Suffix, Prefix.Api.Suffix,
76
        // Prefix.Section.Suffix, Prefix.Suffix
77
        /**
78
         * @var string[] $eventNames
79
         */
80
        $eventNames = explode(',',
81
            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',
82
                ucfirst($data->getEveApiSectionName()),
83
                $data->getEveApiName(),
84
                $eventPrefix,
85
                $eventSuffix));
86
        $event = null;
87
        /**
88
         * @var bool $sufficientlyHandled
89
         */
90
        $sufficientlyHandled = false;
91
        foreach ($eventNames as $eventName) {
92
            $yem->triggerLogEvent('Yapeal.Log.log', Logger::DEBUG, $this->getEmittingEventMessage($data, $eventName));
93
            $event = $yem->triggerEveApiEvent($eventName, $data);
94
            $data = $event->getData();
95
            if ($event->hasBeenHandled()) {
96
                $yem->triggerLogEvent('Yapeal.Log.log',
97
                    Logger::INFO,
98
                    $this->getWasHandledEventMessage($data, $eventName));
99
                $sufficientlyHandled = true;
100
                break;
101
            }
102
            if ($event->isSufficientlyHandled()) {
103
                $yem->triggerLogEvent('Yapeal.Log.log',
104
                    Logger::INFO,
105
                    $this->getSufficientlyHandledEventMessage($data, $eventName));
106
                $sufficientlyHandled = true;
107
            }
108
        }
109
        if (null === $event || false === $sufficientlyHandled) {
110
            $yem->triggerLogEvent('Yapeal.Log.log',
111
                Logger::NOTICE,
112
                $this->getNonHandledEventMessage($data, $eventSuffix));
113
            return false;
114
        }
115
        return true;
116
    }
117
    /**
118
     * @return MediatorInterface
119
     * @throws \LogicException
120
     */
121
    protected function getYem()
122
    {
123
        if (null === $this->yem || !$this->yem instanceof MediatorInterface) {
124
            $mess = 'Tried to use yem before it was set';
125
            throw new \LogicException($mess);
126
        }
127
        return $this->yem;
128
    }
129
    /**
130
     * @return bool
131
     */
132
    protected function hasYem()
133
    {
134
        return null !== $this->yem;
135
    }
136
    /**
137
     * @var MediatorInterface $yem
138
     */
139
    private $yem;
140
}
141