Completed
Push — master ( 5d7782...ea3ea6 )
by Ben
02:41
created

PageKey   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 72
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A fromString() 0 4 1
A fromLineKeyString() 0 5 2
A get() 0 4 1
A getAsLabel() 0 4 1
A isExcludedSource() 0 6 1
A sanitizeKey() 0 4 1
A validateKey() 0 6 4
A getExcludedSources() 0 8 2
A refreshExcludedSources() 0 4 1
1
<?php
2
3
namespace Thinktomorrow\Squanto\Domain;
4
5
use Thinktomorrow\Squanto\Exceptions\InvalidPageKeyException;
6
7
class PageKey
8
{
9
    private static $excludedSources;
10
    private $key;
11
12 17
    public function __construct($key)
13
    {
14 17
        $this->validateKey($key);
15
16 15
        $this->key = $this->sanitizeKey($key);
17 15
    }
18
19 1
    public static function fromString($key)
20
    {
21 1
        return new self($key);
22
    }
23
24 12
    public static function fromLineKeyString($key)
25
    {
26 12
        $key = false !== strpos($key,'.') ? substr($key, 0, strpos($key, '.')) : $key;
27 12
        return new self($key);
28
    }
29
30 1
    public function get()
31
    {
32 1
        return $this->key;
33
    }
34
35
    /**
36
     * Get suggestion for a label based on the key
37
     *
38
     * @return string
39
     */
40 1
    public function getAsLabel()
41
    {
42 1
        return ucfirst($this->key);
43
    }
44
45 13
    public function isExcludedSource()
46
    {
47 13
        $excluded = $this->getExcludedSources();
48
49 13
        return in_array($this->key,$excluded);
50
    }
51
52 15
    private function sanitizeKey($key)
53
    {
54 15
        return strtolower($key);
55
    }
56
57 17
    private function validateKey($key)
58
    {
59 17
        if (!$key || !is_string($key) || false !== strpos($key, '.')) {
60 2
            throw new InvalidPageKeyException('Invalid PageKey format ['.$key.', type: '.gettype($key).'] given. Must be a string without dot separated segments. E.g. about and not about.title');
61
        }
62 15
    }
63
64 13
    private function getExcludedSources()
65
    {
66 13
        if(!self::$excludedSources) {
67 1
            self::$excludedSources = config('squanto.excluded_files',[]);
68
        }
69
70 13
        return self::$excludedSources;
71
    }
72
73 1
    public static function refreshExcludedSources()
74
    {
75 1
        self::$excludedSources = null;
76 1
    }
77
78
}
79