HasSubInvoker::__get()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 10
c 2
b 0
f 0
dl 0
loc 18
rs 9.6111
cc 5
nc 4
nop 1
1
<?php
2
3
namespace CloudyCity\KuaishouMarketingSDK\Kernel\Traits;
4
5
use CloudyCity\KuaishouMarketingSDK\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
                $advertiserId = $this->getAdvertiserId();
37
                $accessToken = $this->getAccessToken();
38
                $responseType = $this->getResponseType();
39
40
                return new $this->providers[$name]($advertiserId, $accessToken, $responseType);
41
            }
42
43
            throw new Exception("Undefined property $name", 500);
44
        }
45
46
        return $this->$name;
47
    }
48
}
49