Completed
Push — master ( 6387a2...3f53e4 )
by Sercan
02:30
created

GatewayFactory::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace CryptoMarkets\Common;
4
5
use InvalidArgumentException;
6
7
class GatewayFactory
8
{
9
    /**
10
     * Create an instance of the specified market driver.
11
     *
12
     * @param  string  $abstract
13
     * @param  array  $options
14
     * @return \CryptoMarkets\Common\Gateway
15
     *
16
     * @throws \InvalidArgumentException
17
     */
18
    public function create($abstract, array $options = [])
19
    {
20
        $concrete = $this->getConcrete($abstract);
21
22
        if (class_exists($concrete)) {
23
            return new $concrete($options);
24
        }
25
26
        throw new InvalidArgumentException("The gateway of [$abstract] not supported.");
27
    }
28
29
    /**
30
     * Get the concrete type for a given abstract.
31
     *
32
     * @param  string  $abstract
33
     * @return string
34
     */
35
    public function getConcrete($abstract)
36
    {
37
        if (0 === strpos($abstract, '\\')) {
38
            return $abstract;
39
        }
40
41
        $abstract = str_replace('_', '\\', $abstract);
42
43
        if (false === strpos($abstract, '\\')) {
44
            $abstract .= '\\';
45
        }
46
47
        return '\\CryptoMarkets\\'.$abstract.'Gateway';
48
    }
49
}
50