Passed
Push — master ( fc2f89...038734 )
by compolom
02:33
created

Client   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Test Coverage

Coverage 77.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
c 1
b 0
f 0
dl 0
loc 171
ccs 31
cts 40
cp 0.775
rs 10
wmc 15

6 Methods

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

133
            echo /** @scrutinizer ignore-type */ $e->getTrace();
Loading history...
134
        }
135
136 8
        if ($object === '') {
137
            throw new BadMethodCallException('Wrong type of object');
138
        }
139
140 8
        return $object;
141
    }
142
143
    /**
144
     * Magic method required for call of another classes
145
     *
146
     * @param string $name
147
     * @param array  $arguments
148
     * @return bool|object
149
     */
150 7
    public function __call(string $name, array $arguments)
151
    {
152
        // By default return is empty
153 7
        $object = '';
154
155
        try {
156
157
            // Set class name as namespace
158 7
            $class = $this->namespace . '\\' . $this->snakeToPascal($name) . 's';
159
160
            // Try to create object by name
161 7
            $object = new $class($this->config);
162
163
            // If object is not created
164 7
            if (!is_object($object)) {
165 7
                throw new BadMethodCallException("Class $class could not to be loaded");
166
            }
167
168
        } catch (\Exception $e) {
169
            echo $e->getMessage() . "\n";
170
            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

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