minulislam /
multicoin-api
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Multicoin\Api; |
||
| 4 | |||
| 5 | use InvalidArgumentException; |
||
| 6 | use Multicoin\Api\Multicoin as Multicoincurrency; |
||
| 7 | |||
| 8 | class MulticoinFactory |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * currency configurations. |
||
| 12 | * |
||
| 13 | * @var array |
||
| 14 | */ |
||
| 15 | protected $config; |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 16 | |||
| 17 | /** |
||
| 18 | * currency instances. |
||
| 19 | * |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | protected $currencys = []; |
||
|
0 ignored issues
–
show
|
|||
| 23 | |||
| 24 | /** |
||
| 25 | * Constructs currency factory instance. |
||
| 26 | * |
||
| 27 | * @param array $config |
||
| 28 | * @return void |
||
| 29 | */ |
||
| 30 | public function __construct(array $config) |
||
| 31 | { |
||
| 32 | $this->config = $config; |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Pass methods onto the default currency. |
||
| 37 | * |
||
| 38 | * @param string $method |
||
| 39 | * @param array $parameters |
||
| 40 | * @return mixed |
||
| 41 | */ |
||
| 42 | public function __call(string $method, array $parameters) |
||
| 43 | { |
||
| 44 | return $this->currency()->{$method}(...$parameters); |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Gets currency instance by name or creates if not exists. |
||
| 49 | * |
||
| 50 | * @param string $name |
||
| 51 | * @return \Multicoin\Api\Multicoin |
||
| 52 | */ |
||
| 53 | public function currency(string $name = 'BTC'): Multicoincurrency |
||
| 54 | { |
||
| 55 | if (! array_key_exists($name, $this->currencys)) { |
||
| 56 | $config = $this->getConfig($name); |
||
| 57 | |||
| 58 | $this->currencys[$name] = $this->make($config); |
||
| 59 | } |
||
| 60 | |||
| 61 | return $this->currencys[$name]; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Gets currency config by name. |
||
| 66 | * |
||
| 67 | * @param string $name |
||
| 68 | * @return array |
||
| 69 | */ |
||
| 70 | public function getConfig(string $name = 'BTC'): array |
||
| 71 | { |
||
| 72 | $flip_currency = array_flip($this->config['currency']); |
||
|
0 ignored issues
–
show
|
|||
| 73 | |||
| 74 | if (! array_key_exists($name, $flip_currency)) { |
||
|
0 ignored issues
–
show
|
|||
| 75 | throw new InvalidArgumentException( |
||
| 76 | "Could not find currency configuration [$name]" |
||
|
0 ignored issues
–
show
As per coding-style, please use concatenation or
sprintf for the variable $name instead of interpolation.
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings. // Instead of
$x = "foo $bar $baz";
// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
|
|||
| 77 | ); |
||
| 78 | } |
||
| 79 | |||
| 80 | return $this->withDefaults($this->config, $name); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Creates currency instance. |
||
| 85 | * |
||
| 86 | * @param array $config |
||
| 87 | * @return \Multicoin\Api\Multicoin |
||
| 88 | */ |
||
| 89 | public function make(array $config = []): Multicoincurrency |
||
| 90 | { |
||
| 91 | return new Multicoincurrency($config); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Appends configuration array with default values. |
||
| 96 | * |
||
| 97 | * @param array $config |
||
| 98 | * @return array |
||
| 99 | */ |
||
| 100 | protected function withDefaults(array $config, string $name): array |
||
| 101 | { |
||
| 102 | return array_merge(['coin' => $name], $config); |
||
| 103 | } |
||
| 104 | } |
||
| 105 |