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

PreAuthCode::getAuthLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
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\Url;
32
33
class PreAuthCode extends AbstractComponent
34
{
35
    /**
36
     * Create pre auth code url.
37
     */
38
    const CREATE_PRE_AUTH_CODE = 'https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode';
39
40
    /**
41
     * Pre auth link.
42
     */
43
    const PRE_AUTH_LINK = 'https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=%s&pre_auth_code=%s&redirect_uri=%s';
44
    /**
45
     * AppId.
46
     *
47
     * @var string
48
     */
49
    private $appId;
50
51
    /**
52
     * Redirect Uri.
53
     *
54
     * @var string
55
     */
56
    protected $redirectUri;
57
58
    /**
59
     * Get AppId.
60
     *
61
     * @return string
62
     */
63
    public function getAppId()
64
    {
65
        if (is_null($this->appId)) {
66
            $this->appId = $this->config['app_id'];
67
        }
68
69
        return $this->appId;
70
    }
71
72
    /**
73
     * Set AppId.
74
     *
75
     * @param string $appId
76
     *
77
     * @return $this
78
     */
79
    public function setAppId($appId)
80
    {
81
        $this->appId = $appId;
82
83
        return $this;
84
    }
85
86
    /**
87
     * Get pre auth code.
88
     *
89
     * @throws InvalidArgumentException
90
     *
91
     * @return string
92
     */
93
    public function getCode()
94
    {
95
        $data = [
96
            'component_appid' => $this->getAppId(),
97
        ];
98
99
        $result = $this->parseJSON('json', [self::CREATE_PRE_AUTH_CODE, $data]);
100
101
        if (empty($result['pre_auth_code'])) {
102
            throw new InvalidArgumentException('Invalid response.');
103
        }
104
105
        return $result['pre_auth_code'];
106
    }
107
108
    /**
109
     * Get Redirect url.
110
     *
111
     * @return string
112
     *
113
     * @throws RuntimeException
114
     */
115
    public function getRedirectUri()
116
    {
117
        if (!$this->redirectUri) {
118
            throw new RuntimeException('You need to provided a redirect uri.');
119
        }
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