Trail::setPosts()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
ccs 3
cts 3
cp 1
cc 1
nc 1
nop 1
crap 1
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 Ramsey\Uuid\Exception\InvalidUuidStringException;
12
13
/**
14
 * Class Trail
15
 * @package DavisPeixoto\BlogCore\Entity
16
 */
17
class Trail extends AbstractEntity
18
{
19
    /**
20
     * @var string
21
     */
22
    private $name;
23
24
    /**
25
     * @var string
26
     */
27
    private $description;
28
29
    /**
30
     * @var array|Post[]
31
     */
32
    private $posts;
33
34
    /**
35
     * Trail constructor.
36
     * @param string $name
37
     * @param string $description
38
     * @param string|null $id
39
     * @param Post[] $posts
40
     * @throws InvalidUuidStringException
41
     */
42 9
    public function __construct(string $name, string $description, string $id = null, array $posts = [])
43
    {
44 9
        $this->setId($id);
45 9
        $this->setName($name);
46 9
        $this->setDescription($description);
47 9
        $this->setPosts($posts);
48 9
    }
49
50
    /**
51
     * @codeCoverageIgnore
52
     * @return string
53
     */
54
    public function getName(): string
55
    {
56
        return $this->name;
57
    }
58
59
    /**
60
     * @codeCoverageIgnore
61
     * @param string $name
62
     */
63
    public function setName(string $name)
64
    {
65
        $this->name = $name;
66
    }
67
68
    /**
69
     * @codeCoverageIgnore
70
     * @return string
71
     */
72
    public function getDescription(): string
73
    {
74
        return $this->description;
75
    }
76
77
    /**
78
     * @codeCoverageIgnore
79
     * @param string $description
80
     */
81
    public function setDescription(string $description)
82
    {
83
        $this->description = $description;
84
    }
85
86
    /**
87
     * @return Post[]
88
     */
89 6
    public function getPosts(): array
90
    {
91 6
        return $this->posts;
92
    }
93
94
    /**
95
     * @param Post[] $posts
96
     */
97 9
    public function setPosts($posts)
98
    {
99 9
        $this->posts = array_unique($posts, SORT_REGULAR);
100 9
    }
101
102
    /**
103
     * @param Post $post
104
     * @return $this
105
     */
106 3
    public function addPost(Post $post)
107
    {
108 3
        $posts = $this->getPosts();
109 3
        $posts[] = $post;
110 3
        $this->setPosts($posts);
111
112 3
        return $this;
113
    }
114
115
    /**
116
     * @param Post $post
117
     * @return $this
118
     */
119 3
    public function removePost(Post $post)
120
    {
121 3
        foreach ($this->getPosts() as $key => $value) {
122 3
            if ($value->getId() === $post->getId()) {
123 2
                unset($this->posts[$key]);
124 2
                sort($this->posts);
125
126 3
                return $this;
127
            }
128
        }
129
130 1
        return $this;
131
    }
132
}
133