Completed
Push — master ( 9e0ead...983405 )
by Carlos
11:10 queued 08:26
created

QRCode::url()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 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
 * QRCode.php.
14
 *
15
 * @author    overtrue <[email protected]>
16
 * @copyright 2015 overtrue <[email protected]>
17
 *
18
 * @link      https://github.com/overtrue
19
 * @link      http://overtrue.me
20
 */
21
namespace EasyWeChat\QRCode;
22
23
use EasyWeChat\Core\AbstractAPI;
24
25
/**
26
 * Class QRCode.
27
 */
28
class QRCode extends AbstractAPI
29
{
30
    const DAY = 86400;
31
    const SCENE_MAX_VALUE = 100000;
32
    const SCENE_QR_CARD = 'QR_CARD';
33
    const SCENE_QR_TEMPORARY = 'QR_SCENE';
34
    const SCENE_QR_FOREVER = 'QR_LIMIT_SCENE';
35
    const SCENE_QR_FOREVER_STR = 'QR_LIMIT_STR_SCENE';
36
37
    const API_CREATE = 'https://api.weixin.qq.com/cgi-bin/qrcode/create';
38
    const API_SHOW = 'https://mp.weixin.qq.com/cgi-bin/showqrcode';
39
40
    /**
41
     * Create forever.
42
     *
43
     * @param int $sceneValue
44
     *
45
     * @return \EasyWeChat\Support\Collection
46
     */
47 1
    public function forever($sceneValue)
48
    {
49 1
        if (is_int($sceneValue) && $sceneValue > 0 && $sceneValue < self::SCENE_MAX_VALUE) {
50 1
            $type = self::SCENE_QR_FOREVER;
51 1
            $sceneKey = 'scene_id';
52 1
        } else {
53 1
            $type = self::SCENE_QR_FOREVER_STR;
54 1
            $sceneKey = 'scene_str';
55
        }
56
57 1
        $scene = [$sceneKey => $sceneValue];
58
59 1
        return $this->create($type, $scene, false);
60
    }
61
62
    /**
63
     * Create temporary.
64
     *
65
     * @param string $sceneId
66
     * @param null   $expireSeconds
67
     *
68
     * @return \EasyWeChat\Support\Collection
69
     */
70 1
    public function temporary($sceneId, $expireSeconds = null)
71
    {
72 1
        $scene = ['scene_id' => intval($sceneId)];
73
74 1
        return $this->create(self::SCENE_QR_TEMPORARY, $scene, true, $expireSeconds);
75
    }
76
77
    /**
78
     * Create QRCode for card.
79
     *
80
     * @param array $card
81
     *
82
     * {
83
     *    "card_id": "pFS7Fjg8kV1IdDz01r4SQwMkuCKc",
84
     *    "code": "198374613512",
85
     *    "openid": "oFS7Fjl0WsZ9AMZqrI80nbIq8xrA",
86
     *    "expire_seconds": "1800",
87
     *    "is_unique_code": false , "outer_id" : 1
88
     *  }
89
     *
90
     * @return \EasyWeChat\Support\Collection
91
     */
92 1
    public function card($card)
93
    {
94 1
        return $this->create(self::SCENE_QR_CARD, ['card' => $card]);
95
    }
96
97
    /**
98
     * Return url for ticket.
99
     *
100
     * @param string $ticket
101
     *
102
     * @return string
103
     */
104 1
    public function url($ticket)
105
    {
106 1
        return self::API_SHOW."?ticket={$ticket}";
107
    }
108
109
    /**
110
     * Create a QRCode.
111
     *
112
     * @param string $actionName
113
     * @param array  $actionInfo
114
     * @param bool   $temporary
115
     * @param int    $expireSeconds
116
     *
117
     * @return \EasyWeChat\Support\Collection
118
     */
119 3
    protected function create($actionName, $actionInfo, $temporary = true, $expireSeconds = null)
120
    {
121 3
        $expireSeconds !== null || $expireSeconds = 7 * self::DAY;
122
123
        $params = [
124 3
                   'action_name' => $actionName,
125 3
                   'action_info' => ['scene' => $actionInfo],
126 3
                  ];
127
128 3
        if ($temporary) {
129 2
            $params['expire_seconds'] = min($expireSeconds, 30 * self::DAY);
130 2
        }
131
132 3
        return $this->parseJSON('json', [self::API_CREATE, $params]);
133
    }
134
}
135