Skip to content

Commit 884d942

Browse files
committed
added Api::childFactory
This method exists only to help keeping BC for authentification when getting a child class. - If a Auth listener exists in parent, it will be passed to child on first instantiation. @todo: To be removed on next major version release.
1 parent bafaa67 commit 884d942

File tree

7 files changed

+48
-99
lines changed

7 files changed

+48
-99
lines changed

lib/Bitbucket/API/Api.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,37 @@ protected function processResponse(\Buzz\Message\MessageInterface $response)
263263
break;
264264
}
265265
}
266+
267+
/**
268+
* Factory for child classes
269+
*
270+
* NOTE: This exists only to keep BC. Do not rely on this factory because
271+
* it will be removed in a future version!
272+
*
273+
* @access protected
274+
* @param string $name
275+
* @return mixed
276+
*
277+
* @throws \InvalidArgumentException
278+
*/
279+
protected function childFactory($name)
280+
{
281+
if (empty($name)) {
282+
throw new \InvalidArgumentException('Not child specified.');
283+
}
284+
285+
/** @var Api $child */
286+
$class = '\\Bitbucket\\API\\'.$name;
287+
$child = new $class($this->client);
288+
289+
if ($this->getClient()->isListener('basicauth')) {
290+
$child->getClient()->addListener($this->getClient()->getListener('basicauth'));
291+
}
292+
293+
if ($this->getClient()->isListener('oauth')) {
294+
$child->getClient()->addListener($this->getClient()->getListener('oauth'));
295+
}
296+
297+
return $child;
298+
}
266299
}

lib/Bitbucket/API/Groups.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,6 @@ public function delete($account, $name)
111111
*/
112112
public function members()
113113
{
114-
$members = new Groups\Members( $this->client );
115-
116-
if ( !is_null($this->auth) ) {
117-
$members->setCredentials( $this->auth );
118-
}
119-
120-
return $members;
114+
return $this->childFactory('Groups\\Members');
121115
}
122116
}

lib/Bitbucket/API/Repositories/Changesets.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,6 @@ public function diff($account, $repo, $node)
100100
*/
101101
public function comments()
102102
{
103-
$comments = new Changesets\Comments( $this->client );
104-
105-
if ( !is_null($this->auth) ) {
106-
$comments->setCredentials( $this->auth );
107-
}
108-
109-
return $comments;
103+
return $this->childFactory('Repositories\\Changesets\\Comments');
110104
}
111105
}

lib/Bitbucket/API/Repositories/Issues.php

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,7 @@ public function delete($account, $repo, $issueID)
146146
*/
147147
public function comments()
148148
{
149-
$comments = new Repositories\Issues\Comments( $this->client );
150-
151-
if ( !is_null($this->auth)) {
152-
$comments->setCredentials( $this->auth );
153-
}
154-
155-
return $comments;
149+
return $this->childFactory('Repositories\\Issues\\Comments');
156150
}
157151

158152
/**
@@ -164,13 +158,7 @@ public function comments()
164158
*/
165159
public function components()
166160
{
167-
$components = new Repositories\Issues\Components( $this->client );
168-
169-
if ( !is_null($this->auth) ) {
170-
$components->setCredentials( $this->auth );
171-
}
172-
173-
return $components;
161+
return $this->childFactory('Repositories\\Issues\\Components');
174162
}
175163

176164
/**
@@ -182,13 +170,7 @@ public function components()
182170
*/
183171
public function versions()
184172
{
185-
$versions = new Repositories\Issues\Versions( $this->client );
186-
187-
if ( !is_null($this->auth) ) {
188-
$versions->setCredentials( $this->auth );
189-
}
190-
191-
return $versions;
173+
return $this->childFactory('Repositories\\Issues\\Versions');
192174
}
193175

194176
/**
@@ -200,12 +182,6 @@ public function versions()
200182
*/
201183
public function milestones()
202184
{
203-
$milestones = new Repositories\Issues\Milestones( $this->client );
204-
205-
if ( !is_null($this->auth) ) {
206-
$milestones->setCredentials( $this->auth );
207-
}
208-
209-
return $milestones;
185+
return $this->childFactory('Repositories\\Issues\\Milestones');
210186
}
211187
}

lib/Bitbucket/API/Repositories/PullRequests.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ class PullRequests extends API\Api
3131
*/
3232
public function comments()
3333
{
34-
$comments = new PullRequests\Comments( $this->client );
35-
36-
if ( !is_null($this->auth)) {
37-
$comments->setCredentials( $this->auth );
38-
}
39-
40-
return $comments;
34+
return $this->childFactory('Repositories\\PullRequests\\Comments');
4135
}
4236
}

lib/Bitbucket/API/User.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,6 @@ public function follows()
7676
*/
7777
public function repositories()
7878
{
79-
$repositories = new User\Repositories( $this->client );
80-
81-
if ( !is_null($this->auth) ) {
82-
$repositories->setCredentials( $this->auth );
83-
}
84-
85-
return $repositories;
79+
return $this->childFactory('User\\Repositories');
8680
}
8781
}

lib/Bitbucket/API/Users.php

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,7 @@ class Users extends Api
2929
*/
3030
public function account()
3131
{
32-
$account = new Users\Account( $this->client );
33-
34-
if ( !is_null($this->auth) ) {
35-
$account->setCredentials( $this->auth );
36-
}
37-
38-
return $account;
32+
return $this->childFactory('Users\\Account');
3933
}
4034

4135
/**
@@ -46,13 +40,7 @@ public function account()
4640
*/
4741
public function emails()
4842
{
49-
$emails = new Users\Emails( $this->client );
50-
51-
if ( !is_null($this->auth) ) {
52-
$emails->setCredentials( $this->auth );
53-
}
54-
55-
return $emails;
43+
return $this->childFactory('Users\\Emails');
5644
}
5745

5846
/**
@@ -63,13 +51,7 @@ public function emails()
6351
*/
6452
public function invitations()
6553
{
66-
$invitations = new Users\Invitations( $this->client );
67-
68-
if ( !is_null($this->auth) ) {
69-
$invitations->setCredentials( $this->auth );
70-
}
71-
72-
return $invitations;
54+
return $this->childFactory('Users\\Invitations');
7355
}
7456

7557
/**
@@ -80,13 +62,7 @@ public function invitations()
8062
*/
8163
public function oauth()
8264
{
83-
$oauth = new Users\OAuth( $this->client );
84-
85-
if ( !is_null($this->auth) ) {
86-
$oauth->setCredentials( $this->auth );
87-
}
88-
89-
return $oauth;
65+
return $this->childFactory('Users\\OAuth');
9066
}
9167

9268
/**
@@ -97,13 +73,7 @@ public function oauth()
9773
*/
9874
public function privileges()
9975
{
100-
$privileges = new Users\Privileges( $this->client );
101-
102-
if ( !is_null($this->auth) ) {
103-
$privileges->setCredentials( $this->auth );
104-
}
105-
106-
return $privileges;
76+
return $this->childFactory('Users\\Privileges');
10777
}
10878

10979
/**
@@ -114,12 +84,6 @@ public function privileges()
11484
*/
11585
public function sshKeys()
11686
{
117-
$keys = new Users\SshKeys( $this->client );
118-
119-
if ( !is_null($this->auth) ) {
120-
$keys->setCredentials( $this->auth );
121-
}
122-
123-
return $keys;
87+
return $this->childFactory('Users\\SshKeys');
12488
}
125-
}
89+
}

0 commit comments

Comments
 (0)