HasSubInvoker::__get()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 4
nop 1
dl 0
loc 20
rs 9.2888
c 0
b 0
f 0
1
<?php
2
3
namespace CloudyCity\UCMarketingSDK\Kernel\Traits;
4
5
use CloudyCity\UCMarketingSDK\Kernel\Exceptions\Exception;
6
7
/**
8
 * Trait HasSubInvoker.
9
 *
10
 * @property array $providers
11
 */
12
trait HasSubInvoker
13
{
14
    use HasSdkBaseInfo;
15
16
    protected $instances = [];
17
18
    abstract public function getResponseType();
19
20
    abstract public function setResponseType($responseType);
21
22
    /**
23
     * @param $name
24
     *
25
     * @throws Exception
26
     *
27
     * @return mixed
28
     */
29
    public function __get($name)
30
    {
31
        if (!property_exists($this, $name)) {
32
            if (array_key_exists($name, $this->instances)) {
33
                return $this->instances[$name];
34
            }
35
            if (property_exists($this, 'providers') && array_key_exists($name, $this->providers)) {
36
                $username = $this->getUsername();
37
                $password = $this->getPassword();
38
                $token = $this->getToken();
39
                $responseType = $this->getResponseType();
40
41
                return new $this->providers[$name]($username, $password, $token, $responseType);
42
            }
43
44
            throw new Exception("Undefined property $name", 500);
45
        }
46
47
        return $this->$name;
48
    }
49
}
50