Completed
Push — master ( 35ba31...f8a85f )
by Ben
05:06
created

LineKey::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Thinktomorrow\Squanto\Domain;
4
5
use Thinktomorrow\Squanto\Exceptions\InvalidLineKeyException;
6
7
class LineKey
8
{
9
    private static $excludedSources;
10
    private $key;
11 57
12
    public function __construct($key)
13 57
    {
14
        $this->validateKey($key);
15 51
16 51
        $this->key = $this->sanitizeKey($key);
17
    }
18 39
19
    public static function fromString($key)
20 39
    {
21
        return new self($key);
22
    }
23
24
    public function get()
25
    {
26
        return $this->key;
27
    }
28 39
29
    /**
30
     * Get suggestion for a label based on the key
31 39
     *
32
     * @return string
33 39
     */
34
    public function getAsLabel()
35 39
    {
36
        // Remove first part since that part equals the page
37
        $key = substr($this->key, strpos($this->key, '.')+1);
38
39
        $label = str_replace('.', ' ', $key);
40
41
        return ucfirst($label);
42
    }
43
44 45
    /**
45
     * Get Page identifier.
46 45
     * This is the first segment of the key
47
     *
48
     * @return string
49 51
     */
50
    public function getPageKey()
51 51
    {
52
        return substr($this->key, 0, strpos($this->key, '.'));
53
    }
54 57
55
    public function isExcludedSource()
56 57
    {
57 6
        $excluded = $this->getExcludedSources();
58
59 51
        return in_array($this->getPageKey(),$excluded);
60
    }
61
62
    private function sanitizeKey($key)
63
    {
64
        return strtolower($key);
65
    }
66
67
    private function validateKey($key)
68
    {
69
        if (!$key || !is_string($key) || false === strpos($key, '.')) {
70
            throw new InvalidLineKeyException('Invalid LineKey format ['.$key.', type: '.gettype($key).'] given. Must be a string containing at least two dot separated segments. E.g. about.title');
71
        }
72
    }
73
74
    private function getExcludedSources()
75
    {
76
        if(!self::$excludedSources)
77
        {
78
            self::$excludedSources = config('squanto.excluded_files',[]);
79
        }
80
81
        return self::$excludedSources;
82
    }
83
84
}
85