Passed
Push — master ( b5ce41...221eb9 )
by Mr
02:54
created

API::__isset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace UON;
4
5
use UON\Exceptions\APIException;
6
7
use UON\Endpoint\Bcard;
8
use UON\Endpoint\Cash;
9
use UON\Endpoint\Catalog;
10
use UON\Endpoint\Chat;
11
use UON\Endpoint\Cities;
12
use UON\Endpoint\Countries;
13
use UON\Endpoint\Hotels;
14
use UON\Endpoint\Leads;
15
use UON\Endpoint\Misc;
16
use UON\Endpoint\Nutrition;
17
use UON\Endpoint\Payments;
18
use UON\Endpoint\Reminders;
19
use UON\Endpoint\Requests;
20
use UON\Endpoint\Services;
21
use UON\Endpoint\Sources;
22
use UON\Endpoint\Statuses;
23
use UON\Endpoint\Suppliers;
24
use UON\Endpoint\Users;
25
26
/**
27
 * @property Bcard     $bcard      Bonus cards
28
 * @property Cash      $cash       Money operations
29
 * @property Catalog   $catalog    Catalog of products
30
 * @property Chat      $chat       For work with chat messages
31
 * @property Cities    $cities     Cities of countries
32
 * @property Countries $countries  Work with countries
33
 * @property Hotels    $hotels     Hotels methods
34
 * @property Leads     $leads      Details about clients
35
 * @property Misc      $misc       Optional single methods
36
 * @property Nutrition $nutrition  Some methods about eat
37
 * @property Payments  $payments   Payment methods
38
 * @property Reminders $reminders  Work with reminders
39
 * @property Requests  $requests   New requests from people
40
 * @property Sources   $sources    All available sources
41
 * @property Services  $services   All available sources
42
 * @property Statuses  $statuses   Request statuses
43
 * @property Suppliers $suppliers  External companies
44
 * @property Users     $users      For work with users
45
 *
46
 * Single entry point for all classes
47
 *
48
 * @package UON
49
 * @since   1.7
50
 */
51
class API
52
{
53
    /**
54
     * All parameters of this class
55
     *
56
     * @var Config
57
     */
58
    private $config;
59
60
    /**
61
     * API constructor, you can create this object just by providing "token" as string
62
     *
63
     * @param string|array|Config $config
64
     * @throws \UON\Exceptions\ConfigException
65
     */
66
    public function __construct($config)
67
    {
68
        // If string then it's a token
69
        if (\is_string($config)) {
70
            $config = new Config(['token' => $config]);
71
        }
72
73
        // If array then need create object
74
        if (\is_array($config)) {
75
            $config = new Config($config);
76
        }
77
78
        $this->config = $config;
79
    }
80
81
    /**
82
     * Magic method required for call of another classes
83
     *
84
     * @param string $name
85
     * @return bool|object
86
     */
87
    public function __get($name)
88
    {
89
        // By default return is false
90
        $object = false;
91
92
        try {
93
            // Extract token from config array
94
            $token = $this->config->get('token');
95
96
            // Check if token is not available
97
            if (false === $token) {
98
                throw new APIException('Token is not set');
99
            }
100
101
            // Generate dynamic name of class
102
            $class = __NAMESPACE__ . '\\Endpoint\\' . ucfirst(strtolower($name));
103
104
            // Try to create object by name
105
            $object = new $class($this->config);
106
107
            // If object is not created
108
            if (!is_object($object)) {
109
                throw new APIException("Class $class could not to be loaded");
110
            }
111
112
        } catch (APIException $e) {
113
            // __constructor
114
        }
115
116
        return $object;
117
    }
118
119
    /**
120
     * Check if class is exist in folder
121
     *
122
     * @param string $name
123
     * @return bool
124
     */
125
    public function __isset($name)
126
    {
127
        $path = __DIR__ . '/Endpoint';
128
        return current(preg_grep("/$name.php$/i", glob("$path/*")));
0 ignored issues
show
Bug introduced by
It seems like glob($path.'/*') can also be of type false; however, parameter $input of preg_grep() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

128
        return current(preg_grep("/$name.php$/i", /** @scrutinizer ignore-type */ glob("$path/*")));
Loading history...
129
    }
130
131
    /**
132
     * Ordinary dummy setter, it should be ignored (added to PSR reasons)
133
     *
134
     * @param string $name
135
     * @param mixed  $value
136
     * @throws APIException
137
     */
138
    public function __set($name, $value)
139
    {
140
        throw new APIException('Unable to write into this place');
141
    }
142
}
143