guillermoandrae /
php-highrise-api
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Guillermoandrae\Highrise\Client; |
||
| 4 | |||
| 5 | use BadMethodCallException; |
||
| 6 | use Guillermoandrae\Highrise\Http\AdapterAwareTrait; |
||
| 7 | use Guillermoandrae\Highrise\Http\AdapterInterface; |
||
| 8 | use Guillermoandrae\Highrise\Http\CredentialsAwareTrait; |
||
| 9 | use Guillermoandrae\Highrise\Http\GuzzleAdapter; |
||
| 10 | use Guillermoandrae\Highrise\Repositories; |
||
| 11 | use Guillermoandrae\Highrise\Repositories\RepositoryInterface; |
||
| 12 | use Guillermoandrae\Repositories\RepositoryFactory; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Highrise API Client class. |
||
| 16 | * |
||
| 17 | * @method Repositories\AccountRepository account() |
||
| 18 | * @method Repositories\CasesRepository cases() |
||
| 19 | * @method Repositories\DealsRepository deals() |
||
| 20 | * @method Repositories\EmailsRepository emails() |
||
| 21 | * @method Repositories\PeopleRepository people() |
||
| 22 | * @method Repositories\TasksRepository tasks() |
||
| 23 | * @method Repositories\UsersRepository users() |
||
| 24 | * |
||
| 25 | * @see https://github.com/basecamp/highrise-api |
||
| 26 | * @author Guillermo A. Fisher <[email protected]> |
||
| 27 | */ |
||
| 28 | final class Client implements ClientInterface |
||
| 29 | { |
||
| 30 | use AdapterAwareTrait, CredentialsAwareTrait; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The default HTTP adapter. |
||
| 34 | * |
||
| 35 | * @var AdapterInterface |
||
| 36 | */ |
||
| 37 | private $defaultAdapter; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Client constructor. |
||
| 41 | * |
||
| 42 | * @param string $subdomain The account subdomain. |
||
| 43 | * @param string $token The authentication token. |
||
| 44 | 1 | */ |
|
| 45 | public function __construct(string $subdomain = '', string $token = '') |
||
| 46 | 1 | { |
|
| 47 | 1 | $this->setSubdomain($subdomain); |
|
| 48 | 1 | $this->setToken($token); |
|
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Invoked when non-existent methods are called. Can be used for returning |
||
| 53 | * API resources. |
||
| 54 | * |
||
| 55 | * @param string $name The method name. |
||
| 56 | * @param array $arguments The method arguments. |
||
| 57 | * @return mixed |
||
| 58 | 1 | */ |
|
| 59 | public function __call($name, $arguments) |
||
| 60 | { |
||
| 61 | 1 | try { |
|
| 62 | 1 | return $this->resource($name); |
|
| 63 | 1 | } catch (\Exception $ex) { |
|
| 64 | 1 | throw new BadMethodCallException( |
|
| 65 | sprintf('The %s method does not exist.', $name) |
||
| 66 | ); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | 1 | ||
| 70 | public function resource(string $name): RepositoryInterface |
||
| 71 | 1 | { |
|
| 72 | 1 | if (!$client = $this->adapter) { |
|
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 73 | $this->setAdapter($this->getDefaultAdapter()); |
||
| 74 | 1 | } |
|
| 75 | return RepositoryFactory::factory($name, $this->getAdapter()); |
||
|
0 ignored issues
–
show
|
|||
| 76 | } |
||
| 77 | 1 | ||
| 78 | public function getDefaultAdapter(): AdapterInterface |
||
| 79 | 1 | { |
|
| 80 | 1 | if (!$this->defaultAdapter) { |
|
| 81 | $this->defaultAdapter = new GuzzleAdapter($this->getSubdomain(), $this->getToken()); |
||
| 82 | 1 | } |
|
| 83 | return $this->defaultAdapter; |
||
| 84 | } |
||
| 85 | } |
||
| 86 |