Contact::getConfigArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
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 Contact.
13
 */
14
class Contact 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
     * Group ticket cache prefix.
32
     */
33
    const GROUP_TICKET_CACHE_PREFIX = 'entwechat.group_ticket.';
34
35
    /**
36
     * Api of group ticket.
37
     */
38
    const API_GROUP_TICKET = 'https://qyapi.weixin.qq.com/cgi-bin/ticket/get';
39
40
    /**
41
     * Get config json for jsapi.
42
     *
43
     * @param array $params
44
     * @param bool  $json
45
     *
46
     * @return array|string
47
     */
48
    public function config(array $params, $json = true)
49
    {
50
        $signPackage = $this->signature();
51
52
        $config = array_merge($signPackage, ['params' => $params]);
53
54
        return $json ? json_encode($config) : $config;
55
    }
56
57
    /**
58
     * Return jsapi config as a PHP array.
59
     *
60
     * @param array $params
61
     *
62
     * @return array
63
     */
64
    public function getConfigArray(array $params)
65
    {
66
        return $this->config($params, false);
67
    }
68
69
    /**
70
     * Get ticket.
71
     *
72
     * @param bool $forceRefresh
73
     *
74
     * @return string
75
     */
76 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...
77
    {
78
        $key = self::GROUP_TICKET_CACHE_PREFIX.$this->getAccessToken()->getFingerprint();
79
        $ticket = $this->getCache()->fetch($key);
80
81
        if (!$forceRefresh && !empty($ticket)) {
82
            return $ticket;
83
        }
84
85
        $result = $this->parseJSON('get', [self::API_GROUP_TICKET, ['type' => 'contact']]);
86
87
        $this->getCache()->save($key, $result, $result['expires_in'] - 500);
88
89
        return $result;
90
    }
91
92
    /**
93
     * Build signature.
94
     *
95
     * @param string $url
96
     * @param string $nonce
97
     * @param int    $timestamp
98
     *
99
     * @return array
100
     */
101
    public function signature($url = null, $nonce = null, $timestamp = null)
102
    {
103
        $url = $url ? $url : $this->getUrl();
104
        $nonce = $nonce ? $nonce : Str::quickRandom(10);
105
        $timestamp = $timestamp ? $timestamp : time();
106
        $ticket = $this->ticket();
107
108
        $sign = [
109
            'groupId'   => $ticket['group_id'],
110
            'nonceStr'  => $nonce,
111
            'timestamp' => $timestamp,
112
            'signature' => $this->getSignature($ticket['ticket'], $nonce, $timestamp, $url),
113
        ];
114
115
        return $sign;
116
    }
117
118
    /**
119
     * Sign the params.
120
     *
121
     * @param string $ticket
122
     * @param string $nonce
123
     * @param int    $timestamp
124
     * @param string $url
125
     *
126
     * @return string
127
     */
128
    public function getSignature($ticket, $nonce, $timestamp, $url)
129
    {
130
        return sha1("group_ticket={$ticket}&noncestr={$nonce}&timestamp={$timestamp}&url={$url}");
131
    }
132
133
    /**
134
     * Set current url.
135
     *
136
     * @param string $url
137
     *
138
     * @return Contact
139
     */
140
    public function setUrl($url)
141
    {
142
        $this->url = $url;
143
144
        return $this;
145
    }
146
147
    /**
148
     * Get current url.
149
     *
150
     * @return string
151
     */
152
    public function getUrl()
153
    {
154
        if ($this->url) {
155
            return $this->url;
156
        }
157
158
        return UrlHelper::current();
159
    }
160
161
    /**
162
     * Set cache manager.
163
     *
164
     * @param \Doctrine\Common\Cache\Cache $cache
165
     *
166
     * @return $this
167
     */
168
    public function setCache(Cache $cache)
169
    {
170
        $this->cache = $cache;
171
172
        return $this;
173
    }
174
175
    /**
176
     * Return cache manager.
177
     *
178
     * @return \Doctrine\Common\Cache\Cache
179
     */
180
    public function getCache()
181
    {
182
        return $this->cache ?: $this->cache = new FilesystemCache(sys_get_temp_dir());
183
    }
184
}
185