Skip to content

Commit

Permalink
Merge pull request #17 from igzard/refactor/remove-search-dependency
Browse files Browse the repository at this point in the history
feat: remove search dependency from init
  • Loading branch information
igzard authored Jun 26, 2024
2 parents b5c2faf + 79c7244 commit 92b9417
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 24 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
);
Expand All @@ -48,6 +52,9 @@ Thank you for considering contributing to the PHP Ncore! To contribution follow
```bash
git clone [email protected]:igzard/ncore.git
composer install

cd tools/php-cs-fixer
composer install
```

For running tests:
Expand Down
2 changes: 1 addition & 1 deletion src/Client/NcoreClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function search(Search $search): ResponseInterface
{
$options = [
's='.$search->getSearch(),
'cat='.$search->getCat()->value(),
'cat='.$search->getCategory()->value(),
];

try {
Expand Down
22 changes: 22 additions & 0 deletions src/Entity/Factory/SearchFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Igzard\Ncore\Entity\Factory;

use Igzard\Ncore\Entity\Search;

class SearchFactory
{
public function createFromArray(array $searchPayload): Search
{
$search = new Search();
$search->setSearch($searchPayload['search']);

if (isset($searchPayload['category'])) {
$search->setCategory($searchPayload['category']);
}

return $search;
}
}
10 changes: 5 additions & 5 deletions src/Entity/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
}
Expand Down
13 changes: 13 additions & 0 deletions src/Exception/EmptyResponseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Igzard\Ncore\Exception;

class EmptyResponseException extends \Exception
{
public static function create(): self
{
return new self('Empty response received');
}
}
18 changes: 13 additions & 5 deletions src/Ncore.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Igzard\Ncore\Client\NcoreClient;
use Igzard\Ncore\Entity\Collection\Factory\TorrentCollectionFactory;
use Igzard\Ncore\Entity\Collection\TorrentCollection;
use Igzard\Ncore\Entity\Search;
use Igzard\Ncore\Entity\Factory\SearchFactory;
use Igzard\Ncore\Exception\ClientException;
use Igzard\Ncore\Exception\EmptyPasskeyException;
use Igzard\Ncore\Exception\RequestException;
Expand All @@ -23,6 +23,7 @@ class Ncore
private RssParser $rssParser;
private Downloader $downloader;
private TorrentCollectionFactory $torrentCollectionFactory;
private SearchFactory $searchFactory;

/**
* @throws EmptyPasskeyException
Expand All @@ -37,16 +38,19 @@ public function __construct(string $passkey)
$this->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
);
}
Expand All @@ -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()
);
}
}
12 changes: 11 additions & 1 deletion src/Parser/RssParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
60 changes: 60 additions & 0 deletions tests/Entity/Factory/SearchFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Igzard\Ncore\Tests\Entity\Factory;

use Igzard\Ncore\Entity\Factory\SearchFactory;
use Igzard\Ncore\Entity\Search;
use Igzard\Ncore\Enum\Category;
use PHPUnit\Framework\TestCase;

class SearchFactoryTest extends TestCase
{
protected function setUp(): void
{
$this->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)
],
];
}
}
10 changes: 6 additions & 4 deletions tests/NcoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,16 +23,17 @@ protected function setUp(): void
}

/**
* @throws GuzzleException
* @throws ClientException
* @throws RequestException
*/
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
]);
}
}

0 comments on commit 92b9417

Please sign in to comment.