Tag::addNotes()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4.125

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 2
dl 0
loc 9
ccs 4
cts 8
cp 0.5
crap 4.125
rs 10
c 0
b 0
f 0
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-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\chkipper\lib;
12
13
/**
14
 * History tag.
15
 * Represents a version with notes.
16
 *
17
 * @property string $name
18
 * @property string $name
19
 * @property array  $notes: note => note object
20
 *
21
 * @author Andrii Vasyliev <[email protected]>
22
 */
23
class Tag
24
{
25
    protected $_name;
26
    protected $_date;
27
    protected $_notes = [];
28
29 3
    public function __construct($tag, $date = null, array $notes = [])
30
    {
31 3
        if ($tag instanceof self) {
32
            $this->set($tag);
33
        } else {
34 3
            $this->setName($tag);
35 3
            $this->setDate($date);
36 3
            $this->setNotes($notes);
37
        }
38 3
    }
39
40
    public function set(Tag $tag)
41
    {
42
        $this->setName($tag->getName());
43
        $this->setDate($tag->getDate());
44
        $this->setNotes($tag->getNotes());
45
    }
46
47 3
    public function setName($value)
48
    {
49 3
        if ($value) {
50 3
            $this->_name = $value;
51 3
        }
52 3
    }
53
54 3
    public function getName()
55
    {
56 3
        return $this->_name;
57
    }
58
59
    /**
60
     * Appends note along with commmits.
61
     * @param Note $note
62
     */
63 1
    public function addNote(Note $note)
64
    {
65 1
        $this->findNote($note->getNote())->addCommits($note->getCommits());
66 1
    }
67
68
    /**
69
     * Appends notes along with commits.
70
     * @param Note[] $notes array of notes
71
     * @param boolean $prepend default is append
72
     */
73 3
    public function addNotes(array $notes, $prepend = false)
74
    {
75 3
        if ($prepend) {
76
            $saved = $this->_notes;
77
            $this->_notes = $notes;
78
            $notes = $saved;
79
        }
80 3
        foreach ($notes as $note) {
81 1
            $this->addNote($note);
82 3
        }
83 3
    }
84
85
    /**
86
     * Set notes.
87
     * @param Note[] $notes array of notes
88
     */
89 3
    public function setNotes(array $notes)
90
    {
91 3
        $this->_notes = $notes;
92 3
    }
93
94
    /**
95
     * Sets date.
96
     * Checks if it is date and later then current.
97
     * @param mixed $value date
98
     * @return Tag this
99
     */
100 3
    public function setDate($value)
101
    {
102 3
        $timestamp = strtotime($value);
103 3
        if ($timestamp !== false && $timestamp > $this->_date) {
104 3
            $this->_date = $timestamp;
105 3
        }
106
107 3
        return $this;
108
    }
109
110
    /**
111
     * Unsets date.
112
     */
113 2
    public function unsetDate()
114
    {
115 2
        $this->_date = null;
116
117 2
        return $this;
118
    }
119
120 3
    public function getDate()
121
    {
122 3
        return $this->_date ? date('Y-m-d', $this->_date) : null;
123
    }
124
125 3
    public function findNote($note)
126
    {
127 3
        if (!isset($this->_notes[$note])) {
128 3
            $this->_notes[$note] = new Note($note);
129 3
        }
130
131 3
        return $this->_notes[$note];
132
    }
133
134 3
    public function getNotes()
135
    {
136 3
        return $this->_notes;
137
    }
138
139
    /**
140
     * Finds date of the tag which is the date of the latest commit.
141
     * @return string|null date or null if no commits
142
     */
143 2
    public function findDate()
144
    {
145 2
        $max = '';
146 2
        foreach ($this->getNotes() as $note) {
147 1
            foreach ($note->getCommits() as $commit) {
148 1
                $date = $commit->getDate();
149 1
                if (strcmp($date, $max) > 0) {
150 1
                    $max = $date;
151 1
                }
152 1
            }
153 2
        }
154
155 2
        return $max ?: null;
156
    }
157
}
158