Post   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 0
loc 170
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A getTitle() 0 4 1
A setTitle() 0 4 1
A getBody() 0 4 1
A setBody() 0 4 1
A getPublishDate() 0 4 1
A setPublishDate() 0 4 1
A getAuthor() 0 4 1
A setAuthor() 0 4 1
A getTags() 0 4 1
A setTags() 0 4 1
A addTag() 0 8 1
A removeTag() 0 13 3
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: davis
5
 * Date: 7/23/17
6
 * Time: 3:04 AM
7
 */
8
9
namespace DavisPeixoto\BlogCore\Entity;
10
11
use DateTime;
12
use Ramsey\Uuid\Exception\InvalidUuidStringException;
13
14
/**
15
 * Class Post
16
 * @package DavisPeixoto\BlogCore\Entity
17
 */
18
class Post extends AbstractEntity
19
{
20
    /**
21
     * @var string $title
22
     */
23
    private $title;
24
25
    /**
26
     * @var string $body
27
     */
28
    private $body;
29
30
    /**
31
     * @var DateTime|null
32
     */
33
    private $publishDate;
34
35
    /**
36
     * @var Author
37
     */
38
    private $author;
39
40
    /**
41
     * @var Tag[]
42
     */
43
    private $tags;
44
45
    /**
46
     * Post constructor.
47
     * @param string $title
48
     * @param string $body
49
     * @param Author $authorId
50
     * @param string|null $id
51
     * @param Tag[] $tags
52
     * @param DateTime|null $publishDate
53
     * @throws InvalidUuidStringException
54
     */
55 21
    public function __construct(
56
        string $title,
57
        string $body,
58
        Author $authorId,
59
        string $id = null,
60
        array $tags = [],
61
        DateTime $publishDate = null
62
    ) {
63 21
        $this->setId($id);
64 21
        $this->setTitle($title);
65 21
        $this->setBody($body);
66 21
        $this->setPublishDate($publishDate);
67 21
        $this->setAuthor($authorId);
68 21
        $this->setTags($tags);
69 21
    }
70
71
    /**
72
     * @codeCoverageIgnore
73
     * @return string
74
     */
75
    public function getTitle(): string
76
    {
77
        return $this->title;
78
    }
79
80
    /**
81
     * @codeCoverageIgnore
82
     * @param string $title
83
     */
84
    public function setTitle(string $title)
85
    {
86
        $this->title = $title;
87
    }
88
89
    /**
90
     * @codeCoverageIgnore
91
     * @return string
92
     */
93
    public function getBody(): string
94
    {
95
        return $this->body;
96
    }
97
98
    /**
99
     * @codeCoverageIgnore
100
     * @param string $body
101
     */
102
    public function setBody(string $body)
103
    {
104
        $this->body = $body;
105
    }
106
107
    /**
108
     * @return DateTime|null
109
     */
110 2
    public function getPublishDate()
111
    {
112 2
        return $this->publishDate;
113
    }
114
115
    /**
116
     * @param DateTime|null $publishDate
117
     */
118 21
    public function setPublishDate(DateTime $publishDate = null)
119
    {
120 21
        $this->publishDate = $publishDate;
121 21
    }
122
123
    /**
124
     * @codeCoverageIgnore
125
     * @return Author
126
     */
127
    public function getAuthor(): Author
128
    {
129
        return $this->author;
130
    }
131
132
    /**
133
     * @codeCoverageIgnore
134
     * @param Author $author
135
     */
136
    public function setAuthor(Author $author)
137
    {
138
        $this->author = $author;
139
    }
140
141
    /**
142
     * @return Tag[]
143
     */
144 6
    public function getTags(): array
145
    {
146 6
        return $this->tags;
147
    }
148
149
    /**
150
     * @param Tag[]|array $tags
151
     */
152 21
    public function setTags(array $tags = [])
153
    {
154 21
        $this->tags = array_unique($tags, SORT_REGULAR);
155 21
    }
156
157
    /**
158
     * @param Tag $tag
159
     * @return $this
160
     */
161 3
    public function addTag(Tag $tag)
162
    {
163 3
        $tags = $this->getTags();
164 3
        $tags[] = $tag;
165 3
        $this->setTags($tags);
166
167 3
        return $this;
168
    }
169
170
    /**
171
     * @param Tag $tag
172
     * @return $this
173
     */
174 3
    public function removeTag(Tag $tag)
175
    {
176 3
        foreach ($this->getTags() as $key => $value) {
177 3
            if ($value->getId() === $tag->getId()) {
178 2
                unset($this->tags[$key]);
179 2
                sort($this->tags);
180
181 3
                return $this;
182
            }
183
        }
184
185 1
        return $this;
186
    }
187
}
188