Tag::addPost()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Blog\Entity;
6
7
use Cycle\Annotated\Annotation\Column;
8
use Cycle\Annotated\Annotation\Entity;
9
use Cycle\Annotated\Annotation\Relation\ManyToMany;
10
use Cycle\Annotated\Annotation\Table;
11
use Cycle\Annotated\Annotation\Table\Index;
12
use Cycle\ORM\Relation\Pivoted\PivotedCollection;
13
use DateTimeImmutable;
14
15
/**
16
 * @Entity(repository="App\Blog\Tag\TagRepository")
17
 * @Table(
18
 *     indexes={
19
 *         @Index(columns={"label"}, unique=true)
20
 *     }
21
 * )
22
 */
23
class Tag
24
{
25
    /**
26
     * @Column(type="primary")
27
     */
28
    private ?int $id = null;
29
30
    /**
31
     * @Column(type="string(255)")
32
     */
33
    private string $label;
34
35
    /**
36
     * @Column(type="datetime")
37
     */
38
    private DateTimeImmutable $created_at;
39
40
    /**
41
     * @ManyToMany(target="App\Blog\Entity\Post", though="PostTag", fkAction="CASCADE", indexCreate=false)
42
     *
43
     * @var PivotedCollection|Post[]
44
     */
45
    private $posts;
46
47
    public function __construct(string $label)
48
    {
49
        $this->label = $label;
50
        $this->created_at = new DateTimeImmutable();
51
        $this->posts = new PivotedCollection();
52
    }
53
54
    public function getId(): ?int
55
    {
56
        return $this->id;
57
    }
58
59
    public function getLabel(): string
60
    {
61
        return $this->label;
62
    }
63
64
    public function setLabel(string $label): void
65
    {
66
        $this->label = $label;
67
    }
68
69
    public function getCreatedAt(): DateTimeImmutable
70
    {
71
        return $this->created_at;
72
    }
73
74
    /**
75
     * @return Post[]
76
     */
77
    public function getPosts(): array
78
    {
79
        return $this->posts->toArray();
80
    }
81
82
    public function addPost(Post $post): void
83
    {
84
        $this->posts->add($post);
85
    }
86
}
87