Js::config()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 4
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace YEntWeChat\Js;
4
5
use Doctrine\Common\Cache\Cache;
6
use Doctrine\Common\Cache\FilesystemCache;
7
use YEntWeChat\Core\AbstractAPI;
8
use YEntWeChat\Support\Str;
9
use YEntWeChat\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 = 'yentwechat.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()->getFingerprint(),
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
        //sha1("group_ticket={$ticket}&noncestr={$nonce}&timestamp={$timestamp}&url={$url}")
140
        return sha1("jsapi_ticket={$ticket}&noncestr={$nonce}&timestamp={$timestamp}&url={$url}");
141
    }
142
143
    /**
144
     * Set current url.
145
     *
146
     * @param string $url
147
     *
148
     * @return Js
149
     */
150
    public function setUrl($url)
151
    {
152
        $this->url = $url;
153
154
        return $this;
155
    }
156
157
    /**
158
     * Get current url.
159
     *
160
     * @return string
161
     */
162
    public function getUrl()
163
    {
164
        if ($this->url) {
165
            return $this->url;
166
        }
167
168
        return UrlHelper::current();
169
    }
170
171
    /**
172
     * Set cache manager.
173
     *
174
     * @param \Doctrine\Common\Cache\Cache $cache
175
     *
176
     * @return $this
177
     */
178
    public function setCache(Cache $cache)
179
    {
180
        $this->cache = $cache;
181
182
        return $this;
183
    }
184
185
    /**
186
     * Return cache manager.
187
     *
188
     * @return \Doctrine\Common\Cache\Cache
189
     */
190
    public function getCache()
191
    {
192
        return $this->cache ?: $this->cache = new FilesystemCache(sys_get_temp_dir());
193
    }
194
}
195