Completed
Push — develop ( ddd2a2...c8e0b3 )
by Fabian
44:48 queued 38:21
created

DefaultImplementation   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 13

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
lcom 1
cbo 13
dl 0
loc 85
ccs 31
cts 31
cp 1
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getOptions() 0 4 1
A setOptions() 0 5 1
A getEventManager() 0 8 2
A setEventManager() 0 5 1
A register() 0 12 1
A registerListener() 0 10 1
1
<?php
2
3
/**
4
 * Copyright 2014 Fabian Grutschus. All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without modification,
7
 * are permitted provided that the following conditions are met:
8
 *
9
 * 1. Redistributions of source code must retain the above copyright notice, this
10
 *   list of conditions and the following disclaimer.
11
 *
12
 * 2. Redistributions in binary form must reproduce the above copyright notice,
13
 *   this list of conditions and the following disclaimer in the documentation
14
 *   and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 *
27
 * The views and conclusions contained in the software and documentation are those
28
 * of the authors and should not be interpreted as representing official policies,
29
 * either expressed or implied, of the copyright holders.
30
 *
31
 * @author    Fabian Grutschus <[email protected]>
32
 * @copyright 2014 Fabian Grutschus. All rights reserved.
33
 * @license   BSD
34
 * @link      http://github.com/fabiang/xmpp
35
 */
36
37
namespace Fabiang\Xmpp\Protocol;
38
39
use Fabiang\Xmpp\Options;
40
use Fabiang\Xmpp\EventListener\EventListenerInterface;
41
use Fabiang\Xmpp\Event\EventManagerInterface;
42
use Fabiang\Xmpp\Event\EventManager;
43
use Fabiang\Xmpp\EventListener\Stream\Stream;
44
use Fabiang\Xmpp\EventListener\Stream\StreamError;
45
use Fabiang\Xmpp\EventListener\Stream\StartTls;
46
use Fabiang\Xmpp\EventListener\Stream\Authentication;
47
use Fabiang\Xmpp\EventListener\Stream\Bind;
48
use Fabiang\Xmpp\EventListener\Stream\Session;
49
use Fabiang\Xmpp\EventListener\Stream\Roster as RosterListener;
50
use Fabiang\Xmpp\EventListener\Stream\Register as RegisterListener;
51
use Fabiang\Xmpp\EventListener\Stream\BlockedUsers as BlockedUsersListener;
52
53
/**
54
 * Default Protocol implementation.
55
 *
56
 * @package Xmpp\Protocol
57
 */
58
class DefaultImplementation implements ImplementationInterface
59
{
60
61
    /**
62
     * Options.
63
     *
64
     * @var Options
65
     */
66
    protected $options;
67
68
    /**
69
     * Eventmanager.
70
     *
71
     * @var EventManagerInterface
72
     */
73
    protected $events;
74
75
    /**
76
     * {@inheritDoc}
77
     */
78 3
    public function register()
79
    {
80 3
        $this->registerListener(new Stream);
81 3
        $this->registerListener(new StreamError);
82 3
        $this->registerListener(new StartTls);
83 3
        $this->registerListener(new Authentication);
84 3
        $this->registerListener(new Bind);
85 3
        $this->registerListener(new Session);
86 3
        $this->registerListener(new RosterListener);
87 3
        $this->registerListener(new RegisterListener);
88 3
        $this->registerListener(new BlockedUsersListener);
89 3
    }
90
91
    /**
92
     * {@inheritDoc}
93
     */
94 3
    public function registerListener(EventListenerInterface $eventListener)
95
    {
96 3
        $connection = $this->getOptions()->getConnection();
97
98 3
        $eventListener->setEventManager($this->getEventManager())
99 3
            ->setOptions($this->getOptions())
100 3
            ->attachEvents();
101
102 3
        $connection->addListener($eventListener);
103 3
    }
104
105
    /**
106
     * {@inheritDoc}
107
     */
108 3
    public function getOptions()
109
    {
110 3
        return $this->options;
111
    }
112
113
    /**
114
     * {@inheritDoc}
115
     */
116 3
    public function setOptions(Options $options)
117
    {
118 3
        $this->options = $options;
119 3
        return $this;
120
    }
121
122
    /**
123
     * {@inheritDoc}
124
     */
125 3
    public function getEventManager()
126
    {
127 3
        if (null === $this->events) {
128 3
            $this->setEventManager(new EventManager());
129 1
        }
130
131 3
        return $this->events;
132
    }
133
134
    /**
135
     * {@inheritDoc}
136
     */
137 3
    public function setEventManager(EventManagerInterface $events)
138
    {
139 3
        $this->events = $events;
140 3
        return $this;
141
    }
142
}
143