Test Setup Failed
Pull Request — master (#606)
by
unknown
03:03
created

VerifyTicket   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 98
rs 10
c 1
b 0
f 0
ccs 0
cts 25
cp 0
wmc 7
lcom 1
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A cache() 0 6 1
A getTicket() 0 7 2
A setCacheKey() 0 5 1
A getCacheKey() 0 7 2
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
        $this->appId = $appId;
75
        $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...
76
    }
77
78
    /**
79
     * Save component verify ticket.
80
     *
81
     * @param Collection $message
82
     *
83
     * @return bool
84
     */
85
    public function cache(Collection $message) {
86
        return $this->set(
87
            $this->getCacheKey(),
88
            $message->get($this->ticketXmlName)
89
        );
90
    }
91
92
    /**
93
     * Get component verify ticket.
94
     *
95
     * @return string
96
     *
97
     * @throws RuntimeException
98
     */
99
    public function getTicket() {
100
        if ($cached = $this->get($this->getCacheKey())) {
101
            return $cached;
102
        }
103
104
        throw new RuntimeException('Component verify ticket does not exists.');
105
    }
106
107
    /**
108
     * Set component verify ticket cache key.
109
     *
110
     * @param string $cacheKey
111
     *
112
     * @return $this
113
     */
114
    public function setCacheKey($cacheKey) {
115
        $this->cacheKey = $cacheKey;
116
117
        return $this;
118
    }
119
120
    /**
121
     * Get component verify ticket cache key.
122
     *
123
     * @return string $this->cacheKey
124
     */
125
    public function getCacheKey() {
126
        if (is_null($this->cacheKey)) {
127
            return $this->prefix . $this->appId;
128
        }
129
130
        return $this->cacheKey;
131
    }
132
}
133