Completed
Push — master ( 7b4929...cefe8d )
by Andrii
01:50
created

Builder   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 24
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 153
ccs 0
cts 67
cp 0
rs 10

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setHistory() 0 4 2
A getHistory() 0 4 1
A setTag() 0 5 1
A setNote() 0 5 1
A setHash() 0 7 2
A getTag() 0 4 1
A getNote() 0 4 1
A getHash() 0 4 1
A addHeader() 0 4 1
A addTag() 0 6 1
A addNote() 0 5 1
A addCommit() 0 8 2
A addComment() 0 4 1
A addLink() 0 4 1
A getLastTag() 0 4 1
A getInitTag() 0 4 1
B skipCommit() 0 27 4
1
<?php
2
/**
3
 * Changelog keeper
4
 *
5
 * @link      https://github.com/hiqdev/chkipper
6
 * @package   chkipper
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\chkipper\history;
12
13
/**
14
 * History builder class.
15
 *
16
 * @author Andrii Vasyliev <[email protected]>
17
 */
18
class Builder
19
{
20
    /**
21
     * @var string current tag
22
     */
23
    protected $_tag = '';
24
25
    /**
26
     * @var string current note
27
     */
28
    protected $_note = '';
29
30
    /**
31
     * @var string current commit's hash
32
     */
33
    protected $_hash = '';
34
35
    /**
36
     * @var ConfigInterface config object
37
     */
38
    protected $_config;
39
40
    /**
41
     * @var History history object
42
     */
43
    protected $_history;
44
45
    public function __construct(ConfigInterface $config, History $history = null)
46
    {
47
        $this->_config = $config;
48
        $this->setHistory($history);
49
    }
50
51
    public function setHistory(History $history = null)
52
    {
53
        $this->_history = is_null($history) ? new History($this->_config) : $history;
54
    }
55
56
    public function getHistory()
57
    {
58
        return $this->_history;
59
    }
60
61
    public function setTag($tag)
62
    {
63
        $this->_tag  = $tag;
64
        $this->setNote('');
65
    }
66
67
    public function setNote($note)
68
    {
69
        $this->_note = $note;
70
        $this->setHash('');
71
    }
72
73
    public function setHash($hash)
74
    {
75
        if ($hash) {
76
            $this->_hash = (string) $hash;
77
            $this->getHistory()->addHash($hash);
78
        }
79
    }
80
81
    public function getTag()
82
    {
83
        return $this->_tag;
84
    }
85
86
    public function getNote()
87
    {
88
        return $this->_note;
89
    }
90
91
    public function getHash()
92
    {
93
        return $this->_hash;
94
    }
95
96
    public function addHeader($header)
97
    {
98
        $this->getHistory()->addHeader($header);
99
    }
100
101
    public function addTag($tag, $date = null)
102
    {
103
        $this->setTag($tag);
104
105
        return $this->getHistory()->addTag(new Tag($tag, $date));
106
    }
107
108
    public function addNote($note)
109
    {
110
        $this->setNote($note);
111
        $this->getHistory()->findTag($this->getTag())->findNote($note);
112
    }
113
114
    public function addCommit($hash, $label)
115
    {
116
        $commit = new Commit($hash, $label);
117
        if (!$this->skipCommit($commit)) {
118
            $this->setHash($hash);
119
            $this->getHistory()->findTag($this->getTag())->findNote($this->getNote())->findCommit($hash)->setLabel($label);
120
        }
121
    }
122
123
    public function addComment($comment)
124
    {
125
        $this->getHistory()->findTag($this->getTag())->findNote($this->getNote())->findCommit($this->getHash())->addComment($comment);
126
    }
127
128
    public function addLink($link, $href)
129
    {
130
        $this->getHistory()->addLink($link, $href);
131
    }
132
133
    public function getLastTag()
134
    {
135
        return $this->getHistory()->lastTag;
136
    }
137
138
    public function getInitTag()
139
    {
140
        return $this->getHistory()->initTag;
141
    }
142
143
    public static function skipCommit(Commit $commit)
144
    {
145
        $subject = $commit->getSubject();
146
147
        static $equals = [
148
            ''       => 1,
149
            'bump'   => 1,
150
            'minor'  => 1,
151
            'bumped' => 1,
152
        ];
153
        if (isset($equals[$subject])) {
154
            return true;
155
        }
156
157
        static $starts = [
158
            'version bump',
159
            'bumped version',
160
            "merge branch 'master'",
161
        ];
162
        foreach ($starts as $start) {
163
            if (strtolower(substr($subject, 0, strlen($start))) === $start) {
164
                return true;
165
            }
166
        }
167
168
        return false;
169
    }
170
}
171