| 1: | <?php |
| 2: | |
| 3: | namespace Scopus; |
| 4: | |
| 5: | use GuzzleHttp\Client; |
| 6: | |
| 7: | class ScopusApiFactory |
| 8: | { |
| 9: | const TIMEOUT_DEFAULT = 40; |
| 10: | |
| 11: | private $apiKey; |
| 12: | private $institutionToken; |
| 13: | private $timeout; |
| 14: | |
| 15: | public function __construct(string $apiKey, string $institutionToken = null, int $timeout = self::TIMEOUT_DEFAULT) |
| 16: | { |
| 17: | $this->apiKey = $apiKey; |
| 18: | $this->institutionToken = $institutionToken; |
| 19: | $this->timeout = $timeout; |
| 20: | } |
| 21: | |
| 22: | public function createApiClient(): ScopusApi |
| 23: | { |
| 24: | $headers = [ |
| 25: | 'Accept' => 'application/json', |
| 26: | 'X-ELS-APIKey' => $this->apiKey, |
| 27: | ]; |
| 28: | |
| 29: | if ($this->institutionToken !== null) { |
| 30: | $headers['X-ELS-Insttoken'] = $this->institutionToken; |
| 31: | } |
| 32: | |
| 33: | return new ScopusApi( |
| 34: | new Client([ |
| 35: | 'timeout' => $this->timeout, |
| 36: | 'headers' => $headers, |
| 37: | ]) |
| 38: | ); |
| 39: | } |
| 40: | } |
| 41: |