diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 04fd6e2..ecf7a4b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -18,10 +18,10 @@ jobs: uses: shivammathur/setup-php@v2 with: php-version: '8.1' - extensions: mbstring, intl + extensions: mbstring, intl, ext-http - name: Install dependencies - run: composer install + run: composer install --ignore-platform-req=ext-http - name: Run PHPUnit run: vendor/bin/phpunit diff --git a/README.md b/README.md index 8c00032..7d91dc7 100644 --- a/README.md +++ b/README.md @@ -22,20 +22,24 @@ composer require igzard/ncore ## Examples -Searching for Toy Story in category: Film (HUN SD) +Searching for Toy Story in category: Film (HUN SD). By default, without category, it searches in film hun sd. ```php $ncore = new Ncore('{passkey}'); -$torrents = $ncore->search( - (new Search())->setSearch('Toy Story')->setCat(Category::FILM_HUN_SD) -); +$torrents = $ncore->search([ + 'search' => 'Toy Story', + 'category' => Category::FILM_HUN_SD +]); ``` -Download first match for Toy Story in category: Film (HUN SD) +Download first match for Toy Story in category: Film (HUN SD). By default, without category, it searches in film hun sd. ```php $ncore = new Ncore('{passkey}'); $torrents = $ncore->download( - (new Search())->setSearch('Toy Story')->setCat(Category::FILM_HUN_SD), + [ + 'search' => 'Toy Story', + 'category' => Category::FILM_HUN_SD + ], '{pathToDownload}', '{filename}' ); @@ -48,6 +52,9 @@ Thank you for considering contributing to the PHP Ncore! To contribution follow ```bash git clone git@github.com:igzard/ncore.git composer install + +cd tools/php-cs-fixer +composer install ``` For running tests: diff --git a/src/Client/NcoreClient.php b/src/Client/NcoreClient.php index 8c4a4cc..bc6fe2e 100644 --- a/src/Client/NcoreClient.php +++ b/src/Client/NcoreClient.php @@ -30,7 +30,7 @@ public function search(Search $search): ResponseInterface { $options = [ 's='.$search->getSearch(), - 'cat='.$search->getCat()->value(), + 'cat='.$search->getCategory()->value(), ]; try { diff --git a/src/Entity/Factory/SearchFactory.php b/src/Entity/Factory/SearchFactory.php new file mode 100644 index 0000000..7b4bba8 --- /dev/null +++ b/src/Entity/Factory/SearchFactory.php @@ -0,0 +1,22 @@ +setSearch($searchPayload['search']); + + if (isset($searchPayload['category'])) { + $search->setCategory($searchPayload['category']); + } + + return $search; + } +} diff --git a/src/Entity/Search.php b/src/Entity/Search.php index 84de36c..c5cd183 100644 --- a/src/Entity/Search.php +++ b/src/Entity/Search.php @@ -9,7 +9,7 @@ class Search { private string $search = ''; - private Category $cat = Category::FILM_HUN_SD; + private Category $category = Category::FILM_HUN_SD; public function getSearch(): string { @@ -23,14 +23,14 @@ public function setSearch(string $search): self return $this; } - public function getCat(): Category + public function getCategory(): Category { - return $this->cat; + return $this->category; } - public function setCat(Category $cat): self + public function setCategory(Category $category): self { - $this->cat = $cat; + $this->category = $category; return $this; } diff --git a/src/Exception/EmptyResponseException.php b/src/Exception/EmptyResponseException.php new file mode 100644 index 0000000..4ec3b0b --- /dev/null +++ b/src/Exception/EmptyResponseException.php @@ -0,0 +1,13 @@ +rssParser = new RssParser(); $this->downloader = new Downloader(); $this->torrentCollectionFactory = new TorrentCollectionFactory(); + $this->searchFactory = new SearchFactory(); } /** * @throws RequestException * @throws ClientException */ - public function search(Search $search): TorrentCollection + public function search(array $search): TorrentCollection { return $this->torrentCollectionFactory->createFromXml( - $this->rssParser->parse($this->client->search($search)), + $this->rssParser->parse($this->client->search( + $this->searchFactory->createFromArray($search) + )), $this->passkey ); } @@ -55,8 +59,12 @@ public function search(Search $search): TorrentCollection * @throws RequestException * @throws ClientException */ - public function download(Search $search, string $path, string $filename): void + public function download(array $search, string $path, string $filename): void { - $this->downloader->download($path, $filename, $this->search($search)->first()->getLink()); + $this->downloader->download( + $path, + $filename, + $this->search($search)->first()->getLink() + ); } } diff --git a/src/Parser/RssParser.php b/src/Parser/RssParser.php index c573c87..ff47a3f 100644 --- a/src/Parser/RssParser.php +++ b/src/Parser/RssParser.php @@ -4,12 +4,22 @@ namespace Igzard\Ncore\Parser; +use Igzard\Ncore\Exception\EmptyResponseException; use Psr\Http\Message\ResponseInterface; class RssParser { + /** + * @throws EmptyResponseException + */ public function parse(ResponseInterface $response): \SimpleXMLElement { - return simplexml_load_string($response->getBody()->getContents()); + $content = $response->getBody()->getContents(); + + if ('' === $content) { + throw EmptyResponseException::create(); + } + + return simplexml_load_string($content); } } diff --git a/tests/Entity/Factory/SearchFactoryTest.php b/tests/Entity/Factory/SearchFactoryTest.php new file mode 100644 index 0000000..2833b4c --- /dev/null +++ b/tests/Entity/Factory/SearchFactoryTest.php @@ -0,0 +1,60 @@ +searchFactory = new SearchFactory(); + } + + /** + * @dataProvider searchDataProvider + */ + public function testFactory(array $searchPayload, Search $expectedSearch): void + { + $search = $this->searchFactory->createFromArray($searchPayload); + + $this->assertEquals($expectedSearch, $search); + } + + public static function searchDataProvider(): array + { + return [ + 'case 1# - ToyStory in FILM HUN SD category' => [ + 'searchPayload' => [ + 'search' => 'Toy Story', + 'category' => Category::FILM_HUN_SD, + ], + 'expectedSearch' => (new Search()) + ->setSearch('Toy Story') + ->setCategory(Category::FILM_HUN_SD) + ], + 'case 2# - ToyStory without category' => [ + 'searchPayload' => [ + 'search' => 'Toy Story', + ], + 'expectedSearch' => (new Search()) + ->setSearch('Toy Story') + ->setCategory(Category::FILM_HUN_SD) + ], + 'case 3# - Spiderman in category' => [ + 'searchPayload' => [ + 'search' => 'Toy Story', + 'category' => Category::FILM_ENG_HD, + ], + 'expectedSearch' => (new Search()) + ->setSearch('Toy Story') + ->setCategory(Category::FILM_ENG_HD) + ], + ]; + } +} \ No newline at end of file diff --git a/tests/NcoreTest.php b/tests/NcoreTest.php index f07d157..1d5ac65 100644 --- a/tests/NcoreTest.php +++ b/tests/NcoreTest.php @@ -7,6 +7,7 @@ use GuzzleHttp\Exception\GuzzleException; use Igzard\Ncore\Entity\Search; use Igzard\Ncore\Enum\Category; +use Igzard\Ncore\Exception\ClientException; use Igzard\Ncore\Exception\EmptyPasskeyException; use Igzard\Ncore\Exception\RequestException; use Igzard\Ncore\Ncore; @@ -22,7 +23,7 @@ protected function setUp(): void } /** - * @throws GuzzleException + * @throws ClientException * @throws RequestException */ public function testPasskeyIsEmpty(): void @@ -30,8 +31,9 @@ public function testPasskeyIsEmpty(): void $this->expectException(EmptyPasskeyException::class); $this->ncore = new Ncore(''); - $this->ncore->search((new Search()) - ->setSearch('Toy Story') - ->setCat(Category::FILM_HUN_SD)); + $this->ncore->search([ + 'search' => 'Toy Story', + 'category' => Category::FILM_HUN_SD + ]); } } \ No newline at end of file