Completed
Pull Request — master (#553)
by
unknown
02:30
created

PreAuthCode::getAppId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
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
 * PreAuthCode.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\Components;
28
29
use EasyWeChat\Core\Exceptions\InvalidArgumentException;
30
use EasyWeChat\Core\Exceptions\RuntimeException;
31
use EasyWeChat\Support\Arr;
32
use EasyWeChat\Support\Url;
33
34
class PreAuthCode extends AbstractComponent
35
{
36
    /**
37
     * Create pre auth code url.
38
     */
39
    const CREATE_PRE_AUTH_CODE = 'https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode';
40
41
    /**
42
     * Pre auth link
43
     */
44
    const PRE_AUTH_LINK = 'https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=%s&pre_auth_code=%s&redirect_uri=%s';
45
    /**
46
     * AppId.
47
     *
48
     * @var string
49
     */
50
    private $appId;
51
52
    /**
53
     * Redirect Uri.
54
     *
55
     * @var string
56
     */
57
    protected $redirectUri;
58
59
    /**
60
     * Get AppId.
61
     *
62
     * @return string
63
     */
64
    public function getAppId()
65
    {
66
        if (is_null($this->appId)) {
67
            $this->appId = $this->config['app_id'];
68
        }
69
70
        return $this->appId;
71
    }
72
73
    /**
74
     * Set AppId.
75
     *
76
     * @param string $appId
77
     *
78
     * @return $this
79
     */
80
    public function setAppId($appId)
81
    {
82
        $this->appId = $appId;
83
84
        return $this;
85
    }
86
87
    /**
88
     * Get pre auth code.
89
     *
90
     * @throws InvalidArgumentException
91
     *
92
     * @return string
93
     */
94
    public function getCode()
95
    {
96
        $data = [
97
            'component_appid' => $this->getAppId()
98
        ];
99
100
        $result = $this->parseJSON('json', [self::CREATE_PRE_AUTH_CODE, $data]);
101
102
        if (!Arr::get($result, 'pre_auth_code')) {
0 ignored issues
show
Documentation introduced by
$result is of type object<EasyWeChat\Support\Collection>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
103
            throw new InvalidArgumentException("Invalid response.");
104
        }
105
106
        return $result['pre_auth_code'];
107
    }
108
109
    /**
110
     * Get Redirect url.
111
     *
112
     * @return string
113
     *
114
     * @throws RuntimeException
115
     */
116
    public function getRedirectUri()
117
    {
118
        if (!$this->redirectUri) {
119
            throw new RuntimeException('You need to provided a redirect uri.');
120
        }
121
        return $this->redirectUri;
122
    }
123
124
    /**
125
     * Set redirect uri.
126
     *
127
     * @param string $uri
128
     *
129
     * @return $this
130
     */
131
    public function setRedirectUri($uri)
132
    {
133
        $this->redirectUri = $uri;
134
135
        return $this;
136
    }
137
138
    /**
139
     * Get auth page link.
140
     *
141
     * @return string
142
     */
143
    public function getAuthLink()
144
    {
145
        return sprintf(self::PRE_AUTH_LINK,
146
            $this->config['app_id'],
147
            $this->getCode(),
148
            Url::encode($this->getRedirectUri())
149
        );
150
    }
151
}
152