Completed
Push — master ( 8e0025...04518a )
by
05:55
created

Facade::__callStatic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the PhpMob package.
5
 *
6
 * (c) Ishmael Doss <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace PhpMob\Omise;
13
14
/**
15
 * @author Ishmael Doss <[email protected]>
16
 */
17
abstract class Facade
18
{
19
    /**
20
     * @var Api[]
21
     */
22
    protected static $api = [];
23
24
    /**
25
     * @var Model
26
     */
27
    protected $domain;
28
29
    /**
30
     * @param array $data
31
     */
32
    public function __construct(array $data = [])
33
    {
34
        $class = str_replace('Facade', 'Domain', get_called_class());
35
36
        $this->domain = new $class($data);
37
    }
38
39
    /**
40
     * @param array $domain
41
     *
42
     * @return static
43
     */
44
    public static function make(array $domain = [])
45
    {
46
        return new static($domain);
47
    }
48
49
    /**
50
     * @param Api $api
51
     */
52
    public static function setApi(Api $api)
53
    {
54
        self::$api[get_called_class()] = $api;
55
    }
56
57
    /**
58
     * @return Api
59
     */
60
    private static function getApiForClass()
61
    {
62
        return self::$api[get_called_class()];
63
    }
64
65
    /**
66
     * @param $method
67
     */
68
    private static function checkApiMethodExists($method)
69
    {
70
        if (!method_exists(self::getApiForClass(), $method)) {
71
            throw new \InvalidArgumentException(
72
                sprintf("Not found method named `%s` for `%s` api.", $method, get_called_class())
73
            );
74
        }
75
    }
76
77
    /**
78
     * @param string $method
79
     * @param array $args
80
     *
81
     * @return mixed|self
82
     */
83
    public static function __callStatic($method, $args)
84
    {
85
        self::checkApiMethodExists($method);
86
87
        return self::getApiForClass()->$method(...$args);
88
    }
89
90
    /**
91
     * @param $method
92
     * @param $args
93
     *
94
     * @return mixed
95
     */
96
    public function __call($method, $args)
97
    {
98
        if (method_exists($this->domain, $method)) {
99
            return $this->domain->$method(...$args);
100
        }
101
102
        self::checkApiMethodExists($method);
103
104
        return self::getApiForClass()->$method($this->domain, ...$args);
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function __get($name)
111
    {
112
        $result = $this->domain->$name;
113
114
        /**
115
         * No need due to `FacadeHydration` implementation.
116
         * if ($result instanceof Model) {
117
         *   $class = str_replace('Domain', 'Facade', get_class($result));
118
         *
119
         *   return new $class($result->toArray());
120
         * }
121
         */
122
123
        return $result;
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function __set($name, $value)
130
    {
131
        $this->domain->$name = $value;
132
    }
133
}
134