Passed
Push — master ( e38f5c...931b65 )
by Carlos
06:23 queued 02:38
created

VerifyTicket::setTicket()   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 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
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
 * VerifyTicket.php.
14
 *
15
 * Part of Overtrue\WeChat.
16
 *
17
 * For the full copyright and license information, please view the LICENSE
18
 * file that was distributed with this source code.
19
 *
20
 * @author    mingyoung <[email protected]>
21
 * @author    lixiao <[email protected]>
22
 * @copyright 2016
23
 *
24
 * @see      https://github.com/overtrue
25
 * @see      http://overtrue.me
26
 */
27
28
namespace EasyWeChat\OpenPlatform;
29
30
use Doctrine\Common\Cache\Cache;
31
use EasyWeChat\Core\Exceptions\RuntimeException;
32
33
class VerifyTicket
34
{
35
    /**
36
     * Cache manager.
37
     *
38
     * @var \Doctrine\Common\Cache\Cache
39
     */
40
    protected $cache;
41
42
    /**
43
     * App Id.
44
     *
45
     * @var string
46
     */
47
    protected $appId;
48
49
    /**
50
     * Cache Key.
51
     *
52
     * @var string
53
     */
54
    private $cacheKey;
55
56
    /**
57
     * Cache key prefix.
58
     *
59
     * @var string
60
     */
61
    protected $prefix = 'easywechat.open_platform.component_verify_ticket.';
62
63
    /**
64
     * VerifyTicket constructor.
65
     *
66
     * @param string                       $appId
67
     * @param \Doctrine\Common\Cache\Cache $cache
68
     */
69 6
    public function __construct($appId, Cache $cache)
70
    {
71 6
        $this->appId = $appId;
72 6
        $this->cache = $cache;
73 6
    }
74
75
    /**
76
     * Set component verify ticket to the cache.
77
     *
78
     * @param string $ticket
79
     *
80
     * @return bool
81
     */
82 3
    public function setTicket($ticket)
83
    {
84 3
        return $this->cache->save($this->getCacheKey(), $ticket);
85
    }
86
87
    /**
88
     * Get component verify ticket.
89
     *
90
     * @return string
91
     *
92
     * @throws \EasyWeChat\Core\Exceptions\RuntimeException
93
     */
94 2
    public function getTicket()
95
    {
96 2
        if ($cached = $this->cache->fetch($this->getCacheKey())) {
97 2
            return $cached;
98
        }
99
100
        throw new RuntimeException('Component verify ticket does not exists.');
101
    }
102
103
    /**
104
     * Set component verify ticket cache key.
105
     *
106
     * @param string $cacheKey
107
     *
108
     * @return $this
109
     */
110 1
    public function setCacheKey($cacheKey)
111
    {
112 1
        $this->cacheKey = $cacheKey;
113
114 1
        return $this;
115
    }
116
117
    /**
118
     * Get component verify ticket cache key.
119
     *
120
     * @return string $this->cacheKey
121
     */
122 4
    public function getCacheKey()
123
    {
124 4
        if (is_null($this->cacheKey)) {
125 4
            return $this->prefix.$this->appId;
126
        }
127
128 1
        return $this->cacheKey;
129
    }
130
}
131