Js::ticket()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 2
nop 1
dl 15
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace EntWeChat\Js;
4
5
use Doctrine\Common\Cache\Cache;
6
use Doctrine\Common\Cache\FilesystemCache;
7
use EntWeChat\Core\AbstractAPI;
8
use EntWeChat\Support\Str;
9
use EntWeChat\Support\Url as UrlHelper;
10
11
/**
12
 * Class Js.
13
 */
14
class Js extends AbstractAPI
15
{
16
    /**
17
     * Cache.
18
     *
19
     * @var Cache
20
     */
21
    protected $cache;
22
23
    /**
24
     * Current URI.
25
     *
26
     * @var string
27
     */
28
    protected $url;
29
30
    /**
31
     * Ticket cache prefix.
32
     */
33
    const TICKET_CACHE_PREFIX = 'entwechat.jsapi_ticket.';
34
35
    /**
36
     * Api of ticket.
37
     */
38
    const API_TICKET = 'https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket';
39
40
    /**
41
     * Get config json for jsapi.
42
     *
43
     * @param array $APIs
44
     * @param bool  $debug
45
     * @param bool  $beta
46
     * @param bool  $json
47
     *
48
     * @return array|string
49
     */
50
    public function config(array $APIs, $debug = false, $beta = false, $json = true)
51
    {
52
        $signPackage = $this->signature();
53
54
        $base = [
55
            'debug' => $debug,
56
            'beta'  => $beta,
57
        ];
58
        $config = array_merge($base, $signPackage, ['jsApiList' => $APIs]);
59
60
        return $json ? json_encode($config) : $config;
61
    }
62
63
    /**
64
     * Return jsapi config as a PHP array.
65
     *
66
     * @param array $APIs
67
     * @param bool  $debug
68
     * @param bool  $beta
69
     *
70
     * @return array
71
     */
72
    public function getConfigArray(array $APIs, $debug = false, $beta = false)
73
    {
74
        return $this->config($APIs, $debug, $beta, false);
75
    }
76
77
    /**
78
     * Get jsticket.
79
     *
80
     * @param bool $forceRefresh
81
     *
82
     * @return string
83
     */
84 View Code Duplication
    public function ticket($forceRefresh = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86
        $key = self::TICKET_CACHE_PREFIX.$this->getAccessToken()->getFingerprint();
87
        $ticket = $this->getCache()->fetch($key);
88
89
        if (!$forceRefresh && !empty($ticket)) {
90
            return $ticket;
91
        }
92
93
        $result = $this->parseJSON('get', [self::API_TICKET]);
94
95
        $this->getCache()->save($key, $result['ticket'], $result['expires_in'] - 500);
96
97
        return $result['ticket'];
98
    }
99
100
    /**
101
     * Build signature.
102
     *
103
     * @param string $url
104
     * @param string $nonce
105
     * @param int    $timestamp
106
     *
107
     * @return array
108
     */
109
    public function signature($url = null, $nonce = null, $timestamp = null)
110
    {
111
        $url = $url ? $url : $this->getUrl();
112
        $nonce = $nonce ? $nonce : Str::quickRandom(10);
113
        $timestamp = $timestamp ? $timestamp : time();
114
        $ticket = $this->ticket();
115
116
        $sign = [
117
            'appId'     => $this->getAccessToken()->getCorpId(),
118
            'nonceStr'  => $nonce,
119
            'timestamp' => $timestamp,
120
            'url'       => $url,
121
            'signature' => $this->getSignature($ticket, $nonce, $timestamp, $url),
122
        ];
123
124
        return $sign;
125
    }
126
127
    /**
128
     * Sign the params.
129
     *
130
     * @param string $ticket
131
     * @param string $nonce
132
     * @param int    $timestamp
133
     * @param string $url
134
     *
135
     * @return string
136
     */
137
    public function getSignature($ticket, $nonce, $timestamp, $url)
138
    {
139
        return sha1("jsapi_ticket={$ticket}&noncestr={$nonce}&timestamp={$timestamp}&url={$url}");
140
    }
141
142
    /**
143
     * Set current url.
144
     *
145
     * @param string $url
146
     *
147
     * @return Js
148
     */
149
    public function setUrl($url)
150
    {
151
        $this->url = $url;
152
153
        return $this;
154
    }
155
156
    /**
157
     * Get current url.
158
     *
159
     * @return string
160
     */
161
    public function getUrl()
162
    {
163
        if ($this->url) {
164
            return $this->url;
165
        }
166
167
        return UrlHelper::current();
168
    }
169
170
    /**
171
     * Set cache manager.
172
     *
173
     * @param \Doctrine\Common\Cache\Cache $cache
174
     *
175
     * @return $this
176
     */
177
    public function setCache(Cache $cache)
178
    {
179
        $this->cache = $cache;
180
181
        return $this;
182
    }
183
184
    /**
185
     * Return cache manager.
186
     *
187
     * @return \Doctrine\Common\Cache\Cache
188
     */
189
    public function getCache()
190
    {
191
        return $this->cache ?: $this->cache = new FilesystemCache(sys_get_temp_dir());
192
    }
193
}
194