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

Mediator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 9
c 3
b 0
f 2
lcom 1
cbo 6
dl 0
loc 104
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setServiceContainer() 0 12 3
A triggerEveApiEvent() 0 11 2
A triggerLogEvent() 0 15 2
A getServiceByName() 0 4 1
1
<?php
2
/**
3
 * Contains Mediator class.
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 EventMediator\AbstractContainerMediator;
37
use Yapeal\Container\ContainerInterface;
38
use Yapeal\Container\PimpleContainer;
39
use Yapeal\Log\Logger;
40
use Yapeal\Xml\EveApiReadWriteInterface;
41
42
/** @noinspection LongInheritanceChainInspection */
43
/**
44
 * Class Mediator
45
 */
46
class Mediator extends AbstractContainerMediator implements MediatorInterface
47
{
48
    /**
49
     * @param ContainerInterface|null $serviceContainer
50
     *
51
     * @throws \InvalidArgumentException
52
     * @throws \RuntimeException
53
     */
54
    public function __construct(ContainerInterface $serviceContainer = null)
55
    {
56
        $this->setServiceContainer($serviceContainer);
57
    }
58
    /**
59
     * This is used to bring in the service container that will be used.
60
     *
61
     * Though not required it would be considered best practice for this method
62
     * to create a new instance of the container when given null. Another good
63
     * practice is to call this method from the class constructor to allow
64
     * easier testing.
65
     *
66
     * @param ContainerInterface|null $value
67
     *
68
     * @return $this Fluent interface.
69
     * @throws \InvalidArgumentException
70
     * @throws \RuntimeException
71
     *
72
     * @link http://pimple.sensiolabs.org/ Pimple
73
     */
74
    public function setServiceContainer($value = null)
75
    {
76
        if (null === $value) {
77
            $value = new PimpleContainer();
78
        }
79
        if (!$value instanceof ContainerInterface) {
80
            $mess = sprintf('Must be an instance of ContainerInterface but was given %s',
81
                gettype($value));
82
            throw new \InvalidArgumentException($mess);
83
        }
84
        $this->serviceContainer = $value;
85
    }
86
    /**
87
     * @param string                   $eventName
0 ignored issues
show
Documentation introduced by
Should the type for parameter $event not be null|EveApiEventInterface?

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.

Loading history...
88
     * @param EveApiReadWriteInterface $data
89
     * @param EveApiEventInterface     $event
90
     *
91
     * @return EveApiEventInterface
92
     * @throws \DomainException
93
     * @throws \InvalidArgumentException
94
     */
95
    public function triggerEveApiEvent(
96
        $eventName,
97
        EveApiReadWriteInterface $data,
98
        EveApiEventInterface $event = null
99
    ) {
100
        if (null === $event) {
101
            $event = new EveApiEvent();
102
        }
103
        $event->setData($data);
104
        return $this->trigger($eventName, $event);
105
    }
106
    /** @noinspection MoreThanThreeArgumentsInspection */
107
    /**
108
     * @param string            $eventName
0 ignored issues
show
Documentation introduced by
Should the type for parameter $event not be null|LogEventInterface?

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.

Loading history...
109
     * @param mixed             $level
110
     * @param string            $message
111
     * @param array             $context
112
     * @param LogEventInterface $event
113
     *
114
     * @return LogEventInterface
115
     * @throws \DomainException
116
     * @throws \InvalidArgumentException
117
     */
118
    public function triggerLogEvent(
119
        $eventName,
120
        $level = Logger::DEBUG,
121
        $message = '',
122
        array $context = [],
123
        LogEventInterface $event = null
124
    ) {
125
        if (null === $event) {
126
            $event = new LogEvent();
127
        }
128
        $event->setLevel($level)
129
            ->setMessage($message)
130
            ->setContext($context);
131
        return $this->trigger($eventName, $event);
132
    }
133
    /**
134
     * This method is used any time the mediator need to get the actual instance
135
     * of the class for an event.
136
     *
137
     * Normal will only be called during actual trigger of an event since lazy
138
     * loading is used.
139
     *
140
     * @param string $serviceName
141
     *
142
     * @return array
143
     * @throws \LogicException
144
     */
145
    protected function getServiceByName($serviceName)
146
    {
147
        return $this->getServiceContainer()[$serviceName];
148
    }
149
}
150