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

PreAuthorization   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
dl 0
loc 55
ccs 10
cts 11
cp 0.9091
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCode() 0 14 2
A redirect() 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
 * PreAuth.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\Exceptions\InvalidArgumentException;
31
use Symfony\Component\HttpFoundation\RedirectResponse;
32
33
class PreAuthorization 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
    /**
46
     * Redirect Uri.
47
     *
48
     * @var string
49
     */
50
    protected $redirectUri;
51
52
    /**
53
     * Get pre auth code.
54
     *
55
     * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
56
     *
57
     * @return string
58
     */
59 2
    public function getCode()
60
    {
61
        $data = [
62 2
            'component_appid' => $this->getAppId(),
63 2
        ];
64
65 2
        $result = $this->parseJSON('json', [self::CREATE_PRE_AUTH_CODE, $data]);
66
67 2
        if (empty($result['pre_auth_code'])) {
68
            throw new InvalidArgumentException('Invalid response.');
69
        }
70
71 2
        return $result['pre_auth_code'];
72
    }
73
74
    /**
75
     * Redirect to WeChat PreAuthorization page.
76
     *
77
     * @param string $url
78
     *
79
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
80
     */
81 1
    public function redirect($url)
82
    {
83 1
        return new RedirectResponse(
84 1
            sprintf(self::PRE_AUTH_LINK, $this->getAppId(), $this->getCode(), urlencode($url))
85 1
        );
86
    }
87
}
88