Skip to content

Commit e509d8d

Browse files
committed
Merge branch 'feature/split-httpClient' into develop
HTTP Client split The reason behind the split is that Http\Client class started to grow quite large. - All listener related methods have been moved in Http\Listener class - bug fix: Listener:delListener now *really* deletes the listener. - added tests for listener manipulation
2 parents 1267f99 + 093b91b commit e509d8d

File tree

3 files changed

+135
-76
lines changed

3 files changed

+135
-76
lines changed

lib/Bitbucket/API/Http/Client.php

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
/**
2323
* @author Alexandru G. <alex@gentle.ro>
2424
*/
25-
class Client implements ClientInterface
25+
class Client extends Listener implements ClientInterface
2626
{
2727
/**
2828
* @var array
@@ -53,11 +53,6 @@ class Client implements ClientInterface
5353
*/
5454
private $lastResponse;
5555

56-
/**
57-
* @var ListenerInterface[]
58-
*/
59-
protected $listeners = array();
60-
6156
/**
6257
* @var MessageInterface
6358
*/
@@ -77,59 +72,6 @@ public function __construct(array $options = array(), BuzzClientInterface $clien
7772
$this->client->setVerifyPeer($this->options['verify_peer']);
7873
}
7974

80-
/**
81-
* {@inheritDoc}
82-
*/
83-
public function addListener(ListenerInterface $listener, $priority = 0)
84-
{
85-
// Don't allow same listener with different priorities.
86-
if ($this->isListener($listener->getName())) {
87-
$this->delListener($listener->getName());
88-
}
89-
90-
$this->listeners[$priority][$listener->getName()] = $listener;
91-
92-
return $this;
93-
}
94-
95-
/**
96-
* {@inheritDoc}
97-
*/
98-
public function delListener($name)
99-
{
100-
if ($name instanceof ListenerInterface) {
101-
$name = $name->getName();
102-
}
103-
104-
if ($this->isListener($name) === true) {
105-
foreach ($this->listeners as $collection) {
106-
unset($collection[$name]);
107-
}
108-
}
109-
110-
return $this;
111-
}
112-
113-
/**
114-
* {@inheritDoc}
115-
*/
116-
public function getListener($name)
117-
{
118-
if (!$listener = $this->searchListener($name)) {
119-
throw new \InvalidArgumentException(sprintf('Unknown listener %s', $name));
120-
}
121-
122-
return $listener;
123-
}
124-
125-
/**
126-
* {@inheritDoc}
127-
*/
128-
public function isListener($name)
129-
{
130-
return ($this->searchListener($name) instanceof ListenerInterface);
131-
}
132-
13375
/**
13476
* {@inheritDoc}
13577
*/
@@ -376,20 +318,4 @@ function ($class) use ($when, $params) {
376318
}
377319
);
378320
}
379-
380-
/**
381-
* @access protected
382-
* @param string $name Listener name
383-
* @return ListenerInterface|bool false on error
384-
*/
385-
protected function searchListener($name)
386-
{
387-
foreach ($this->listeners as $collection) {
388-
if (isset($collection[$name])) {
389-
return $collection[$name];
390-
}
391-
}
392-
393-
return false;
394-
}
395321
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the bitbucket-api package.
5+
*
6+
* (c) Alexandru G. <alex@gentle.ro>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Bitbucket\API\Http;
13+
14+
use Bitbucket\API\Http\Listener\ListenerInterface;
15+
16+
/**
17+
* @author Alexandru G. <alex@gentle.ro>
18+
*/
19+
abstract class Listener
20+
{
21+
/**
22+
* @var ListenerInterface[]
23+
*/
24+
protected $listeners = array();
25+
26+
/**
27+
* {@inheritDoc}
28+
*/
29+
public function addListener(ListenerInterface $listener, $priority = 0)
30+
{
31+
// Don't allow same listener with different priorities.
32+
if ($this->isListener($listener->getName())) {
33+
$this->delListener($listener->getName());
34+
}
35+
36+
$this->listeners[$priority][$listener->getName()] = $listener;
37+
38+
return $this;
39+
}
40+
41+
/**
42+
* {@inheritDoc}
43+
*/
44+
public function delListener($name)
45+
{
46+
if ($name instanceof ListenerInterface) {
47+
$name = $name->getName();
48+
}
49+
50+
if ($this->isListener($name) === true) {
51+
foreach ($this->listeners as $prio => $collection) {
52+
unset($this->listeners[$prio][$name]);
53+
}
54+
}
55+
56+
return $this;
57+
}
58+
59+
/**
60+
* {@inheritDoc}
61+
*/
62+
public function getListener($name)
63+
{
64+
if (!$listener = $this->searchListener($name)) {
65+
throw new \InvalidArgumentException(sprintf('Unknown listener %s', $name));
66+
}
67+
68+
return $listener;
69+
}
70+
71+
/**
72+
* {@inheritDoc}
73+
*/
74+
public function isListener($name)
75+
{
76+
return ($this->searchListener($name) instanceof ListenerInterface);
77+
}
78+
79+
/**
80+
* @access protected
81+
* @param string $name Listener name
82+
* @return ListenerInterface|bool false on error
83+
*/
84+
protected function searchListener($name)
85+
{
86+
foreach ($this->listeners as $collection) {
87+
if (isset($collection[$name])) {
88+
return $collection[$name];
89+
}
90+
}
91+
92+
return false;
93+
}
94+
}

test/Bitbucket/Tests/API/Http/ClientTest.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,43 @@ public function testShouldDoPatchRequestAndReturnResponseInstance()
132132

133133
$this->assertInstanceOf('\Buzz\Message\MessageInterface', $response);
134134
}
135-
}
135+
136+
public function testAddListener()
137+
{
138+
$listener = $this->getListenerMock();
139+
140+
$this->client->addListener($listener, 1);
141+
$this->client->addListener($listener, 14);
142+
143+
$this->assertInstanceOf('Bitbucket\API\Http\Listener\ListenerInterface', $this->client->getListener('dummy'));
144+
}
145+
146+
public function testDeleteListener()
147+
{
148+
$listener = $this->getListenerMock('lorem');
149+
150+
$this->client->addListener($listener);
151+
$this->assertTrue($this->client->isListener('lorem'));
152+
153+
$this->client->delListener($listener);
154+
155+
$this->assertFalse($this->client->isListener('lorem'));
156+
}
157+
158+
/**
159+
* @expectedException \InvalidArgumentException
160+
*/
161+
public function testGetAbsentListener()
162+
{
163+
$this->client->getListener('invalid');
164+
}
165+
166+
private function getListenerMock($name = 'dummy')
167+
{
168+
$listener = $this->getMock('Bitbucket\API\Http\Listener\ListenerInterface');
169+
170+
$listener->expects($this->any())->method('getName')->will($this->returnValue($name));
171+
172+
return $listener;
173+
}
174+
}

0 commit comments

Comments
 (0)