Completed
Pull Request — master (#617)
by mingyoung
05:10
created

OpenPlatform::setContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
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
use Pimple\Container;
32
33
/**
34
 * Class OpenPlatform.
35
 *
36
 * @property \EasyWeChat\OpenPlatform\Guard $server
37
 * @property \EasyWeChat\OpenPlatform\Components\PreAuthCode $pre_auth
38
 * @property \EasyWeChat\OpenPlatform\AccessToken $access_token
39
 * @property \EasyWeChat\OpenPlatform\AuthorizerToken $authorizer_token;
40
 * @property \EasyWeChat\OpenPlatform\Authorization $authorization;
41
 * @property \EasyWeChat\OpenPlatform\Components\Authorizer $authorizer
42
 */
43
class OpenPlatform
44
{
45
    /**
46
     * Server guard.
47
     *
48
     * @var Guard
49
     */
50
    protected $server;
51
52
    /**
53
     * OpenPlatform component access token.
54
     *
55
     * @var AccessToken
56
     */
57
    protected $access_token;
58
59
    /**
60
     * OpenPlatform config.
61
     *
62
     * @var array
63
     */
64
    protected $config;
65
66
    /**
67
     * Container in the scope of the open platform.
68
     *
69
     * @var Container
70
     */
71
    protected $container;
72
73
    /**
74
     * Components.
75
     *
76
     * @var array
77
     */
78
    private $components = [
79
        'pre_auth' => Components\PreAuthCode::class,
80
        'authorizer' => Components\Authorizer::class,
81
    ];
82
83
    /**
84
     * OpenPlatform constructor.
85
     *
86
     * @param Guard $server
87
     * @param $access_token
88
     * @param array $config
89
     */
90 1
    public function __construct(Guard $server, $access_token, $config)
91
    {
92 1
        $this->server = $server;
93 1
        $this->access_token = $access_token;
94 1
        $this->config = $config;
95 1
    }
96
97
    /**
98
     * Sets the container for use of the platform.
99
     *
100
     * @param Container $container
101
     */
102 1
    public function setContainer(Container $container)
103
    {
104 1
        $this->container = $container;
105 1
    }
106
107
    /**
108
     * Magic get access.
109
     *
110
     * @param $name
111
     *
112
     * @return mixed
113
     *
114
     * @throws \Exception
115
     */
116 1
    public function __get($name)
117
    {
118 1
        if (property_exists($this, $name)) {
119 1
            return $this->$name;
120
        }
121
122 1
        if ($class = Arr::get($this->components, $name)) {
123 1
            return new $class($this->access_token, $this->config);
124
        }
125
126
        if ($instance = $this->container->offsetGet("open_platform.{$name}")) {
127
            return $instance;
128
        }
129
130
        throw new InvalidArgumentException("Property or component \"$name\" does not exists.");
131
    }
132
}
133