Passed
Push — master ( 2bd1bb...6033fd )
by hugh
08:09
created

Attribute::getOts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: hugh.li
5
 * Date: 2021/6/8
6
 * Time: 11:02 下午.
7
 */
8
9
namespace HughCube\Laravel\OTS\Cache;
10
11
use Aliyun\OTS\Consts\ColumnTypeConst;
12
use HughCube\Laravel\OTS\Connection;
13
use Illuminate\Support\InteractsWithTime;
14
15
trait Attribute
16
{
17
    use InteractsWithTime;
18
19
    /**
20
     * @var Connection
21
     */
22
    protected $ots;
23
24
    /**
25
     * @var string
26
     */
27
    protected $table;
28
29
    /**
30
     * @var null|string
31
     */
32
    protected $prefix;
33
34
    /**
35
     * @var null|string
36
     */
37
    protected $type;
38
39
    /**
40
     * @var null|string
41
     */
42
    protected $owner;
43
44
    /**
45
     * @return string|null
46
     */
47
    public function getPrefix(): ?string
48
    {
49
        return $this->prefix;
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    public function getTable(): string
56
    {
57
        return $this->table;
58
    }
59
60
    /**
61
     * @return Connection
62
     */
63
    public function getOts(): Connection
64
    {
65
        return $this->ots;
66
    }
67
68
    /**
69
     * @param  string  $key
70
     * @return array
71
     */
72
    protected function makePrimaryKey(string $key): array
73
    {
74
        return [
75
            ['key', $key],
76
            ['prefix', $this->getPrefix()],
77
            ['type', ($this->type ?? 'cache')],
78
        ];
79
    }
80
81
    /**
82
     * @param  mixed  $value
83
     * @param  int|null  $seconds
84
     *
85
     * @return array[]
86
     */
87
    protected function makeAttributeColumns($value, ?int $seconds = null): array
88
    {
89
        $columns = [];
90
91
        $columns[] = ['created_at', $this->ots->availableDate(), ColumnTypeConst::CONST_STRING];
92
93
        if (null !== $value) {
94
            $columns[] = ['value', $this->serialize($value), ColumnTypeConst::CONST_BINARY];
95
        }
96
97
        if (null !== $seconds) {
98
            $columns[] = ['expiration', $this->availableAt($seconds), ColumnTypeConst::CONST_INTEGER];
99
        }
100
101
        if (null !== $this->owner) {
102
            $columns[] = ['owner', $this->owner, ColumnTypeConst::CONST_STRING];
103
        }
104
105
        return $columns;
106
    }
107
108
    /**
109
     * @param  array  $response
110
     * @return mixed|null
111
     */
112
    protected function parseValueInOtsResponse(array $response)
113
    {
114
        $columns = $this->getOts()->parseRowColumns($response);
115
116
        if (!isset($columns['value'])) {
117
            return null;
118
        }
119
120
        if (isset($columns['expiration']) && $this->currentTime() >= $columns['expiration']) {
121
            return null;
122
        }
123
124
        return $this->unserialize($columns['value']);
125
    }
126
127
    /**
128
     * @param  array  $response
129
     *
130
     * @return string|null
131
     */
132
    protected function parseKeyInOtsResponse(array $response): ?string
133
    {
134
        $columns = $this->getOts()->parseRowColumns($response);
135
        return $columns['key'] ?? null;
136
    }
137
138
    /**
139
     * Serialize the value.
140
     *
141
     * @param  mixed  $value
142
     * @return string
143
     */
144
    protected function serialize($value): string
145
    {
146
        if (is_numeric($value) && !in_array($value, [INF, -INF]) && !is_nan($value)) {
147
            return is_string($value) ? $value : strval($value);
148
        }
149
150
        return serialize($value);
151
    }
152
153
    /**
154
     * Unserialize the value.
155
     *
156
     * @param  mixed  $value
157
     * @return mixed
158
     */
159
    protected function unserialize($value)
160
    {
161
        return is_numeric($value) ? $value : unserialize($value);
162
    }
163
}
164