From 2a107a2a5c41381b43c0237e5004a0b41f9c4b5b Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Tue, 27 Dec 2016 15:29:42 +1100 Subject: [PATCH 01/33] Add @method documentation for app container --- app/Support/Container.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/Support/Container.php b/app/Support/Container.php index 2028a0e..c34272b 100644 --- a/app/Support/Container.php +++ b/app/Support/Container.php @@ -4,6 +4,7 @@ use App\Exceptions\InvalidCallException; use League\Container\Container as DiContainer; +use League\Container\Definition\DefinitionInterface; use League\Container\ReflectionContainer; use League\Route\RouteCollection; use Psr\Http\Message\ResponseInterface; @@ -17,6 +18,10 @@ * Registers services and routes request * * @package App\Support + * @method mixed get(string $alias, array $args) + * @method bool has(string $alias) + * @method DefinitionInterface add(string $alias, mixed|null $concrete, boolean $share) + * @method DefinitionInterface share(string $alias, mixed|null $conrete) */ class Container { From 691a831fb20fdd99653cea7b7b5ea3b1fdf29b9b Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Tue, 27 Dec 2016 15:43:40 +1100 Subject: [PATCH 02/33] Create LastFm factory + unit tests --- app/Services/LastFm/Client.php | 47 +++++++++++++++++++++++---- app/Services/LastFm/Factory.php | 24 ++++++++++++++ tests/Services/LastFm/FactoryTest.php | 24 ++++++++++++++ 3 files changed, 88 insertions(+), 7 deletions(-) create mode 100644 app/Services/LastFm/Factory.php create mode 100644 tests/Services/LastFm/FactoryTest.php diff --git a/app/Services/LastFm/Client.php b/app/Services/LastFm/Client.php index 05c61e9..3ae5c6a 100644 --- a/app/Services/LastFm/Client.php +++ b/app/Services/LastFm/Client.php @@ -14,8 +14,8 @@ * * @package App\Services\LastFm * @property Artist artist - * @property Geo geo - * @todo create unique Exception class for these errors + * @property Geo geo + * @todo create unique Exception class for these errors */ class Client { @@ -26,6 +26,7 @@ class Client //properties start private $apiKey; private $apiSecret; + private $apiOptions = []; /** * @var ClientInterface @@ -38,11 +39,41 @@ class Client * * @param string $key * @param string $secret + * @param array $options */ - public function __construct($key, $secret) + public function __construct($key, $secret, array $options = []) { - $this->apiKey = $key; - $this->apiSecret = $secret; + $this->apiKey = $key; + $this->apiSecret = $secret; + $this->apiOptions = $options; + } + + /** + * Get the api key + * + * @return string + */ + public function getApiKey() + { + return $this->apiKey; + } + + /** + * Get the api secret + * + * @return string + */ + public function getApiSecret() + { + return $this->apiSecret; + } + + /** + * @return array + */ + public function getOptions(): array + { + return $this->apiOptions; } /** @@ -129,9 +160,9 @@ public function request($methodName, array $params = []): array private function buildParams($methodName, array $params = []): array { return array_merge([ - 'method' => $methodName, + 'method' => $methodName, 'api_key' => $this->apiKey, - 'format' => self::RESPONSE_FORMAT, + 'format' => self::RESPONSE_FORMAT, ], $params); } @@ -154,6 +185,8 @@ public function getHttpClient(): ClientInterface } /** + * Inject the required http client + * * @param ClientInterface $httpClient */ public function setHttpClient(ClientInterface $httpClient) diff --git a/app/Services/LastFm/Factory.php b/app/Services/LastFm/Factory.php new file mode 100644 index 0000000..27916ec --- /dev/null +++ b/app/Services/LastFm/Factory.php @@ -0,0 +1,24 @@ + '12345', + 'api_secret' => 'secret', + 'pagination' => ['limit' => 100] + ] + ); + $this->assertSame('12345', $client->getApiKey()); + $this->assertArrayHasKey('pagination', $client->getOptions()); + } +} From 4a9e5d67e85dfdca8eaf8409b4f877e9327adc40 Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Tue, 27 Dec 2016 15:43:54 +1100 Subject: [PATCH 03/33] Update application container to use lastfm factory --- bootstrap/app.php | 4 ++-- config/lastfm.php | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/bootstrap/app.php b/bootstrap/app.php index 3b090f5..d060979 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -32,8 +32,8 @@ //endregion //region general application services -$app->add('lastfm', function () { - return new \App\Services\LastFm\Client(getenv('LASTFM_KEY'), getenv('LASTFM_SECRET')); +$app->add('lastfm', function () use ($app) { + return \App\Services\LastFm\Factory::fromConfig($app->get('config')->get('lastfm')); }); //endregion diff --git a/config/lastfm.php b/config/lastfm.php index ddcb7a4..ba2681c 100644 --- a/config/lastfm.php +++ b/config/lastfm.php @@ -3,5 +3,7 @@ return [ 'pagination' => [ 'limit' => getenv('LASTFM_PAGINATION_LIMIT'), - ] + ], + 'api_key' => getenv('LASTFM_KEY'), + 'api_secret' => getenv('LASTFM_SECRET'), ]; \ No newline at end of file From db4d6b7a16ce10d6f3e0e88dd42f7ca9c09c7922 Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Tue, 27 Dec 2016 16:37:29 +1100 Subject: [PATCH 04/33] Add docblock for abstract controller --- app/Http/Controllers/AbstractController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Http/Controllers/AbstractController.php b/app/Http/Controllers/AbstractController.php index 77a961b..91cff01 100644 --- a/app/Http/Controllers/AbstractController.php +++ b/app/Http/Controllers/AbstractController.php @@ -64,6 +64,7 @@ protected function hasRequestParam($key) * @param string $method * @param array $params * @return string|array|ResponseInterface + * @todo Invoke action through container for DI in controller::action*() */ public static function __callStatic($method, $params) { From e850fea5bdd5ec0246ce591b956362bd0033f9f9 Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Tue, 27 Dec 2016 16:39:17 +1100 Subject: [PATCH 05/33] Add getOption method to LastFm client --- app/Services/LastFm/Client.php | 9 +++++++++ tests/Services/LastFm/FactoryTest.php | 1 + 2 files changed, 10 insertions(+) diff --git a/app/Services/LastFm/Client.php b/app/Services/LastFm/Client.php index 3ae5c6a..7dc7b28 100644 --- a/app/Services/LastFm/Client.php +++ b/app/Services/LastFm/Client.php @@ -76,6 +76,15 @@ public function getOptions(): array return $this->apiOptions; } + public function getOption($key, $default = null) + { + $value = \App\array_get($this->apiOptions, $key); + if ($value) { + return $value; + } + return $default; + } + /** * Get the artist api * diff --git a/tests/Services/LastFm/FactoryTest.php b/tests/Services/LastFm/FactoryTest.php index d86ae5f..eb118f1 100644 --- a/tests/Services/LastFm/FactoryTest.php +++ b/tests/Services/LastFm/FactoryTest.php @@ -20,5 +20,6 @@ public function testFactory() ); $this->assertSame('12345', $client->getApiKey()); $this->assertArrayHasKey('pagination', $client->getOptions()); + $this->assertSame(100, $client->getOption('pagination.limit')); } } From f38908b9c07682c58aad0c5dcbf1a7696c56e642 Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Tue, 27 Dec 2016 16:40:11 +1100 Subject: [PATCH 06/33] Add support for pagination (page/totalpages) in the result set component --- app/Services/LastFm/Response/ResultSet.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/app/Services/LastFm/Response/ResultSet.php b/app/Services/LastFm/Response/ResultSet.php index 040bed6..d7a9adf 100644 --- a/app/Services/LastFm/Response/ResultSet.php +++ b/app/Services/LastFm/Response/ResultSet.php @@ -8,6 +8,7 @@ /** * Class ResultSet * Basic collection for the result set from lastfm calls (artists, tracks, etc) + * This result set should be treated as immutable * * @package App\Services\LastFm\Response */ @@ -16,12 +17,27 @@ class ResultSet implements ResultSetContract private $data; private $client; + private $page; + private $totalPages; + private $position = 0; - public function __construct(array $data, Client $client) + public function __construct(array $data, Client $client, $page = 1, $totalPages = null) { $this->data = $data; $this->client = $client; + $this->page = $page; + $this->totalPages = $totalPages; + } + + public function currentPage() + { + return $this->page; + } + + public function totalPages() + { + return $this->totalPages; } /** From 488ba903acf6cc232f01da0c23407688af75fb33 Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Tue, 27 Dec 2016 16:41:21 +1100 Subject: [PATCH 07/33] Add TraitSupportsPagination for api calls --- .../LastFm/Api/TraitSupportsPagintation.php | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 app/Services/LastFm/Api/TraitSupportsPagintation.php diff --git a/app/Services/LastFm/Api/TraitSupportsPagintation.php b/app/Services/LastFm/Api/TraitSupportsPagintation.php new file mode 100644 index 0000000..16f126e --- /dev/null +++ b/app/Services/LastFm/Api/TraitSupportsPagintation.php @@ -0,0 +1,40 @@ +client->getOption('pagination.limit', 5); + } + if (!isset($params['page'])) { + $params['page'] = 1; + } + + return $params; + } + + /** + * @param $methodName + * @param $params + * @param $type + * @param $key + * @return \App\Services\LastFm\Response\ResultSet + */ + private function buildPaginationResultSet($methodName, $params, $type, $key) + { + $params = $this->buildPaginationParams($params); + $response = $this->client->request($methodName, $params); + + return AbstractResponse::makeResultSet($this->client, $response, $type, $key); + } +} From 5582057a0a740d356d486dde5ee38cd55e041ebf Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Tue, 27 Dec 2016 16:41:38 +1100 Subject: [PATCH 08/33] Upgrade Geo api to use new TraitSupportsPagination --- app/Services/LastFm/Api/Geo.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/Services/LastFm/Api/Geo.php b/app/Services/LastFm/Api/Geo.php index 389b131..f7cb069 100644 --- a/app/Services/LastFm/Api/Geo.php +++ b/app/Services/LastFm/Api/Geo.php @@ -12,6 +12,7 @@ */ class Geo { + use TraitSupportsPagintation; /** * @var Client */ @@ -32,8 +33,11 @@ public function __construct(Client $client) public function topArtists($countryCode, array $params = []) { $params = array_merge($params, ['country' => $countryCode]); - $response = $this->client->request('geo.gettopartists', $params); - - return AbstractResponse::makeResultSet($this->client, $response, ArtistResponse::class, 'topartists.artist'); + return $this->buildPaginationResultSet( + 'geo.gettopartists', + $params, + ArtistResponse::class, + 'topartists.artist' + ); } } From 6d8136fa5783796d366070f9c9b64c0e1447e2cc Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Tue, 27 Dec 2016 16:42:02 +1100 Subject: [PATCH 09/33] Update makeResultSet to pass through pagination details from response --- app/Services/LastFm/Response/AbstractResponse.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/app/Services/LastFm/Response/AbstractResponse.php b/app/Services/LastFm/Response/AbstractResponse.php index 174837f..e4f0bc1 100644 --- a/app/Services/LastFm/Response/AbstractResponse.php +++ b/app/Services/LastFm/Response/AbstractResponse.php @@ -58,12 +58,18 @@ public static function make(Client $client, array $response, $type, $key):Abstra public static function makeResultSet(Client $client, $response, $type, $key):ResultSet { self::verifyResponseType($type); - $response = \App\array_get($response, $key); + $responseData = \App\array_get($response, $key); $data = []; - foreach ($response as $item) { + foreach ($responseData as $item) { $data[] = new $type($item, $client); } - return new ResultSet($data, $client); + list($key) = explode('.', $key); + return new ResultSet( + $data, + $client, + \App\array_get($response, $key.'.@attr.page'), + \App\array_get($response, $key.'.@attr.totalPages') + ); } /** From e981af33444172a7f13c1b385b199d4bd5ae9f16 Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Tue, 27 Dec 2016 16:42:30 +1100 Subject: [PATCH 10/33] Allow default controller to pass through current page --- app/Http/Controllers/DefaultController.php | 2 +- app/Models/Forms/SearchForm.php | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/DefaultController.php b/app/Http/Controllers/DefaultController.php index 7c8a185..5922d34 100644 --- a/app/Http/Controllers/DefaultController.php +++ b/app/Http/Controllers/DefaultController.php @@ -26,7 +26,7 @@ public function actionIndex() return $this->view('default', [ 'countries' => Country::all(), 'model' => $searchForm, - 'results' => $searchForm->isSearchable() ? $searchForm->results() : null, + 'results' => $searchForm->isSearchable() ? $searchForm->results($this->getRequestParam('page', 1)) : null, ]); } } diff --git a/app/Models/Forms/SearchForm.php b/app/Models/Forms/SearchForm.php index 2d602d4..484552e 100644 --- a/app/Models/Forms/SearchForm.php +++ b/app/Models/Forms/SearchForm.php @@ -23,13 +23,14 @@ class SearchForm extends AbstractModel /** * Calculate the result set based on the current state of the object * + * @param int $page * @return ResultSet */ - public function results() + public function results($page = 1) { $client = $this->getLastFmApi(); - return $client->geo->topArtists($this->country()->name); + return $client->geo->topArtists($this->country()->name, ['page' => $page]); } /** From 559c357c17a36c266577968e52d5c73df5ec2029 Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Tue, 27 Dec 2016 16:51:43 +1100 Subject: [PATCH 11/33] Add mechanism to ensure page value is correct --- app/Services/LastFm/Api/TraitSupportsPagintation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Services/LastFm/Api/TraitSupportsPagintation.php b/app/Services/LastFm/Api/TraitSupportsPagintation.php index 16f126e..6e2b0bf 100644 --- a/app/Services/LastFm/Api/TraitSupportsPagintation.php +++ b/app/Services/LastFm/Api/TraitSupportsPagintation.php @@ -16,7 +16,7 @@ private function buildPaginationParams($params) if (!isset($params['limit'])) { $params['limit'] = $this->client->getOption('pagination.limit', 5); } - if (!isset($params['page'])) { + if (!isset($params['page']) || $params['page'] < 1) { $params['page'] = 1; } From 950d8719282a32484d9f8a83e3d1afca09ed37fd Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Tue, 27 Dec 2016 16:51:53 +1100 Subject: [PATCH 12/33] Add fix to resolve lastfm pagination issue --- .../LastFm/Api/TraitSupportsPagintation.php | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/app/Services/LastFm/Api/TraitSupportsPagintation.php b/app/Services/LastFm/Api/TraitSupportsPagintation.php index 6e2b0bf..cbc3511 100644 --- a/app/Services/LastFm/Api/TraitSupportsPagintation.php +++ b/app/Services/LastFm/Api/TraitSupportsPagintation.php @@ -35,6 +35,38 @@ private function buildPaginationResultSet($methodName, $params, $type, $key) $params = $this->buildPaginationParams($params); $response = $this->client->request($methodName, $params); + /** + * !important + * see method note below + */ + $response = $this->fixLastFmPaginationBug($response, $key); + return AbstractResponse::makeResultSet($this->client, $response, $type, $key); } + + /** + * Note: LastFM's pagination mechanism is very broken! Requesting a limit & page does + * not reply with the expected result, thus the following is a client side fix to ensure the result set is + * correct. + * The problem is the limit is ignored, and the result includes previous pages! + * If they ever fix this problem, this 'hack' should not effect the result set + * + * @param $key + * @return mixed + */ + private function fixLastFmPaginationBug($response, $key) + { + $data = array_pull($response, $key); + if (count($data) > $this->client->getOption('pagination.limit', 5)) { + $data = array_slice($data, $this->client->getOption('pagination.limit', 5) * -1); + } + + array_set( + $response, + $key, + $data + ); + + return $response; + } } From a83b86a620718c55870dbe96e415002a73cee272 Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Tue, 27 Dec 2016 16:52:08 +1100 Subject: [PATCH 13/33] Add $url for pagination base url --- resources/views/default.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/default.blade.php b/resources/views/default.blade.php index 433a417..03db644 100644 --- a/resources/views/default.blade.php +++ b/resources/views/default.blade.php @@ -13,7 +13,7 @@ @endforeach - @include('partials.pagination', ['resultSet' => $results]) + @include('partials.pagination', ['resultSet' => $results, 'url' => '?SearchForm%5Bcountry%5D='.$model->country.'&']) @else Please select a country @endif From 89c7d16d1c9b480e519f2f90d4c246f738fa057f Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Tue, 27 Dec 2016 16:52:15 +1100 Subject: [PATCH 14/33] Implement pagination view --- resources/views/partials/pagination.blade.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/resources/views/partials/pagination.blade.php b/resources/views/partials/pagination.blade.php index e69de29..215cfe4 100644 --- a/resources/views/partials/pagination.blade.php +++ b/resources/views/partials/pagination.blade.php @@ -0,0 +1,8 @@ +@if ($resultSet->currentPage() > 1) + Prev +@endif +Page {{ $resultSet->currentPage() }} of {{ $resultSet->totalPages() }} + +@if ($resultSet->currentPage() < $resultSet->totalPages()) + Next +@endif \ No newline at end of file From 44eeb18a49a84c50654b4340bdaf61c97729621c Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Tue, 27 Dec 2016 16:57:08 +1100 Subject: [PATCH 15/33] Add doctrine/cache to dependencies --- composer.json | 3 ++- composer.lock | 74 +++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 74 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 5505998..0f9cf68 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,8 @@ "symfony/psr-http-message-bridge": "^1.0", "zendframework/zend-diactoros": "^1.3", "guzzlehttp/guzzle": "^6.2", - "philo/laravel-blade": "^3.1" + "philo/laravel-blade": "^3.1", + "doctrine/cache": "^1.6" }, "require-dev": { "phpunit/phpunit": "^5.7", diff --git a/composer.lock b/composer.lock index 1d4316d..31f8954 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "98cef4fec8eff7e038b5428c5b531ef3", - "content-hash": "a45e0c9aff2bbd30f4c64e7ceb9aaeb1", + "hash": "5319787e4ec78e52b73179766ac105af", + "content-hash": "f1281a1688e33381976e4ace553343e0", "packages": [ { "name": "container-interop/container-interop", @@ -34,6 +34,76 @@ "description": "Promoting the interoperability of container objects (DIC, SL, etc.)", "time": "2014-12-30 15:22:37" }, + { + "name": "doctrine/cache", + "version": "v1.6.1", + "source": { + "type": "git", + "url": "https://github.com/doctrine/cache.git", + "reference": "b6f544a20f4807e81f7044d31e679ccbb1866dc3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/cache/zipball/b6f544a20f4807e81f7044d31e679ccbb1866dc3", + "reference": "b6f544a20f4807e81f7044d31e679ccbb1866dc3", + "shasum": "" + }, + "require": { + "php": "~5.5|~7.0" + }, + "conflict": { + "doctrine/common": ">2.2,<2.4" + }, + "require-dev": { + "phpunit/phpunit": "~4.8|~5.0", + "predis/predis": "~1.0", + "satooshi/php-coveralls": "~0.6" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.6.x-dev" + } + }, + "autoload": { + "psr-4": { + "Doctrine\\Common\\Cache\\": "lib/Doctrine/Common/Cache" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com" + }, + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com" + }, + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com" + } + ], + "description": "Caching library offering an object-oriented API for many cache backends", + "homepage": "http://www.doctrine-project.org", + "keywords": [ + "cache", + "caching" + ], + "time": "2016-10-29 11:16:17" + }, { "name": "doctrine/inflector", "version": "v1.1.0", From e350f3123414467d1d72990fc22a77b10f647f89 Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Wed, 28 Dec 2016 14:32:30 +1100 Subject: [PATCH 16/33] Add gitkeep to cache storage dir --- storage/cache/.gitkeep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 storage/cache/.gitkeep diff --git a/storage/cache/.gitkeep b/storage/cache/.gitkeep new file mode 100644 index 0000000..e69de29 From 055c1ad955f8823451600cd25773029a7cae0d35 Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Wed, 28 Dec 2016 14:32:50 +1100 Subject: [PATCH 17/33] Add storage/cache to git ignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index efb4c56..6505b66 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /vendor/ .env .idea -/storage/views/ \ No newline at end of file +/storage/views/ +/storage/cache/ \ No newline at end of file From b73cc39b57d06920b81ed481e50c57186dec741d Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Wed, 28 Dec 2016 14:34:21 +1100 Subject: [PATCH 18/33] Add config for cache component --- .env.example | 5 ++++- config/cache.php | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 config/cache.php diff --git a/.env.example b/.env.example index c840559..ad20d6b 100644 --- a/.env.example +++ b/.env.example @@ -2,4 +2,7 @@ APP_ENV=dev LASTFM_KEY= LASTFM_SECRET= -LASTFM_PAGINATION_LIMIT=5 \ No newline at end of file +LASTFM_PAGINATION_LIMIT=5 + +CACHE_DRIVER=file +CACHE_FILE_PATH=storage/cache \ No newline at end of file diff --git a/config/cache.php b/config/cache.php new file mode 100644 index 0000000..84741e7 --- /dev/null +++ b/config/cache.php @@ -0,0 +1,11 @@ + getenv('CACHE_DRIVER'), + 'drivers' => [ + 'file' => [ + 'driver' => 'file', + 'path' => App\path(getenv('CACHE_FILE_PATH')), + ], + ] +]; \ No newline at end of file From 93d4a91369a401b013865cc10933c6a4e823fc5a Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Wed, 28 Dec 2016 14:35:45 +1100 Subject: [PATCH 19/33] Add cache into app container as service --- bootstrap/app.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/bootstrap/app.php b/bootstrap/app.php index d060979..1ff130a 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -1,6 +1,8 @@ share('cache', function () use ($app) { + $config = $app->get('config'); + switch ($config->get('cache.driver')) { + case 'file': + return new FilesystemCache($config->get('cache.drivers.file.path')); + break; + default: + throw new RuntimeException(sprintf('Cache engine %s is not supported by the application', $config->get('cache.engine'))); + } +}); + $app->add('lastfm', function () use ($app) { return \App\Services\LastFm\Factory::fromConfig($app->get('config')->get('lastfm')); }); From 757c5b30ecf46919b1cb5510cb2c9ac9859d5d8e Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Wed, 28 Dec 2016 14:37:02 +1100 Subject: [PATCH 20/33] Refactor LastFM Client factory into make method This will remove hard coding of the client class. --- app/Services/LastFm/Factory.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/app/Services/LastFm/Factory.php b/app/Services/LastFm/Factory.php index 27916ec..8e7ac87 100644 --- a/app/Services/LastFm/Factory.php +++ b/app/Services/LastFm/Factory.php @@ -15,10 +15,21 @@ class Factory * @return Client */ public static function fromConfig(array $config): Client + { + return self::make(Client::class, $config); + } + + /** + * Make an instance of the lastfm client, using the specified type and configuration values + * @param $class + * @param $config + * @return mixed + */ + private static function make($class, $config) { $key = array_pull($config, 'api_key'); $secret = array_pull($config, 'api_secret'); - return new Client($key, $secret, $config); + return new $class($key, $secret, $config); } } From 25f2657a7347d425efe129331408f5334da515a4 Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Wed, 28 Dec 2016 14:38:36 +1100 Subject: [PATCH 21/33] Add client extension for lastfm api to enable request caching --- app/Support/LastFmClientCache.php | 78 +++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 app/Support/LastFmClientCache.php diff --git a/app/Support/LastFmClientCache.php b/app/Support/LastFmClientCache.php new file mode 100644 index 0000000..1d5918e --- /dev/null +++ b/app/Support/LastFmClientCache.php @@ -0,0 +1,78 @@ +makeCacheKey($methodName, $params); + $value = $this->getCacheProvider()->fetch($key); + if ($value === false) { + $value = parent::request($methodName, $params); + $this->getCacheProvider()->save($key, $value, self::CACHE_TTL_DEFAULT); + } + return $value; + } + + /** + * Set the instance of the cache provider to use + * + * @param CacheProvider $provider + */ + public function setCacheProvider(CacheProvider $provider) + { + $this->cache = $provider; + } + + /** + * Retrieve the instance of the cache provider + * + * @return CacheProvider + * @throws RuntimeException + */ + public function getCacheProvider(): CacheProvider + { + if (!$this->cache) { + throw new RuntimeException(sprintf("No cache has been provided to %s", get_class($this))); + } + return $this->cache; + } + + /** + * Create a unique string to represent the state of the api request + * + * @param string $methodName + * @param array $params + * @return string + */ + private function makeCacheKey($methodName, array $params): string + { + return self::CACHE_KEY_PREFIX . md5(serialize([$methodName, $params])); + } + +} From d3aae3f69b784865a95c2fe1ba49ce1a47ca6589 Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Wed, 28 Dec 2016 14:40:10 +1100 Subject: [PATCH 22/33] Add factory method to handle caching lastfm client --- app/Services/LastFm/Factory.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/app/Services/LastFm/Factory.php b/app/Services/LastFm/Factory.php index 8e7ac87..5f4be67 100644 --- a/app/Services/LastFm/Factory.php +++ b/app/Services/LastFm/Factory.php @@ -6,6 +6,9 @@ namespace App\Services\LastFm; +use App\Support\LastFmClientCache; +use Doctrine\Common\Cache\CacheProvider; + class Factory { /** @@ -19,6 +22,21 @@ public static function fromConfig(array $config): Client return self::make(Client::class, $config); } + /** + * Make an instance of the lastfm api client that supports request caching + * + * @param array $config + * @param CacheProvider $cache + * @return Client + */ + public static function fromConfigWithCaching(array $config, CacheProvider $cache): Client + { + $client = self::make(LastFmClientCache::class, $config); + $client->setCacheProvider($cache); + + return $client; + } + /** * Make an instance of the lastfm client, using the specified type and configuration values * @param $class From fb8671212ad3dece8cc2fd9460cd24a531e388d1 Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Wed, 28 Dec 2016 14:40:21 +1100 Subject: [PATCH 23/33] Update documentation for Factory::make --- app/Services/LastFm/Factory.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Services/LastFm/Factory.php b/app/Services/LastFm/Factory.php index 5f4be67..f22ff8d 100644 --- a/app/Services/LastFm/Factory.php +++ b/app/Services/LastFm/Factory.php @@ -41,9 +41,9 @@ public static function fromConfigWithCaching(array $config, CacheProvider $cache * Make an instance of the lastfm client, using the specified type and configuration values * @param $class * @param $config - * @return mixed + * @return Client */ - private static function make($class, $config) + private static function make($class, $config): Client { $key = array_pull($config, 'api_key'); $secret = array_pull($config, 'api_secret'); From abefbebada21e15607b7f4208f435d622e759d7b Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Wed, 28 Dec 2016 14:40:33 +1100 Subject: [PATCH 24/33] Update lastfm service to use caching version --- bootstrap/app.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bootstrap/app.php b/bootstrap/app.php index 1ff130a..8c2c852 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -46,7 +46,7 @@ }); $app->add('lastfm', function () use ($app) { - return \App\Services\LastFm\Factory::fromConfig($app->get('config')->get('lastfm')); + return \App\Services\LastFm\Factory::fromConfigWithCaching($app->get('config')->get('lastfm'), $app->get('cache')); }); //endregion From 7b402cacb3b05908e556f1be74c6dd14c9d14a1a Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Wed, 28 Dec 2016 14:41:22 +1100 Subject: [PATCH 25/33] Add RuntimeException class --- app/Exceptions/RuntimeException.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 app/Exceptions/RuntimeException.php diff --git a/app/Exceptions/RuntimeException.php b/app/Exceptions/RuntimeException.php new file mode 100644 index 0000000..52c3711 --- /dev/null +++ b/app/Exceptions/RuntimeException.php @@ -0,0 +1,16 @@ + Date: Wed, 28 Dec 2016 14:50:52 +1100 Subject: [PATCH 26/33] Fix missing space in RuntimeException import --- app/Exceptions/RuntimeException.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Exceptions/RuntimeException.php b/app/Exceptions/RuntimeException.php index 52c3711..448e93f 100644 --- a/app/Exceptions/RuntimeException.php +++ b/app/Exceptions/RuntimeException.php @@ -2,7 +2,7 @@ namespace App\Exceptions; -use RuntimeExceptionas Exception; +use RuntimeException as Exception; /** * RuntimeException From 77fa72c81c244294f731906c156ab2b081b15f5a Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Wed, 28 Dec 2016 15:05:05 +1100 Subject: [PATCH 27/33] Fixed misc psr2 issues with helpers and LastFmClientCache --- app/Support/LastFmClientCache.php | 1 - app/helpers.php | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/Support/LastFmClientCache.php b/app/Support/LastFmClientCache.php index 1d5918e..c86866b 100644 --- a/app/Support/LastFmClientCache.php +++ b/app/Support/LastFmClientCache.php @@ -74,5 +74,4 @@ private function makeCacheKey($methodName, array $params): string { return self::CACHE_KEY_PREFIX . md5(serialize([$methodName, $params])); } - } diff --git a/app/helpers.php b/app/helpers.php index 3b9ce6d..dd73007 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -32,7 +32,7 @@ function array_get(&$data, $path) /** * Retrieve an instance of the application Container - * + * * @return Container */ function app() @@ -49,7 +49,7 @@ function app() function path($relPath = null) { $path = app()->root(); - if ($relPath) { + if ($relPath !== null) { $path .= trim($relPath, ' /'); } return $path; From d097f9ed8714344d1bd910c2d699b1d3eb02a1fa Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Wed, 28 Dec 2016 15:05:53 +1100 Subject: [PATCH 28/33] Fix incorrect indenting on Container --- app/Support/Container.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Support/Container.php b/app/Support/Container.php index c34272b..214f1e4 100644 --- a/app/Support/Container.php +++ b/app/Support/Container.php @@ -77,7 +77,7 @@ public static function instance() */ public function root() { - return $this->pathRoot; + return $this->pathRoot; } /** From 4849ce105070dd8b512c232af7fe0a60e7799755 Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Wed, 28 Dec 2016 20:26:15 +1100 Subject: [PATCH 29/33] Add/Update docblocks --- app/Exceptions/HttpException.php | 5 +++++ app/Http/Controllers/AbstractController.php | 2 +- app/Http/Controllers/ArtistController.php | 13 +++++++++++++ app/Http/Controllers/DefaultController.php | 1 + app/Http/routes.php | 2 ++ app/Models/AbstractModel.php | 12 ++++++++++++ app/Models/Country.php | 6 ++++++ app/Models/Forms/SearchForm.php | 6 +++++- app/Services/LastFm/Api/Artist.php | 4 ++-- app/Services/LastFm/Client.php | 6 ++++++ app/Services/LastFm/Response/Artist.php | 7 +++++++ app/Services/LastFm/SearchRequest.php | 7 +++++++ app/Support/Container.php | 8 ++++---- app/helpers.php | 3 ++- 14 files changed, 73 insertions(+), 9 deletions(-) diff --git a/app/Exceptions/HttpException.php b/app/Exceptions/HttpException.php index 0a0b636..4412949 100644 --- a/app/Exceptions/HttpException.php +++ b/app/Exceptions/HttpException.php @@ -4,6 +4,11 @@ use Exception; +/** + * HttpException + * + * @package App\Exceptions + */ class HttpException extends Exception { } diff --git a/app/Http/Controllers/AbstractController.php b/app/Http/Controllers/AbstractController.php index 91cff01..6816a96 100644 --- a/app/Http/Controllers/AbstractController.php +++ b/app/Http/Controllers/AbstractController.php @@ -83,7 +83,7 @@ public static function __callStatic($method, $params) * * @param string $path * @param array $params - * @return \Psr\Http\Message\StreamInterface + * @return ResponseInterface */ protected function view($path, array $params = []) { diff --git a/app/Http/Controllers/ArtistController.php b/app/Http/Controllers/ArtistController.php index 0e67503..e40cd6a 100644 --- a/app/Http/Controllers/ArtistController.php +++ b/app/Http/Controllers/ArtistController.php @@ -5,14 +5,27 @@ use App\Exceptions\HttpException; use App\Services\LastFm\Response\Artist; +/** + * ArtistController + * + * @package App\Http\Controllers + */ class ArtistController extends AbstractController { + /** + * Display details for the selected artist + * + * @param string $id music brains id + * @return \Psr\Http\Message\ResponseInterface + */ public function actionView($id) { return $this->view('artist', ['artist' => $this->getModel($id)]); } /** + * Load the artist model based on the supplied id + * * @param string $id * @return Artist * @throws HttpException diff --git a/app/Http/Controllers/DefaultController.php b/app/Http/Controllers/DefaultController.php index 5922d34..9b259c6 100644 --- a/app/Http/Controllers/DefaultController.php +++ b/app/Http/Controllers/DefaultController.php @@ -5,6 +5,7 @@ use App\Models\Country; use App\Models\Forms\SearchForm; + /** * Class DefaultController * diff --git a/app/Http/routes.php b/app/Http/routes.php index 34e6826..5a3c9fc 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -3,6 +3,8 @@ use App\Http\Controllers\ArtistController; use App\Http\Controllers\DefaultController; + + /** * @var \League\Route\RouteCollection $route */ diff --git a/app/Models/AbstractModel.php b/app/Models/AbstractModel.php index 1bf5e0c..2c4e837 100644 --- a/app/Models/AbstractModel.php +++ b/app/Models/AbstractModel.php @@ -12,6 +12,12 @@ abstract class AbstractModel { protected $attributes = []; + /** + * AbstractModel constructor. + * Initialise the concrete model with the supplied attributes + * + * @param null $attributes + */ public function __construct($attributes = null) { if (is_array($attributes)) { @@ -19,6 +25,12 @@ public function __construct($attributes = null) } } + /** + * Fill the model with the array of attributes, in addition to any existing attribute values + * Any existing values which matching keys will be overridden. + * + * @param array $attributes + */ public function fill(array $attributes) { $this->attributes = array_merge($this->attributes, $attributes); diff --git a/app/Models/Country.php b/app/Models/Country.php index bbf48d8..31b7181 100644 --- a/app/Models/Country.php +++ b/app/Models/Country.php @@ -4,9 +4,15 @@ use App\Exceptions\InvalidParamException; +/** + * Country + * + * @package App\Models + */ class Country { const DATA_FILE = 'resources/data/countries.json'; + /** * Raw country data * diff --git a/app/Models/Forms/SearchForm.php b/app/Models/Forms/SearchForm.php index 484552e..ed0de3e 100644 --- a/app/Models/Forms/SearchForm.php +++ b/app/Models/Forms/SearchForm.php @@ -5,8 +5,12 @@ use App\Models\AbstractModel; use App\Models\Country; use App\Services\LastFm\Client; +use App\Services\LastFm\Contracts\ResultSet; /** + * SearchForm model + * Represents the state of the search form and allows a result set to be produced for the top artist + * as per the attributes * * @property Country country * @package App\Models @@ -37,7 +41,7 @@ public function results($page = 1) * Check the state of the SearchForm model to determine if a search could * return possible results * - * @return boolean [description] + * @return boolean */ public function isSearchable() { diff --git a/app/Services/LastFm/Api/Artist.php b/app/Services/LastFm/Api/Artist.php index dd29f23..6c69050 100644 --- a/app/Services/LastFm/Api/Artist.php +++ b/app/Services/LastFm/Api/Artist.php @@ -65,7 +65,7 @@ public function search(SearchRequest $request, array $params = []): ResultSet * Find the artist by their id * * @param string $artistRef either the musicbrainz id or the artist name - * @return ArtistResponse|bool false when no artist found + * @return AbstractResponse|bool false when no artist found * @throws Exception */ public function find($artistRef) @@ -122,7 +122,7 @@ public function topTracks($artistRef, array $params = []) } /** - * Build the arist api params based on the type of data being provided + * Build the artist api params based on the type of data being provided * * @param string $artistRef * @return array diff --git a/app/Services/LastFm/Client.php b/app/Services/LastFm/Client.php index 7dc7b28..b0286e3 100644 --- a/app/Services/LastFm/Client.php +++ b/app/Services/LastFm/Client.php @@ -76,6 +76,12 @@ public function getOptions(): array return $this->apiOptions; } + /** + * Get the custom option ba + * @param $key + * @param null $default + * @return mixed|null + */ public function getOption($key, $default = null) { $value = \App\array_get($this->apiOptions, $key); diff --git a/app/Services/LastFm/Response/Artist.php b/app/Services/LastFm/Response/Artist.php index a08fa1e..8f9aa45 100644 --- a/app/Services/LastFm/Response/Artist.php +++ b/app/Services/LastFm/Response/Artist.php @@ -2,6 +2,13 @@ namespace App\Services\LastFm\Response; +/** + * Class Artist + * + * @package App\Services\LastFm\Response + * @property array $image + * @property array $mbid + */ class Artist extends AbstractResponse { const IMAGE_SIZE_SMALL = 'small'; diff --git a/app/Services/LastFm/SearchRequest.php b/app/Services/LastFm/SearchRequest.php index 95b2c45..1c538ec 100644 --- a/app/Services/LastFm/SearchRequest.php +++ b/app/Services/LastFm/SearchRequest.php @@ -34,6 +34,13 @@ public function has($key) return array_key_exists($key, $this->data); } + /** + * Get the search request param + * + * @param string $key + * @param mixed $default + * @return mixed|null + */ public function get($key, $default = null) { if ($this->has($key)) { diff --git a/app/Support/Container.php b/app/Support/Container.php index 214f1e4..57b5c0a 100644 --- a/app/Support/Container.php +++ b/app/Support/Container.php @@ -18,10 +18,10 @@ * Registers services and routes request * * @package App\Support - * @method mixed get(string $alias, array $args) + * @method mixed get(string $alias, array $args = []) * @method bool has(string $alias) - * @method DefinitionInterface add(string $alias, mixed|null $concrete, boolean $share) - * @method DefinitionInterface share(string $alias, mixed|null $conrete) + * @method DefinitionInterface add(string $alias, mixed | null $concrete, boolean $share) + * @method DefinitionInterface share(string $alias, mixed | null $concrete) */ class Container { @@ -154,7 +154,7 @@ private function getRouteCollection($path): RouteCollection * @param RouteCollection $route * @return ResponseInterface */ - private function dispatchRoute($route):ResponseInterface + private function dispatchRoute($route): ResponseInterface { $psr7 = new DiactorosFactory(); diff --git a/app/helpers.php b/app/helpers.php index dd73007..416ff48 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -13,7 +13,7 @@ /** * Get a value out of the provided array using dot-notation * - * @param array $data + * @param array $data * @param string $path * @return mixed|null */ @@ -52,5 +52,6 @@ function path($relPath = null) if ($relPath !== null) { $path .= trim($relPath, ' /'); } + return $path; } From ee4bf0c0971a0aa2b2734cb863a0d6972bdcade1 Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Wed, 28 Dec 2016 20:26:27 +1100 Subject: [PATCH 30/33] Update composer --- composer.lock | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/composer.lock b/composer.lock index 31f8954..7c697ce 100644 --- a/composer.lock +++ b/composer.lock @@ -235,28 +235,28 @@ }, { "name": "guzzlehttp/promises", - "version": "1.3.0", + "version": "v1.3.1", "source": { "type": "git", "url": "https://github.com/guzzle/promises.git", - "reference": "2693c101803ca78b27972d84081d027fca790a1e" + "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/guzzle/promises/zipball/2693c101803ca78b27972d84081d027fca790a1e", - "reference": "2693c101803ca78b27972d84081d027fca790a1e", + "url": "https://api.github.com/repos/guzzle/promises/zipball/a59da6cf61d80060647ff4d3eb2c03a2bc694646", + "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646", "shasum": "" }, "require": { "php": ">=5.5.0" }, "require-dev": { - "phpunit/phpunit": "~4.0" + "phpunit/phpunit": "^4.0" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "1.0-dev" + "dev-master": "1.4-dev" } }, "autoload": { @@ -282,7 +282,7 @@ "keywords": [ "promise" ], - "time": "2016-11-18 17:47:58" + "time": "2016-12-20 10:07:11" }, { "name": "guzzlehttp/psr7", @@ -1743,16 +1743,16 @@ }, { "name": "phpunit/php-code-coverage", - "version": "4.0.3", + "version": "4.0.4", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "903fd6318d0a90b4770a009ff73e4a4e9c437929" + "reference": "c14196e64a78570034afd0b7a9f3757ba71c2a0a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/903fd6318d0a90b4770a009ff73e4a4e9c437929", - "reference": "903fd6318d0a90b4770a009ff73e4a4e9c437929", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/c14196e64a78570034afd0b7a9f3757ba71c2a0a", + "reference": "c14196e64a78570034afd0b7a9f3757ba71c2a0a", "shasum": "" }, "require": { @@ -1802,7 +1802,7 @@ "testing", "xunit" ], - "time": "2016-11-28 16:00:31" + "time": "2016-12-20 15:22:42" }, { "name": "phpunit/php-file-iterator", @@ -1987,16 +1987,16 @@ }, { "name": "phpunit/phpunit", - "version": "5.7.3", + "version": "5.7.4", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "de164acc2f2bb0b79beb892a36260264b2a03233" + "reference": "af91da3f2671006ff5d0628023de3b7ac4d1ef09" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/de164acc2f2bb0b79beb892a36260264b2a03233", - "reference": "de164acc2f2bb0b79beb892a36260264b2a03233", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/af91da3f2671006ff5d0628023de3b7ac4d1ef09", + "reference": "af91da3f2671006ff5d0628023de3b7ac4d1ef09", "shasum": "" }, "require": { @@ -2017,7 +2017,7 @@ "sebastian/diff": "~1.2", "sebastian/environment": "^1.3.4 || ^2.0", "sebastian/exporter": "~2.0", - "sebastian/global-state": "~1.0", + "sebastian/global-state": "^1.0 || ^2.0", "sebastian/object-enumerator": "~2.0", "sebastian/resource-operations": "~1.0", "sebastian/version": "~1.0|~2.0", @@ -2065,7 +2065,7 @@ "testing", "xunit" ], - "time": "2016-12-09 02:48:53" + "time": "2016-12-13 16:19:44" }, { "name": "phpunit/phpunit-mock-objects", @@ -2641,16 +2641,16 @@ }, { "name": "symfony/yaml", - "version": "v3.2.0", + "version": "v3.2.1", "source": { "type": "git", "url": "https://github.com/symfony/yaml.git", - "reference": "f2300ba8fbb002c028710b92e1906e7457410693" + "reference": "a7095af4b97a0955f85c8989106c249fa649011f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/yaml/zipball/f2300ba8fbb002c028710b92e1906e7457410693", - "reference": "f2300ba8fbb002c028710b92e1906e7457410693", + "url": "https://api.github.com/repos/symfony/yaml/zipball/a7095af4b97a0955f85c8989106c249fa649011f", + "reference": "a7095af4b97a0955f85c8989106c249fa649011f", "shasum": "" }, "require": { @@ -2692,7 +2692,7 @@ ], "description": "Symfony Yaml Component", "homepage": "https://symfony.com", - "time": "2016-11-18 21:17:59" + "time": "2016-12-10 10:07:06" }, { "name": "webmozart/assert", From c18e5222e1c82b7cbdb6c7895b13f0a5d07f6a8f Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Wed, 28 Dec 2016 20:27:35 +1100 Subject: [PATCH 31/33] Tidy code to psr2 --- app/Services/LastFm/Api/Artist.php | 3 +-- app/Services/LastFm/Api/Geo.php | 2 +- app/Services/LastFm/Api/TraitSupportsPagintation.php | 2 +- app/Support/Container.php | 5 +++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/Services/LastFm/Api/Artist.php b/app/Services/LastFm/Api/Artist.php index 6c69050..ac45cf6 100644 --- a/app/Services/LastFm/Api/Artist.php +++ b/app/Services/LastFm/Api/Artist.php @@ -99,8 +99,7 @@ public function find($artistRef) */ public function topTracks($artistRef, array $params = []) { - $params = $this->buildArtistParams($artistRef); - $params = array_merge($params, $params); + $params = array_merge($params, $this->buildArtistParams($artistRef)); try { $response = $this->client->request( 'artist.gettoptracks', diff --git a/app/Services/LastFm/Api/Geo.php b/app/Services/LastFm/Api/Geo.php index f7cb069..d77991e 100644 --- a/app/Services/LastFm/Api/Geo.php +++ b/app/Services/LastFm/Api/Geo.php @@ -3,7 +3,7 @@ namespace App\Services\LastFm\Api; use App\Services\LastFm\Client; -use App\Services\LastFm\Response\AbstractResponse; +use App\Services\LastFm\Contracts\ResultSet; use App\Services\LastFm\Response\Artist as ArtistResponse; /** diff --git a/app/Services/LastFm/Api/TraitSupportsPagintation.php b/app/Services/LastFm/Api/TraitSupportsPagintation.php index cbc3511..de3b8aa 100644 --- a/app/Services/LastFm/Api/TraitSupportsPagintation.php +++ b/app/Services/LastFm/Api/TraitSupportsPagintation.php @@ -54,7 +54,7 @@ private function buildPaginationResultSet($methodName, $params, $type, $key) * @param $key * @return mixed */ - private function fixLastFmPaginationBug($response, $key) + private function fixLastFmPaginationBug(array $response, $key): array { $data = array_pull($response, $key); if (count($data) > $this->client->getOption('pagination.limit', 5)) { diff --git a/app/Support/Container.php b/app/Support/Container.php index 57b5c0a..75e38d7 100644 --- a/app/Support/Container.php +++ b/app/Support/Container.php @@ -55,7 +55,7 @@ public function __construct(DiContainer $di, $path) new ReflectionContainer ); $this->dependency = $di; - $this->pathRoot = rtrim($path, '/').'/'; + $this->pathRoot = rtrim($path, '/') . '/'; self::$instance = $this; } @@ -133,6 +133,7 @@ private function handleResponseTypes($response) if (!($response instanceof ResponseInterface)) { throw new \RuntimeException("Route response is unsupported"); } + return $response; } @@ -145,7 +146,7 @@ private function getRouteCollection($path): RouteCollection $route = new RouteCollection($this->dependency); // load the - require $this->pathRoot.$path; + require $this->pathRoot . $path; return $route; } From 4bae2bb3f7972ad1bdc4bdc064a0be8e0bca74a8 Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Thu, 29 Dec 2016 14:06:06 +1100 Subject: [PATCH 32/33] Fix docblocks --- app/Services/LastFm/Api/TraitSupportsPagintation.php | 5 +++-- tests/Fake/DependencyTest.php | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/Services/LastFm/Api/TraitSupportsPagintation.php b/app/Services/LastFm/Api/TraitSupportsPagintation.php index de3b8aa..491c773 100644 --- a/app/Services/LastFm/Api/TraitSupportsPagintation.php +++ b/app/Services/LastFm/Api/TraitSupportsPagintation.php @@ -51,8 +51,9 @@ private function buildPaginationResultSet($methodName, $params, $type, $key) * The problem is the limit is ignored, and the result includes previous pages! * If they ever fix this problem, this 'hack' should not effect the result set * - * @param $key - * @return mixed + * @param array $response + * @param string $key + * @return array */ private function fixLastFmPaginationBug(array $response, $key): array { diff --git a/tests/Fake/DependencyTest.php b/tests/Fake/DependencyTest.php index 26cf35b..e0f57df 100644 --- a/tests/Fake/DependencyTest.php +++ b/tests/Fake/DependencyTest.php @@ -13,7 +13,7 @@ class DependencyTest /** * DependencyTest constructor. * - * @param SimpleClass $object + * @param Simple $object */ public function __construct(Simple $object) { From 433f935934a322c5e37aa1dc2ad75c707946d886 Mon Sep 17 00:00:00 2001 From: Ben Rowe Date: Thu, 29 Dec 2016 14:06:48 +1100 Subject: [PATCH 33/33] Update logic statements in blade files --- resources/views/default.blade.php | 22 +++++++++++-------- resources/views/partials/pagination.blade.php | 14 +++++++----- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/resources/views/default.blade.php b/resources/views/default.blade.php index 03db644..a2e423d 100644 --- a/resources/views/default.blade.php +++ b/resources/views/default.blade.php @@ -4,16 +4,20 @@ @section('content') @include('partials.form', ['model' => $model]) - @if ($results) + @if ($results !== null)

Top Artists Per Country: {{ $model->country()->name }}

-
    - @foreach ($results as $artist) -
  1. - @include('partials.artist', ['artist' => $artist]) -
  2. - @endforeach -
- @include('partials.pagination', ['resultSet' => $results, 'url' => '?SearchForm%5Bcountry%5D='.$model->country.'&']) + @if (count($results) > 0) +
    + @foreach ($results as $pos => $artist) +
  • + @include('partials.artist', ['artist' => $artist]) +
  • + @endforeach +
+ @include('partials.pagination', ['resultSet' => $results, 'url' => '?SearchForm%5Bcountry%5D='.$model->country.'&']) + @else +

No results can be found for {{ $model->country()->name }}

+ @endif @else Please select a country @endif diff --git a/resources/views/partials/pagination.blade.php b/resources/views/partials/pagination.blade.php index 215cfe4..ae62fac 100644 --- a/resources/views/partials/pagination.blade.php +++ b/resources/views/partials/pagination.blade.php @@ -1,8 +1,10 @@ -@if ($resultSet->currentPage() > 1) - Prev -@endif -Page {{ $resultSet->currentPage() }} of {{ $resultSet->totalPages() }} +@if ($resultSet->totalPages() > 0) + @if ($resultSet->currentPage() > 1) + Prev + @endif + Page {{ $resultSet->currentPage() }} of {{ $resultSet->totalPages() }} -@if ($resultSet->currentPage() < $resultSet->totalPages()) - Next + @if ($resultSet->currentPage() < $resultSet->totalPages()) + Next + @endif @endif \ No newline at end of file