RedisTaggedScripts   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 61
c 0
b 0
f 0
wmc 6
lcom 0
ccs 24
cts 24
cp 1
cbo 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getScriptBody() 0 16 3
A getScriptDigest() 0 22 3
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\RedisTaggedCache\Scripts;
4
5
/**
6
 * Copyright 2015-2019 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use function assert;
22
23
/**
24
 * @package Limoncello\RedisTaggedCache
25
 */
26
class RedisTaggedScripts
27
{
28
    /** @var int Script index */
29
    const ADD_VALUE_SCRIPT_INDEX = 0;
30
31
    /** @var int Script index */
32
    const REMOVE_VALUE_SCRIPT_INDEX = self::ADD_VALUE_SCRIPT_INDEX + 1;
33
34
    /** @var int Script index */
35
    const INVALIDATE_TAG_SCRIPT_INDEX = self::REMOVE_VALUE_SCRIPT_INDEX + 1;
36
37
    /**
38
     * @param int $scriptIndex
39
     *
40
     * @return string
41
     */
42 6
    public static function getScriptBody(int $scriptIndex): string
43
    {
44 6
        $scriptFolder = __DIR__ . DIRECTORY_SEPARATOR;
45
46
        switch ($scriptIndex) {
47 6
            case static::ADD_VALUE_SCRIPT_INDEX:
48 2
                return file_get_contents($scriptFolder . 'Add.lua');
49 4
            case static::REMOVE_VALUE_SCRIPT_INDEX:
50 2
                return file_get_contents($scriptFolder . 'RemoveImpl.lua') .
51 2
                    file_get_contents($scriptFolder . 'Remove.lua');
52
            default:
53 2
                assert($scriptIndex === static::INVALIDATE_TAG_SCRIPT_INDEX);
54 2
                return file_get_contents($scriptFolder . 'RemoveImpl.lua') .
55 2
                    file_get_contents($scriptFolder . 'InvalidateTag.lua');
56
        }
57
    }
58
59
    /**
60
     * @param int $scriptIndex
61
     *
62
     * @return string
63
     */
64 6
    public static function getScriptDigest(int $scriptIndex): string
65
    {
66 6
        assert(
67 6
            $scriptIndex === static::ADD_VALUE_SCRIPT_INDEX ||
68 6
            $scriptIndex === static::REMOVE_VALUE_SCRIPT_INDEX ||
69 6
            $scriptIndex === static::INVALIDATE_TAG_SCRIPT_INDEX
70
        );
71
72
        $sha1 = [
73 6
            static::ADD_VALUE_SCRIPT_INDEX      => '0aa6dbf4cc17ca0a261c4664ef6fe5fe9cd44fd0',
74 6
            static::REMOVE_VALUE_SCRIPT_INDEX   => 'bb69c1c4da3c4ec6afe545e893e5521bc2e7191e',
75 6
            static::INVALIDATE_TAG_SCRIPT_INDEX => '96181c1772cea8dd3a74a09d749ef20bb7f0349f',
76 6
        ][$scriptIndex];
77
78 6
        assert(
79 6
            $sha1 === sha1(static::getScriptBody($scriptIndex)),
80 6
            "Script body with ID `$scriptIndex` do not match expected sha1 " .
81 6
            sha1(static::getScriptBody($scriptIndex))
82
        );
83
84 6
        return $sha1;
85
    }
86
}
87