HasLayoutKey   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 56
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isUseKey() 0 3 2
A key() 0 3 1
A generateValidLayoutKey() 0 15 3
A inUseKey() 0 3 1
1
<?php
2
3
namespace NovaFlexibleContent\Layouts\LayoutTraits;
4
5
use Illuminate\Support\Str;
6
7
trait HasLayoutKey
8
{
9
    /**
10
     * The layout's unique identifier.
11
     */
12
    protected ?string $key = null;
13
14
    /**
15
     * The layout's temporary identifier.
16
     */
17
    protected ?string $_key = null;
18
19
    /**
20
     * Retrieve the group's unique key.
21
     */
22 15
    public function key(): ?string
23
    {
24 15
        return $this->key;
25
    }
26
27
    /**
28
     * Retrieve the key currently in use in the views.
29
     */
30 14
    public function inUseKey(): ?string
31
    {
32 14
        return $this->_key ?? $this->key();
33
    }
34
35
    /**
36
     * Check if this group matches the given key.
37
     */
38 6
    public function isUseKey(string $groupKey): bool
39
    {
40 6
        return $this->key === $groupKey || $this->_key === $groupKey;
41
    }
42
43
    /**
44
     * Returns an unique key for this group if it's not already the case.
45
     *
46
     * @throws \Exception
47
     */
48 16
    protected function generateValidLayoutKey(string $key): string
49
    {
50 16
        $keyLen = 16;
51
52 16
        if (!str_contains($key, '-')
53 16
            && strlen($key) === $keyLen) {
54 16
            return $key;
55
        }
56
57
        // The key is either generated by Javascript or not strong enough.
58
        // Before assigning a new valid key we'll keep track of this one
59
        // in order to keep it usable in a Flexible::findGroup($key) search.
60 2
        $this->_key = $key;
61
62 2
        return 'c' . Str::random($keyLen-7) . date('ymd');
63
    }
64
}
65