|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Muffin\Webservice\Datasource; |
| 5 | + |
| 6 | +use Cake\Core\App; |
| 7 | +use Muffin\Webservice\Datasource\Exception\MissingConnectionException; |
| 8 | +use Muffin\Webservice\Webservice\Driver\AbstractDriver; |
| 9 | +use Muffin\Webservice\Webservice\Exception\MissingDriverException; |
| 10 | +use Muffin\Webservice\Webservice\Exception\UnexpectedDriverException; |
| 11 | + |
| 12 | +/** |
| 13 | + * Class Connection |
| 14 | + * |
| 15 | + * @method \Muffin\Webservice\Webservice\Driver\AbstractDriver setWebservice(string $name, \Muffin\Webservice\Webservice\WebserviceInterface $webservice) Proxy method through to the Driver |
| 16 | + * @method \Muffin\Webservice\Webservice\WebserviceInterface getWebservice(string $name) Proxy method through to the Driver |
| 17 | + * @method string configName() Proxy method through to the Driver |
| 18 | + */ |
| 19 | +class Connection |
| 20 | +{ |
| 21 | + /** |
| 22 | + * Driver |
| 23 | + * |
| 24 | + * @var \Muffin\Webservice\Webservice\Driver\AbstractDriver |
| 25 | + */ |
| 26 | + protected $_driver; |
| 27 | + |
| 28 | + /** |
| 29 | + * Constructor |
| 30 | + * |
| 31 | + * @param array $config Custom configuration. |
| 32 | + * @throws \Muffin\Webservice\Webservice\Exception\UnexpectedDriverException If the driver is not an instance of `Muffin\Webservice\AbstractDriver`. |
| 33 | + */ |
| 34 | + public function __construct(array $config) |
| 35 | + { |
| 36 | + $config = $this->_normalizeConfig($config); |
| 37 | + /** @psalm-var class-string<\Muffin\Webservice\Webservice\Driver\AbstractDriver> */ |
| 38 | + $driver = $config['driver']; |
| 39 | + unset($config['driver'], $config['service']); |
| 40 | + |
| 41 | + $this->_driver = new $driver($config); |
| 42 | + |
| 43 | + /** @psalm-suppress TypeDoesNotContainType */ |
| 44 | + if (!($this->_driver instanceof AbstractDriver)) { |
| 45 | + throw new UnexpectedDriverException(['driver' => $driver]); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Validates certain custom configuration values. |
| 51 | + * |
| 52 | + * @param array $config Raw custom configuration. |
| 53 | + * @return array |
| 54 | + * @throws \Muffin\Webservice\Datasource\Exception\MissingConnectionException If the connection does not exist. |
| 55 | + * @throws \Muffin\Webservice\Webservice\Exception\MissingDriverException If the driver does not exist. |
| 56 | + */ |
| 57 | + protected function _normalizeConfig(array $config): array |
| 58 | + { |
| 59 | + if (empty($config['driver'])) { |
| 60 | + if (empty($config['service'])) { |
| 61 | + throw new MissingConnectionException(['name' => $config['name']]); |
| 62 | + } |
| 63 | + |
| 64 | + $config['driver'] = App::className($config['service'], 'Webservice/Driver'); |
| 65 | + if (!$config['driver']) { |
| 66 | + throw new MissingDriverException(['driver' => $config['driver']]); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return $config; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Proxies the driver's methods. |
| 75 | + * |
| 76 | + * @param string $method Method name. |
| 77 | + * @param array $args Arguments to pass-through |
| 78 | + * @return mixed |
| 79 | + */ |
| 80 | + public function __call($method, $args) |
| 81 | + { |
| 82 | + return call_user_func_array([$this->_driver, $method], $args); |
| 83 | + } |
| 84 | +} |
0 commit comments