Passed
Push — master ( 253018...166e4e )
by Davis
38s
created

Post::setAuthor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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