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\EventListener\Stream; |
38
|
|
|
|
39
|
|
|
use Fabiang\Xmpp\Event\XMLEvent; |
40
|
|
|
use Fabiang\Xmpp\EventListener\AbstractEventListener; |
41
|
|
|
use Fabiang\Xmpp\EventListener\BlockingEventListenerInterface; |
42
|
|
|
use Fabiang\Xmpp\Protocol\User\User; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Listener |
46
|
|
|
* |
47
|
|
|
* @package Xmpp\EventListener |
48
|
|
|
*/ |
49
|
|
|
class BlockedUsers extends AbstractEventListener implements BlockingEventListenerInterface |
50
|
|
|
{ |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Blocking. |
54
|
|
|
* |
55
|
|
|
* @var boolean |
56
|
|
|
*/ |
57
|
|
|
protected $blocking = false; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* user object. |
61
|
|
|
* |
62
|
|
|
* @var User |
63
|
|
|
*/ |
64
|
|
|
protected $userObject; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritDoc} |
68
|
|
|
*/ |
69
|
|
View Code Duplication |
public function attachEvents() |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$this->getOutputEventManager() |
72
|
|
|
->attach('{urn:xmpp:blocking}blocklist', [$this, 'query']); |
73
|
|
|
$this->getInputEventManager() |
74
|
|
|
->attach('{urn:xmpp:blocking}blocklist', [$this, 'result']); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Sending a query request for roster sets listener to blocking mode. |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
|
public function query() |
83
|
|
|
{ |
84
|
|
|
$this->blocking = true; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Result received. |
89
|
|
|
* |
90
|
|
|
* @param \Fabiang\Xmpp\Event\XMLEvent $event |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
|
|
public function result(XMLEvent $event) |
94
|
|
|
{ |
95
|
|
|
if ($event->isEndTag()) { |
96
|
|
|
$users = []; |
97
|
|
|
|
98
|
|
|
/* @var $element \DOMElement */ |
99
|
|
|
$element = $event->getParameter(0); |
100
|
|
|
$items = $element->getElementsByTagName('item'); |
101
|
|
|
/* @var $item \DOMElement */ |
102
|
|
|
foreach ($items as $item) { |
103
|
|
|
$users[] = $item->getAttribute('jid'); |
104
|
|
|
} |
105
|
|
|
dd($users); |
106
|
|
|
//$this->getOptions()->setUsers($users); |
107
|
|
|
$this->blocking = false; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get user object. |
113
|
|
|
* |
114
|
|
|
* @return User |
115
|
|
|
*/ |
116
|
|
|
public function getUserObject() |
117
|
|
|
{ |
118
|
|
|
if (null === $this->userObject) { |
119
|
|
|
$this->setUserObject(new User); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this->userObject; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Set user object. |
127
|
|
|
* |
128
|
|
|
* @param User $userObject |
129
|
|
|
* @return $this |
130
|
|
|
*/ |
131
|
|
|
public function setUserObject(User $userObject) |
132
|
|
|
{ |
133
|
|
|
$this->userObject = $userObject; |
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritDoc} |
139
|
|
|
*/ |
140
|
|
|
public function isBlocking() |
141
|
|
|
{ |
142
|
|
|
return $this->blocking; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.