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

LineKey::isExcludedSource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Thinktomorrow\Squanto\Domain;
4
5
use Thinktomorrow\Squanto\Exceptions\InvalidLineKeyException;
6
7
class LineKey
8
{
9
    private $key;
10
11 62
    public function __construct($key)
12
    {
13 62
        $this->validateKey($key);
14
15 56
        $this->key = $this->sanitizeKey($key);
16 56
    }
17
18 26
    public static function fromString($key)
19
    {
20 26
        return new self($key);
21
    }
22
23 18
    public function get()
24
    {
25 18
        return $this->key;
26
    }
27
28 26
    /**
29
     * Get suggestion for a label based on the key
30
     *
31 26
     * @return string
32
     */
33 44
    public function getAsLabel()
34
    {
35 26
        // Remove first part since that part equals the page
36 18
        $key = substr($this->key, strpos($this->key, '.')+1);
37
38 18
        $label = str_replace('.', ' ', $key);
39
40 18
        return ucfirst($label);
41
    }
42
43
    /**
44 30
     * Get Page identifier.
45
     * This is the first segment of the key
46 30
     *
47
     * @return string
48
     */
49 54
    public function getPageKey()
50
    {
51 54
        return substr($this->key, 0, strpos($this->key, '.'));
52
    }
53
54 60
    private function sanitizeKey($key)
55
    {
56 60
        return strtolower($key);
57 4
    }
58
59 58
    private function validateKey($key)
60
    {
61 24 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...
62 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');
63
        }
64 22
    }
65
66
}
67