Test Setup Failed
Pull Request — master (#606)
by
unknown
03:03
created

AbstractComponent::setAppId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 2
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
 * AbstractComponent.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
 * @author    lixiao <[email protected]>
22
 * @copyright 2016
23
 *
24
 * @see      https://github.com/overtrue
25
 * @see      http://overtrue.me
26
 */
27
28
namespace EasyWeChat\OpenPlatform\Components;
29
30
use EasyWeChat\Core\AbstractAPI;
31
use EasyWeChat\Core\AccessToken;
32
use EasyWeChat\Core\Exception;
33
use Symfony\Component\HttpFoundation\Request;
34
35
abstract class AbstractComponent extends AbstractAPI
36
{
37
38
    /**
39
     * Config.
40
     *
41
     * @var array
42
     */
43
    protected $config;
44
45
    /**
46
     * AppId, component app id.
47
     *
48
     * @var string
49
     */
50
    private $appId;
51
52
    /**
53
     * Request.
54
     *
55
     * @var Request
56
     */
57
    protected $request;
58
59
    /**
60
     * AbstractComponent constructor.
61
     *
62
     * @param AccessToken $accessToken
63
     * @param array $config
64
     * @param $request
65
     */
66
    public function __construct($accessToken, array $config, $request = null)
67
    {
68
        parent::__construct($accessToken);
69
        $this->config = $config;
70
        $this->request = $request ?: Request::createFromGlobals();
71
    }
72
73
    /**
74
     * Get AppId.
75
     *
76
     * @return string
77
     * @throws Exception When app id is not present.
78
     */
79
    public function getAppId()
80
    {
81
        if ($this->appId) {
82
            return $this->appId;
83
        }
84
85
        if (isset($this->config['open_platform'])) {
86
            $this->appId = $this->config['open_platform']['app_id'];
87
        } else {
88
            $this->appId = $this->config['app_id'];
89
        }
90
91
        if (empty($this->appId)) {
92
            throw new Exception('App Id is not present.');
93
        }
94
95
        return $this->appId;
96
    }
97
98
    /**
99
     * Set AppId.
100
     *
101
     * @param string $appId
102
     *
103
     * @return $this
104
     */
105
    public function setAppId($appId)
106
    {
107
        $this->appId = $appId;
108
109
        return $this;
110
    }
111
112
}
113