Completed
Push — master ( 7fd037...272fda )
by Ben
09:19
created

LineKey::getExcludedSources()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
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 38
12 32
    public function __construct($key)
13 38
    {
14 32
        $this->validateKey($key);
15 34
16 64
        $this->key = $this->sanitizeKey($key);
17 30
    }
18 26
19 9
    public static function fromString($key)
20 26
    {
21 9
        return new self($key);
22
    }
23
24 17
    public function get()
25
    {
26 17
        return $this->key;
27
    }
28 26
29
    /**
30
     * Get suggestion for a label based on the key
31 26
     *
32
     * @return string
33 26
     */
34 17
    public function getAsLabel()
35 26
    {
36
        // Remove first part since that part equals the page
37 17
        $key = substr($this->key, strpos($this->key, '.')+1);
38
39 17
        $label = str_replace('.', ' ', $key);
40
41 17
        return ucfirst($label);
42
    }
43
44 30
    /**
45
     * Get Page identifier.
46 30
     * This is the first segment of the key
47
     *
48
     * @return string
49 34
     */
50 28
    public function getPageKey()
51 34
    {
52 28
        return substr($this->key, 0, strpos($this->key, '.'));
53
    }
54 38
55 9
    public function isExcludedSource()
56 38
    {
57 13
        $excluded = $this->getExcludedSources();
58
59 43
        return in_array($this->getPageKey(),$excluded);
60
    }
61
62 30
    private function sanitizeKey($key)
63
    {
64 30
        return strtolower($key);
65
    }
66
67 32
    private function validateKey($key)
68
    {
69 32
        if (!$key || !is_string($key) || false === strpos($key, '.')) {
70 2
            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 30
    }
73
74 9
    private function getExcludedSources()
75
    {
76 9
        if(!self::$excludedSources)
77
        {
78 1
            self::$excludedSources = config('squanto.excluded_files',[]);
79
        }
80
81 9
        return self::$excludedSources;
82
    }
83
84
}
85