Passed
Pull Request — develop (#2)
by Andreas
03:58 queued 01:12
created

CacheKey::getCacheKeyPrefix()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 25
c 0
b 0
f 0
ccs 12
cts 12
cp 1
rs 8.5806
cc 4
eloc 12
nc 4
nop 1
crap 4
1
<?php
2
namespace Wambo\Catalog\Cache;
3
4
use Wambo\Core\Module\Exception\InvalidArgumentException;
5
use Wambo\Core\ValueObject\ValueObjectInterface;
6
use Wambo\Core\ValueObject\ValueObjectTrait;
7
8
/**
9
 * Class CacheKey represents a single cache key used to identify elements in a cache storage.
10
 *
11
 * @package Wambo\Catalog\Cache
12
 */
13
class CacheKey implements ValueObjectInterface
14
{
15
    use ValueObjectTrait;
16
    private $value;
17
18
    /**
19
     * Create a new cache key instance for the given element name and tags.
20
     *
21
     * @param string    $elementName A name of the element that you want to cache
22
     * @param \string[] $tags        A list of tags that help to categorize the element you want to cache
23
     *
24
     * @return CacheKey
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
25
     */
26 17
    function __construct(string $elementName, string ...$tags)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
27
    {
28 17
        $key = sprintf("%s--%s", $this->getCacheKeyPrefix(...$tags), $this->getCleanedName($elementName));
29 12
        $this->value = $key;
30
31 12
        $this->setConstructed();
32 12
    }
33
34
    /**
35
     * Get the value of the cache key
36
     *
37
     * @return string
38
     */
39 6
    public function getValue()
40
    {
41 6
        return $this->value;
42
    }
43
44
    /**
45
     * Get a cache key prefix from the given tags (default: "none")
46
     *
47
     * @param \string[] ...$tags A list of tags that help to categorize the element you want to cache
48
     *
49
     * @return string
50
     */
51 17
    private function getCacheKeyPrefix(string ...$tags): string
52
    {
53 17
        if (count($tags) === 0) {
54 11
            return "none"; // default prefix if no tags were given
55
        }
56
57
        // sort the tags alphabetically
58 6
        sort($tags);
59
60
        // assemble the prefix
61 6
        $prefix = "";
62 6
        foreach ($tags as $tag) {
63
64 6
            $cleanedTag = $this->getCleanedName($tag);
65
66 3
            if (strlen($prefix) == 0) {
67 3
                $prefix = "$cleanedTag";
68 3
                continue;
69
            }
70
71 2
            $prefix = "$prefix,$cleanedTag";
72
        }
73
74 3
        return $prefix;
75
    }
76
77
    /**
78
     * Get a cleaned cache key name for the given element name
79
     *
80
     * @param string $elementName
81
     *
82
     * @return string
83
     *
84
     * @throws InvalidArgumentException If the given element name is empty
85
     */
86 17
    private function getCleanedName(string $elementName): string
87
    {
88 17
        $trimmedElementName = trim($elementName);
89 17
        if (strlen($trimmedElementName) == 0) {
90 5
            throw new InvalidArgumentException("The element name cannot be empty");
91
        }
92
93 12
        return strtolower($trimmedElementName);
94
    }
95
}