SuiteTicket::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
crap 1
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
namespace EasyWeChat\OpenWork\SuiteAuth;
13
14
use EasyWeChat\Kernel\Exceptions\RuntimeException;
15
use EasyWeChat\Kernel\Traits\InteractsWithCache;
16
use EasyWeChat\OpenWork\Application;
17
18
/**
19
 * SuiteTicket.
20
 *
21
 * @author xiaomin <[email protected]>
22
 */
23
class SuiteTicket
24
{
25
    use InteractsWithCache;
26
27
    /**
28
     * @var Application
29
     */
30
    protected $app;
31
32
    /**
33
     * SuiteTicket constructor.
34
     *
35
     * @param Application $app
36
     */
37 5
    public function __construct(Application $app)
38
    {
39 5
        $this->app = $app;
40 5
    }
41
42
    /**
43
     * @param string $ticket
44
     *
45
     * @return $this
46
     *
47
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
48
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
49
     * @throws \Psr\SimpleCache\InvalidArgumentException
50
     */
51 3
    public function setTicket(string $ticket)
52
    {
53 3
        $this->getCache()->set($this->getCacheKey(), $ticket, 1800);
54
55 3
        if (!$this->getCache()->has($this->getCacheKey())) {
56 1
            throw new RuntimeException('Failed to cache suite ticket.');
57
        }
58
59 2
        return $this;
60
    }
61
62
    /**
63
     * @return string
64
     *
65
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
66
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
67
     * @throws \Psr\SimpleCache\InvalidArgumentException
68
     */
69 3
    public function getTicket(): string
70
    {
71 3
        if ($cached = $this->getCache()->get($this->getCacheKey())) {
72 2
            return $cached;
73
        }
74
75 1
        throw new RuntimeException('Credential "suite_ticket" does not exist in cache.');
76
    }
77
78
    /**
79
     * @return string
80
     */
81 5
    protected function getCacheKey(): string
82
    {
83 5
        return 'easywechat.open_work.suite_ticket.'.$this->app['config']['suite_id'];
84
    }
85
}
86