HasSubInvoker   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 15
c 1
b 0
f 0
dl 0
loc 40
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __get() 0 23 6
1
<?php
2
3
namespace CloudyCity\TencentMarketingSDK\Kernel\Traits;
4
5
use CloudyCity\TencentMarketingSDK\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
36
            if (property_exists($this, 'providers') && array_key_exists($name, $this->providers)) {
37
                $advertiserId = $this->getAdvertiserId();
38
                $accessToken = $this->getAccessToken();
39
                $responseType = $this->getResponseType();
40
41
                return new $this->providers[$name]($advertiserId, $accessToken, $responseType);
42
            }
43
44
            if (method_exists($this, 'factory')) {
45
                return $this->factory($name);
46
            }
47
48
            throw new Exception("Undefined property $name", 500);
49
        }
50
51
        return $this->$name;
52
    }
53
}
54