Passed
Push — master ( 88d9d6...ec4c8d )
by Tom
04:42
created

Reference   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 105
ccs 23
cts 23
cp 1
rs 10
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
A getType() 0 3 1
A parse() 0 16 3
A getName() 0 3 1
A __construct() 0 3 1
A getPipelinesType() 0 3 2
A valid() 0 5 1
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Runner;
6
7
use InvalidArgumentException;
8
use Ktomk\Pipelines\File\ReferenceTypes;
9
10
class Reference
11
{
12
    const TRIGGER = '~^(branch|tag|bookmark|pr):(.+)$~';
13
14
    private $string;
15
16
    private $type;
17
18
    /**
19
     * @var string
20
     */
21
    private $value;
22
23
    private static $map = array(
24
        'bookmark' => ReferenceTypes::REF_BOOKMARKS,
25
        'branch' => ReferenceTypes::REF_BRANCHES,
26
        'pr' => ReferenceTypes::REF_PULL_REQUESTS,
27
        'tag' => ReferenceTypes::REF_TAGS,
28
    );
29
30
    /**
31
     * @param null|string $string [optional] use null for a null reference
32
     *
33
     * @throws InvalidArgumentException
34
     *
35
     * @return Reference
36
     */
37 10
    public static function create($string = null)
38
    {
39 10
        return new self($string);
40
    }
41
42
    /**
43
     * Validates if a string is a valid (non-null) reference
44
     *
45
     * @param $string
46
     *
47
     * @return bool
48
     */
49 8
    public static function valid($string)
50
    {
51 8
        $result = preg_match(self::TRIGGER, $string);
52
53 8
        return (bool)$result;
54
    }
55
56
    /**
57
     * Reference constructor
58
     *
59
     * @param null|string $string [optional] use null for a null reference
60
     *
61
     * @throws InvalidArgumentException
62
     */
63 11
    public function __construct($string = null)
64
    {
65 11
        $this->parse($string);
66 7
    }
67
68
    /**
69
     * @return null|string
70
     */
71 4
    public function getType()
72
    {
73 4
        return $this->type;
74
    }
75
76
    /**
77
     * @return null|string
78
     */
79 1
    public function getPipelinesType()
80
    {
81 1
        return $this->type ? self::$map[$this->type] : null;
82
    }
83
84
    /**
85
     * @return string
86
     */
87 4
    public function getName()
88
    {
89 4
        return $this->value;
90
    }
91
92
    /**
93
     * @param null|string $string
94
     *
95
     * @throws InvalidArgumentException
96
     *
97
     * @return void
98
     */
99 11
    private function parse($string)
100
    {
101 11
        $this->string = $string;
102
103 11
        if (null === $string) {
104 4
            return;
105
        }
106
107 8
        $result = preg_match(self::TRIGGER, $string, $matches);
108
109 8
        if (!$result) {
110 4
            throw new InvalidArgumentException(sprintf('invalid reference: "%s"', $string));
111
        }
112
113 4
        $this->type = $matches[1];
114 4
        $this->value = $matches[2];
115 4
    }
116
}
117