Passed
Push — master ( 7841ea...01e589 )
by Carlos
02:32
created

QRCode::temporary()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 9
CRAP Score 3

Importance

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