Dispositif /
Wikibot
| 1 | <?php |
||
| 2 | /* |
||
| 3 | * This file is part of dispositif/wikibot application (@github) |
||
| 4 | * 2019-2023 © Philippe M./Irønie <[email protected]> |
||
| 5 | * For the full copyright and MIT license information, view the license file. |
||
| 6 | */ |
||
| 7 | |||
| 8 | declare(strict_types=1); |
||
| 9 | |||
| 10 | namespace App\Infrastructure; |
||
| 11 | |||
| 12 | use App\Domain\InfrastructurePorts\BookApiInterface; |
||
| 13 | use App\Domain\InfrastructurePorts\GoogleBooksInterface; |
||
| 14 | use App\Domain\Publisher\GoogleBookMapper; |
||
| 15 | use Exception; |
||
| 16 | use Scriptotek\GoogleBooks\GoogleBooks as GoogleAPI; |
||
| 17 | use Scriptotek\GoogleBooks\Volume; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * See https://github.com/scriptotek/php-google-books package. |
||
| 21 | * Class GoogleBooksAdapter. |
||
| 22 | */ |
||
| 23 | class GoogleBooksAdapter extends AbstractBookApiAdapter implements GoogleBooksInterface, BookApiInterface |
||
| 24 | { |
||
| 25 | final public const SCRIPT_GOOGLE_QUOTA = 900; |
||
| 26 | final public const SCRIPT_GOOGLE_COUNTRY = 'US'; |
||
| 27 | |||
| 28 | protected $api; |
||
| 29 | |||
| 30 | protected $mapper; |
||
| 31 | |||
| 32 | // todo inject + factory |
||
| 33 | private readonly GoogleApiQuota $quotaCounter; |
||
| 34 | |||
| 35 | public function __construct() |
||
| 36 | { |
||
| 37 | $api = new GoogleAPI( |
||
| 38 | [ |
||
| 39 | 'key' => getenv('GOOGLE_BOOKS_API_KEY'), |
||
| 40 | 'maxResults' => 5, |
||
| 41 | 'country' => self::SCRIPT_GOOGLE_COUNTRY, |
||
| 42 | ] |
||
| 43 | ); |
||
| 44 | // 'country' => 'FR' (ISO-3166 Country Codes?) |
||
| 45 | $this->api = $api; |
||
| 46 | $this->mapper = new GoogleBookMapper(); |
||
| 47 | $this->quotaCounter = new GoogleApiQuota(); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 48 | } |
||
| 49 | |||
| 50 | public function getDataByIsbn(string $isbn): ?Volume |
||
| 51 | { |
||
| 52 | $this->checkGoogleQuota(); |
||
| 53 | $res = $this->api->volumes->byIsbn($isbn); |
||
| 54 | if ($res !== null) { |
||
| 55 | $this->quotaCounter->increment(); |
||
| 56 | } |
||
| 57 | |||
| 58 | return $res; |
||
| 59 | } |
||
| 60 | |||
| 61 | private function checkGoogleQuota() |
||
| 62 | { |
||
| 63 | if ($this->quotaCounter->getCount() > self::SCRIPT_GOOGLE_QUOTA) { |
||
| 64 | throw new Exception('Quota Google dépassé pour ce script : '.self::SCRIPT_GOOGLE_QUOTA); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * |
||
| 70 | * @return Volume |
||
| 71 | * @throws Exception |
||
| 72 | */ |
||
| 73 | public function getDataByGoogleId(string $googleId) |
||
| 74 | { |
||
| 75 | $this->checkGoogleQuota(); |
||
| 76 | $res = $this->api->volumes->get($googleId); |
||
| 77 | if ($res !== null) { |
||
| 78 | $this->quotaCounter->increment(); |
||
| 79 | } |
||
| 80 | |||
| 81 | return $res; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @return array |
||
| 86 | */ |
||
| 87 | public function search(string $query) |
||
| 88 | { |
||
| 89 | $data = []; |
||
| 90 | foreach ($this->api->volumes->search($query) as $vol) { |
||
| 91 | dump($vol->title); |
||
| 92 | $data[] = $vol; |
||
| 93 | } |
||
| 94 | |||
| 95 | return $data; |
||
| 96 | } |
||
| 97 | } |
||
| 98 |