Completed
Push — master ( 73ed3f...e38e9f )
by Carlos
02:46
created

OpenPlatform   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 69
rs 10
c 0
b 0
f 0
ccs 0
cts 16
cp 0
wmc 4
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __get() 0 12 3
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
/**
13
 * OpenPlatform.php.
14
 *
15
 * Part of Overtrue\WeChat.
16
 *
17
 * For the full copyright and license information, please view the LICENSE
18
 * file that was distributed with this source code.
19
 *
20
 * @author    mingyoung <[email protected]>
21
 * @copyright 2016
22
 *
23
 * @see      https://github.com/overtrue
24
 * @see      http://overtrue.me
25
 */
26
27
namespace EasyWeChat\OpenPlatform;
28
29
use EasyWeChat\Core\Exceptions\InvalidArgumentException;
30
use EasyWeChat\Support\Arr;
31
32
/**
33
 * Class OpenPlatform.
34
 *
35
 * @property \EasyWeChat\OpenPlatform\Guard $server
36
 * @property \EasyWeChat\OpenPlatform\Components\PreAuthCode $pre_auth
37
 * @property \EasyWeChat\OpenPlatform\Components\Authorizer $authorizer
38
 */
39
class OpenPlatform
40
{
41
    /**
42
     * Server guard.
43
     *
44
     * @var Guard
45
     */
46
    protected $server;
47
48
    /**
49
     * OpenPlatform component access token.
50
     *
51
     * @var string
52
     */
53
    protected $access_token;
54
55
    /**
56
     * OpenPlatform config.
57
     *
58
     * @var array
59
     */
60
    protected $config;
61
62
    /**
63
     * Components.
64
     *
65
     * @var array
66
     */
67
    private $components = [
68
        'pre_auth' => Components\PreAuthCode::class,
69
        'authorizer' => Components\Authorizer::class,
70
    ];
71
72
    /**
73
     * OpenPlatform constructor.
74
     *
75
     * @param Guard $server
76
     * @param $access_token
77
     * @param array $config
78
     */
79
    public function __construct(Guard $server, $access_token, $config)
80
    {
81
        $this->server = $server;
82
        $this->access_token = $access_token;
83
        $this->config = $config;
84
    }
85
86
    /**
87
     * Magic get access.
88
     *
89
     * @param $name
90
     *
91
     * @return mixed
92
     *
93
     * @throws \Exception
94
     */
95
    public function __get($name)
96
    {
97
        if (property_exists($this, $name)) {
98
            return $this->$name;
99
        }
100
101
        if ($class = Arr::get($this->components, $name)) {
102
            return new $class($this->access_token, $this->config);
103
        }
104
105
        throw new InvalidArgumentException("Property or component \"$name\" does not exists.");
106
    }
107
}
108