Completed
Push — master ( 8c886d...91409e )
by Ben
03:10
created

PageKey::fromString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 14
    public function __construct($key)
13
    {
14 14
        $this->validateKey($key);
15
16 12
        $this->key = $this->sanitizeKey($key);
17 12
    }
18
19 1
    public static function fromString($key)
20
    {
21 1
        return new self($key);
22
    }
23
24 9
    public static function fromLineKeyString($key)
25
    {
26 9
        $key = false !== strpos($key,'.') ? substr($key, 0, strpos($key, '.')) : $key;
27 9
        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 10
    public function isExcludedSource()
46
    {
47 10
        $excluded = $this->getExcludedSources();
48
49 10
        return in_array($this->key,$excluded);
50
    }
51
52 12
    private function sanitizeKey($key)
53
    {
54 12
        return strtolower($key);
55
    }
56
57 14
    private function validateKey($key)
58
    {
59 14 View Code Duplication
        if (!$key || !is_string($key) || false !== strpos($key, '.')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 12
    }
63
64 10
    private function getExcludedSources()
65
    {
66 10
        if(!self::$excludedSources)
67
        {
68 1
            self::$excludedSources = config('squanto.excluded_files',[]);
69
        }
70
71 10
        return self::$excludedSources;
72
    }
73
74
}
75