Passed
Push — master ( e38f5c...931b65 )
by Carlos
06:23 queued 02:38
created

AbstractComponent   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 72.21%

Importance

Changes 0
Metric Value
dl 0
loc 77
ccs 13
cts 18
cp 0.7221
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getAppId() 0 18 4
A setAppId() 0 6 1
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\Api;
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
     * Config.
39
     *
40
     * @var array
41
     */
42
    protected $config;
43
44
    /**
45
     * AppId, component app id.
46
     *
47
     * @var string
48
     */
49
    private $appId;
50
51
    /**
52
     * Request.
53
     *
54
     * @var Request
55
     */
56
    protected $request;
57
58
    /**
59
     * AbstractComponent constructor.
60
     *
61
     * @param AccessToken $accessToken
62
     * @param array       $config
63
     * @param $request
64
     */
65 11
    public function __construct($accessToken, array $config, $request = null)
66
    {
67 11
        parent::__construct($accessToken);
68 11
        $this->config = $config;
69 11
        $this->request = $request ?: Request::createFromGlobals();
70 11
    }
71
72
    /**
73
     * Get AppId.
74
     *
75
     * @return string
76
     *
77
     * @throws Exception when app id is not present
78
     */
79 8
    public function getAppId()
80
    {
81 8
        if ($this->appId) {
82 1
            return $this->appId;
83
        }
84
85 8
        if (isset($this->config['open_platform'])) {
86 8
            $this->appId = $this->config['open_platform']['app_id'];
87 8
        } else {
88
            $this->appId = $this->config['app_id'];
89
        }
90
91 8
        if (empty($this->appId)) {
92
            throw new Exception('App Id is not present.');
93
        }
94
95 8
        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