Completed
Push — master ( 40ad21...af053b )
by Mr
02:05
created

Client::__set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Resova;
4
5
use BadMethodCallException;
6
use ErrorException;
7
use Exception;
8
use GuzzleHttp\Exception\ClientException;
9
use Resova\Endpoints\Availability;
10
use Resova\Endpoints\Baskets;
11
use Resova\Endpoints\Customers;
12
use Resova\Endpoints\GiftVouchers;
13
use Resova\Endpoints\Items;
14
15
/**
16
 * @property Availability $availability  Availability of time slots
17
 * @property Baskets      $baskets       Baskets management
18
 * @property Customers    $customers     Customers management
19
 * @property GiftVouchers $gift_vouchers GiftVouchers management
20
 * @property Items        $items         For work with list of items
21
 *
22
 * @method Baskets      basket(int $basket_id)
23
 * @method Customers    customer(int $customer_id)
24
 * @method GiftVouchers gift_voucher(int $gift_voucher_id)
25
 * @method Items        item(int $item_id)
26
 *
27
 * Single entry point for all classes
28
 *
29
 * @package Resova
30
 */
31
class Client
32
{
33
    use HttpTrait;
34
35
    /**
36
     * @var string
37
     */
38
    protected $namespace = __NAMESPACE__ . '\\Endpoints';
39
40
    /**
41
     * Type of query
42
     *
43
     * @var string
44
     */
45
    protected $type;
46
47
    /**
48
     * Endpoint of query
49
     *
50
     * @var string
51
     */
52
    protected $endpoint;
53
54
    /**
55
     * Parameters of query
56
     *
57
     * @var mixed
58
     */
59
    protected $params;
60
61
    /**
62
     * @var array
63
     */
64
    protected static $variables = [];
65
66
    /**
67
     * API constructor.
68
     *
69
     * @param string|array|Config $config
70
     * @throws ErrorException
71
     */
72 10
    public function __construct($config)
73
    {
74
        // If string then it's a token
75 10
        if (is_string($config)) {
76 1
            $config = new Config(['api_key' => $config]);
77
        }
78
79
        // If array then need create object
80 10
        if (is_array($config)) {
81 9
            $config = new Config($config);
82
        }
83
84
        // Save config into local variable
85 10
        $this->config = $config;
86
87
        // Store the client object
88 10
        $this->client = new \GuzzleHttp\Client($config->guzzle());
89 10
    }
90
91
    /**
92
     * Convert underscore_strings to camelCase (medial capitals).
93
     *
94
     * @param string $str
95
     *
96
     * @return string
97
     */
98 8
    private function snakeToPascal(string $str): string
99
    {
100
        // Remove underscores, capitalize words, squash, lowercase first.
101 8
        return str_replace(' ', '', ucwords(str_replace('_', ' ', $str)));
102
    }
103
104
    /**
105
     * Magic method required for call of another classes
106
     *
107
     * @param string $name
108
     *
109
     * @return bool|object
110
     * @throws BadMethodCallException
111
     */
112 8
    public function __get(string $name)
113
    {
114 8
        if (isset(self::$variables[$name])) {
115 2
            return self::$variables[$name];
116
        }
117
118
        // By default return is empty
119 8
        $object = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $object is dead and can be removed.
Loading history...
120
121
        // Set class name as namespace
122 8
        $class = $this->namespace . '\\' . $this->snakeToPascal($name);
123
124
        try {
125
126
            // Try to create object by name
127 8
            $object = new $class($this->config);
128
129
        } catch (ErrorException | ClientException $e) {
130
            echo $e->getMessage() . "\n";
131
            echo $e->getTrace();
0 ignored issues
show
Bug introduced by
Are you sure $e->getTrace() of type array can be used in echo? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

131
            echo /** @scrutinizer ignore-type */ $e->getTrace();
Loading history...
132
        }
133
134
        // If object is not created
135 8
        if (!is_object($object)) {
136
            throw new BadMethodCallException("Class $class could not to be loaded");
137
        }
138
139 8
        return $object;
140
    }
141
142
    /**
143
     * Magic method required for call of another classes
144
     *
145
     * @param string $name
146
     * @param array  $arguments
147
     *
148
     * @return bool|object
149
     * @throws BadMethodCallException
150
     */
151 7
    public function __call(string $name, array $arguments)
152
    {
153
        // By default return is empty
154 7
        $object = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $object is dead and can be removed.
Loading history...
155
156
        // Set class name as namespace
157 7
        $class = $this->namespace . '\\' . $this->snakeToPascal($name) . 's';
158
159
        try {
160
161
            // Try to create object by name
162 7
            $object = new $class($this->config);
163
164
        } catch (ErrorException | ClientException $e) {
165
            echo $e->getMessage() . "\n";
166
            echo $e->getTrace();
0 ignored issues
show
Bug introduced by
Are you sure $e->getTrace() of type array can be used in echo? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

166
            echo /** @scrutinizer ignore-type */ $e->getTrace();
Loading history...
167
        }
168
169
        // If object is not created
170 7
        if (!is_object($object)) {
171
            throw new BadMethodCallException("Class $class could not to be loaded");
172
        }
173
174 7
        return call_user_func_array($object, $arguments);
0 ignored issues
show
Bug introduced by
$object of type object is incompatible with the type callable expected by parameter $function of call_user_func_array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

174
        return call_user_func_array(/** @scrutinizer ignore-type */ $object, $arguments);
Loading history...
175
    }
176
177
    /**
178
     * Check if class is exist in folder
179
     *
180
     * @param string $name
181
     * @return bool
182
     */
183 1
    public function __isset(string $name): bool
184
    {
185 1
        return isset(self::$variables[$name]);
186
    }
187
188
    /**
189
     * Ordinary dummy setter, it should be ignored (added to PSR reasons)
190
     *
191
     * @param string $name
192
     * @param mixed  $value
193
     * @throws BadMethodCallException
194
     */
195 7
    public function __set(string $name, $value)
196
    {
197 7
        self::$variables[$name] = $value;
198 7
    }
199
}
200