Skip to content
This repository has been archived by the owner on Dec 30, 2020. It is now read-only.

Commit

Permalink
Merge pull request #252 from seregazhuk/develop
Browse files Browse the repository at this point in the history
Provider refactoring
  • Loading branch information
seregazhuk authored Mar 18, 2017
2 parents 71ed185 + 87868c5 commit f44927c
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 54 deletions.
20 changes: 12 additions & 8 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@
<whitelist>
<directory suffix=".php">./src/</directory>
<exclude>
<file>/src/Api/Providers/Topics.php</file>
<file>/src/Api/Providers/Boards.php</file>
<file>/src/Api/Providers/Comments.php</file>
<file>/src/Api/Providers/Inbox.php</file>
<file>/src/Api/Providers/Pins.php</file>
<file>/src/Api/Providers/Pinners.php</file>
<file>/src/Api/Providers/Interests.php</file>
<file>/src/Api/Providers/User.php</file>
<file>./src/Api/Providers/Core/EntityProvider.php</file>
<file>./src/Api/Providers/Topics.php</file>
<file>./src/Api/Providers/Boards.php</file>
<file>./src/Api/Providers/Comments.php</file>
<file>./src/Api/Providers/Inbox.php</file>
<file>./src/Api/Providers/Pins.php</file>
<file>./src/Api/Providers/Pinners.php</file>
<file>./src/Api/Providers/Password.php</file>
<file>./src/Api/Providers/Interests.php</file>
<file>./src/Api/Providers/User.php</file>
<file>./src/Api/Providers/Auth.php</file>
<file>./src/Api/Providers/Keywords.php</file>
</exclude>
</whitelist>
</filter>
Expand Down
4 changes: 2 additions & 2 deletions src/Api/Providers/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ protected function processLogin($username, $password)
'password' => $password,
];

$response = $this->post($credentials, UrlBuilder::RESOURCE_LOGIN, true);
$this->post($credentials, UrlBuilder::RESOURCE_LOGIN);

if ($response->hasErrors()) return false;
if ($this->response->hasErrors()) return false;

$this->request->login();

Expand Down
17 changes: 9 additions & 8 deletions src/Api/Providers/Core/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,16 @@ public function __construct(Request $request, Response $response)
*
* @param array $requestOptions
* @param string $resourceUrl
* @param bool $returnResponse
*
* @return Response|bool
*/
protected function post($requestOptions, $resourceUrl, $returnResponse = false)
protected function post($requestOptions, $resourceUrl)
{
$postString = Request::createQuery($requestOptions);

$this->execute($resourceUrl, $postString);

return $returnResponse ? $this->response : $this->response->isOk();
return $this->response->isOk();

}

Expand All @@ -64,10 +63,9 @@ protected function post($requestOptions, $resourceUrl, $returnResponse = false)
*
* @param array $requestOptions
* @param string $resourceUrl
* @param bool $returnResponse
* @return array|bool|Response
*/
protected function get(array $requestOptions = [], $resourceUrl = '', $returnResponse = false)
protected function get(array $requestOptions = [], $resourceUrl = '')
{
$query = Request::createQuery(
$requestOptions,
Expand All @@ -76,7 +74,7 @@ protected function get(array $requestOptions = [], $resourceUrl = '', $returnRes

$this->execute($resourceUrl . '?' . $query);

return $returnResponse ? $this->response : $this->response->getResponseData();
return $this->response->getResponseData();

}

Expand Down Expand Up @@ -121,8 +119,11 @@ public function isLoggedIn()
*/
protected function paginate($data, $resourceUrl, $limit = Pagination::DEFAULT_LIMIT)
{
return $this->paginateCustom(function() use ($data, $resourceUrl) {
return $this->get($data, $resourceUrl, true);
return $this
->paginateCustom(function () use ($data, $resourceUrl) {
$this->get($data, $resourceUrl);

return $this->response;
})->take($limit);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Api/Providers/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function reset($link, $newPassword)
'expiration' => $query['e'],
];

return $this->post($request, UrlBuilder::RESOURCE_RESET_PASSWORD_UPDATE, true);
return $this->post($request, UrlBuilder::RESOURCE_RESET_PASSWORD_UPDATE);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Api/Providers/Pins.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ public function create($imageUrl, $boardId, $description = '', $link = '')
'board_id' => $boardId,
];

return $this
->post($requestOptions, UrlBuilder::RESOURCE_CREATE_PIN, true)
->getResponseData();
$this->post($requestOptions, UrlBuilder::RESOURCE_CREATE_PIN);

return $this->response->getResponseData();
}

/**
Expand Down Expand Up @@ -140,9 +140,9 @@ public function repin($repinId, $boardId, $description = '')
'pin_id' => $repinId,
];

return $this
->post($requestOptions, UrlBuilder::RESOURCE_REPIN, true)
->getResponseData();
$this->post($requestOptions, UrlBuilder::RESOURCE_REPIN);

return $this->response->getResponseData();
}

/**
Expand Down
8 changes: 3 additions & 5 deletions src/Api/Traits/HandlesRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,17 @@ trait HandlesRequest
*
* @param array $requestOptions
* @param string $resourceUrl
* @param bool $returnResponse
*
* @return Response|bool
*/
abstract protected function post($requestOptions, $resourceUrl, $returnResponse = false);
abstract protected function post($requestOptions, $resourceUrl);

/**
* Executes a GET request to Pinterest API.
*
* @param array $requestOptions
* @param string $resourceUrl
* @param bool $returnResponse
* @return array|bool|Response
*/
abstract protected function get(array $requestOptions = [], $resourceUrl = '', $returnResponse = false);
}
abstract protected function get(array $requestOptions = [], $resourceUrl = '');
}
2 changes: 1 addition & 1 deletion src/Api/Traits/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected function execSearchRequest($query, $scope)
*/
public function search($query, $limit = Pagination::DEFAULT_LIMIT)
{
return $this->paginateCustom(function() use ($query) {
return $this->paginateCustom(function () use ($query) {
return $this->execSearchRequest($query, $this->getSearchScope());
})->take($limit);
}
Expand Down
29 changes: 6 additions & 23 deletions tests/Bot/ProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,6 @@ public function it_should_clear_response_before_pagination()
$provider->dummyPaginate(['test' => 'test'], 'http://example.com')->toArray();
}

/** @test */
public function it_should_return_response_object_if_required_for_get_request()
{
$provider = $this->makeProvider([]);

$this->assertInstanceOf(Response::class, $provider->dummyGet(true));
}

/** @test */
public function it_should_return_response_object_if_required_for_post_request()
{
$provider = $this->makeProvider([]);

$this->assertInstanceOf(Response::class, $provider->dummyPost(true));
}

/** @test */
public function it_should_return_bool_if_required_for_post_request()
Expand All @@ -90,7 +75,7 @@ public function it_should_return_bool_if_required_for_post_request()

$provider = $this->makeProvider($response);

$this->assertTrue($provider->dummyPost(false));
$this->assertTrue($provider->dummyPost());
}

/**
Expand Down Expand Up @@ -134,20 +119,18 @@ public function dummyPaginate($data, $resourceUrl)
}

/**
* @param bool $returnResponse
* @return array|bool|Response
*/
public function dummyGet($returnResponse)
public function dummyGet()
{
return $this->get([], '', $returnResponse);
return $this->get([], '');
}

/**
* @param bool $returnResponse
* @return array|bool|Response
*/
public function dummyPost($returnResponse)
public function dummyPost()
{
return $this->post([], '', $returnResponse);
return $this->post([], '');
}
}
}

0 comments on commit f44927c

Please sign in to comment.