Completed
Push — master ( 017947...8f5b55 )
by frey
13s
created

PreAuthorization::redirect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace EntWeChat\Suite\Api;
4
5
use EntWeChat\Core\Exceptions\InvalidArgumentException;
6
use Symfony\Component\HttpFoundation\RedirectResponse;
7
8
class PreAuthorization extends AbstractSuite
9
{
10
    /**
11
     * Get pre auth code url.
12
     */
13
    const GET_PRE_AUTH_CODE = 'https://qyapi.weixin.qq.com/cgi-bin/service/get_pre_auth_code';
14
15
    /**
16
     * Pre auth link.
17
     */
18
    const PRE_AUTH_LINK = 'https://qy.weixin.qq.com/cgi-bin/loginpage?suite_id=%s$&pre_auth_code=%s$&redirect_uri=%s';
19
20
    /**
21
     * Redirect to WeChat PreAuthorization page.
22
     *
23
     * @param string $url
24
     *
25
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
26
     */
27
    public function redirect($url)
28
    {
29
        return new RedirectResponse(
30
            sprintf(self::PRE_AUTH_LINK, $this->getSuiteId(), $this->getCode(), urlencode($url))
31
        );
32
    }
33
34
    /**
35
     * Get pre auth code.
36
     *
37
     * @throws \EntWeChat\Core\Exceptions\InvalidArgumentException
38
     *
39
     * @return string
40
     */
41
    public function getCode()
42
    {
43
        $data = [
44
            'suite_id' => $this->getSuiteId(),
45
        ];
46
47
        $result = $this->parseJSON('json', [self::GET_PRE_AUTH_CODE, $data]);
48
49
        if (empty($result['pre_auth_code'])) {
50
            throw new InvalidArgumentException('Invalid response.');
51
        }
52
53
        return $result['pre_auth_code'];
54
    }
55
}
56