Core::init()   B
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 5
nc 9
nop 2
dl 0
loc 10
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace OpenOauth\Core;
4
5
use OpenOauth\Core\CacheDriver\BaseDriver as CacheBaseDriver;
6
use OpenOauth\Core\CacheDriver\BaseDriver;
7
use OpenOauth\Core\DatabaseDriver\BaseDriver as DatabaseBaseDriver;
8
9
use OpenOauth\Core\CacheDriver\FileDriver as CacheFileDriver;
10
use OpenOauth\Core\DatabaseDriver\FileDriver as DatabaseFileDriver;
11
use OpenOauth\Core\Config;
12
use OpenOauth\Core\Exceptions\ConfigMistakeException;
13
use OpenOauth\Core\Http\Http;
14
use stdClass;
15
16
class Core
17
{
18
    /** @var  object */
19
    public           $configs;
20
    protected static $error;
21
    /** @var  BaseDriver */
22
    protected static $cacheDriver;
23
    /** @var  DatabaseBaseDriver */
24
    protected static $databaseDriver;
25
26
    const GET_COMPONENT_ACCESS_TOKEN  = 'https://api.weixin.qq.com/cgi-bin/component/api_component_token'; //获取第三方token
27
    const GET_COMPONENT_PRE_AUTH_CODE = 'https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode'; //获取第三方auth_code
28
29
    /**
30
     * 初始化 缓存 数据库 配置
31
     *
32
     * @param \OpenOauth\Core\CacheDriver\BaseDriver|null    $cacheDriver
33
     * @param \OpenOauth\Core\DatabaseDriver\BaseDriver|null $databaseDriver
34
     */
35
    public static function init(CacheBaseDriver $cacheDriver = null, DatabaseBaseDriver $databaseDriver = null)
36
    {
37
        if (!self::$cacheDriver) {
38
            self::$cacheDriver = empty($cacheDriver) ? new CacheFileDriver(dirname(dirname(dirname(__FILE__))) . '/cache') : $cacheDriver;
39
        }
40
41
        if (!self::$databaseDriver) {
42
            self::$databaseDriver = empty($databaseDriver) ? new DatabaseFileDriver(dirname(dirname(dirname(__FILE__))) . '/database') : $databaseDriver;
43
        }
44
    }
45
46
    /**
47
     * Core constructor.
48
     *
49
     */
50
    public function __construct()
51
    {
52
        try {
53
            if (!self::$cacheDriver || !self::$databaseDriver) {
54
                throw new ConfigMistakeException('未初始化init 缓存 数据库 配置');
55
            }
56
        } catch (ConfigMistakeException $e) {
57
            echo $e->getMessage();
58
59
            return false;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
60
        }
61
62
        $configs = Config::$configs;
0 ignored issues
show
Bug introduced by
The property configs cannot be accessed from this context as it is declared private in class OpenOauth\Core\Config.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
63
64
        try {
65
            if (!isset($configs['component_app_id']) || !isset($configs['component_app_secret']) || !isset($configs['component_app_token']) || !isset($configs['component_app_key'])) {
66
                throw new ConfigMistakeException();
67
            }
68
        } catch (ConfigMistakeException $e) {
69
            echo $e->getMessage();
70
71
            return false;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
72
        }
73
74
        if (!$this->configs) {
75
            $this->configs                       = new stdClass();
76
            $this->configs->component_app_id     = $configs['component_app_id'];
77
            $this->configs->component_app_secret = $configs['component_app_secret'];
78
            $this->configs->component_app_token  = $configs['component_app_token'];
79
            $this->configs->component_app_key    = $configs['component_app_key'];
80
        }
81
    }
82
83
    /**
84
     * 获取开放平台 ComponentAccessToken
85
     *
86
     * @return bool|mixed
87
     */
88
    public function getComponentAccessToken()
89
    {
90
        $component_access_token = self::$cacheDriver->_get('component_access_token:' . $this->configs->component_app_id);
91
92
        if (false == $component_access_token) {
93
94
            $request_data = [
95
                'component_appid'         => $this->configs->component_app_id,
96
                'component_appsecret'     => $this->configs->component_app_secret,
97
                'component_verify_ticket' => self::$cacheDriver->_get('component_verify_ticket:' . $this->configs->component_app_id),
98
            ];
99
100
            $response_data = Http::_post(self::GET_COMPONENT_ACCESS_TOKEN, $request_data);
101
102 View Code Duplication
            if (!$response_data || !is_array($response_data) || empty($response_data)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $response_data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
                $this->setError(Http::$error);
104
105
                return false;
106
            }
107
108
            self::$cacheDriver->_set('component_access_token:' . $this->configs->component_app_id, $response_data['component_access_token'], 5000);
109
110
            $component_access_token = $response_data['component_access_token'];
111
        }
112
113
        return $component_access_token;
114
    }
115
116
    /**
117
     * 获取第三方auth_code
118
     *
119
     * @return bool|null|string|void
120
     */
121
    public function getComponentPreAuthCode()
122
    {
123
        $component_access_token = $this->getComponentAccessToken();
124
125
        $query_data   = http_build_query(['component_access_token' => $component_access_token]);
126
        $request_data = [
127
            'component_appid' => $this->configs->component_app_id,
128
        ];
129
130
        $response_data = Http::_post(self::GET_COMPONENT_PRE_AUTH_CODE . '?' . $query_data, $request_data);
131 View Code Duplication
        if (!$response_data || !is_array($response_data) || empty($response_data)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $response_data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
            $this->setError(Http::$error);
133
134
            return false;
135
        }
136
137
        $component_pre_auth_code = $response_data['pre_auth_code'];
138
139
        if (false == $component_pre_auth_code) {
140
            $component_pre_auth_code = self::$cacheDriver->_get('component_pre_auth_code:' . $this->configs->component_app_id);
141
        } else {
142
            self::$cacheDriver->_set('component_pre_auth_code:' . $this->configs->component_app_id, $component_pre_auth_code, 5000);
143
        }
144
145
        return $component_pre_auth_code;
146
    }
147
148
    /**
149
     * @return mixed
150
     */
151
    public static function getError()
152
    {
153
        return self::$error;
154
    }
155
156
    /**
157
     * @param string $error
158
     */
159
    public static function setError($error = '')
160
    {
161
        self::$error = $error;
162
    }
163
}
164