Test Setup Failed
Push — master ( d37a6a...512c0e )
by Carlos
03:19
created

VerifyTicket::getCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 4
cp 0
crap 6
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
use EasyWeChat\OpenPlatform\Traits\Caches;
33
use EasyWeChat\Support\Collection;
34
35
class VerifyTicket
36
{
37
    use Caches;
38
39
    /**
40
     * App Id.
41
     *
42
     * @var string
43
     */
44
    protected $appId;
45
46
    /**
47
     * Cache Key.
48
     *
49
     * @var string
50
     */
51
    private $cacheKey;
52
53
    /**
54
     * Component verify ticket xml structure name.
55
     *
56
     * @var string
57
     */
58
    protected $ticketXmlName = 'ComponentVerifyTicket';
59
60
    /**
61
     * Cache key prefix.
62
     *
63
     * @var string
64
     */
65
    protected $prefix = 'easywechat.open_platform.component_verify_ticket.';
66
67
    /**
68
     * VerifyTicket constructor.
69
     *
70
     * @param string $appId
71
     * @param Cache $cache
72
     */
73
    public function __construct($appId, Cache $cache = null)
74
    {
75
        $this->appId = $appId;
76
        $this->setCache($cache);
0 ignored issues
show
Bug introduced by
It seems like $cache defined by parameter $cache on line 73 can be null; however, EasyWeChat\OpenPlatform\Traits\Caches::setCache() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
77
    }
78
79
    /**
80
     * Save component verify ticket.
81
     *
82
     * @param Collection $message
83
     *
84
     * @return bool
85
     */
86
    public function cache(Collection $message)
87
    {
88
        return $this->set(
89
            $this->getCacheKey(),
90
            $message->get($this->ticketXmlName)
91
        );
92
    }
93
94
    /**
95
     * Get component verify ticket.
96
     *
97
     * @return string
98
     *
99
     * @throws RuntimeException
100
     */
101
    public function getTicket()
102
    {
103
        if ($cached = $this->get($this->getCacheKey())) {
104
            return $cached;
105
        }
106
107
        throw new RuntimeException('Component verify ticket does not exists.');
108
    }
109
110
    /**
111
     * Set component verify ticket cache key.
112
     *
113
     * @param string $cacheKey
114
     *
115
     * @return $this
116
     */
117
    public function setCacheKey($cacheKey)
118
    {
119
        $this->cacheKey = $cacheKey;
120
121
        return $this;
122
    }
123
124
    /**
125
     * Get component verify ticket cache key.
126
     *
127
     * @return string $this->cacheKey
128
     */
129
    public function getCacheKey()
130
    {
131
        if (is_null($this->cacheKey)) {
132
            return $this->prefix . $this->appId;
133
        }
134
135
        return $this->cacheKey;
136
    }
137
}
138