Passed
Push — master ( eb296b...63b45b )
by Petr
03:53
created

Event::getLinks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace AppBundle\Entity;
4
5
use AppBundle\Entity\Infrasctucture\CreatorLoginTrait;
6
use AppBundle\Service\HashGenerator;
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Criteria;
9
use Doctrine\ORM\Mapping as ORM;
10
use Doctrine\ORM\PersistentCollection;
11
12
/**
13
 * @ORM\Table(name="events", uniqueConstraints={@ORM\UniqueConstraint(name="unique_events_date_name", columns={"date", "name"})})
14
 * @ORM\Entity(repositoryClass="AppBundle\Entity\Repository\EventRepository")
15
 * @author Vehsamrak
16
 */
17
class Event
18
{
19
20
    use CreatorLoginTrait;
21
22
    /**
23
     * @var string
24
     * @ORM\Column(name="id", type="string", length=8)
25
     * @ORM\Id
26
     */
27
    private $id;
28
29
    /**
30
     * @var \DateTime
31
     * @ORM\Column(name="date", type="datetime")
32
     */
33
    private $date;
34
35
    /**
36
     * @var string
37
     * @ORM\Column(name="name", type="string", length=255)
38
     */
39
    private $name;
40
41
    /**
42
     * @var string
43
     * @ORM\Column(name="description", type="text")
44
     */
45
    private $description;
46
47
    /**
48
     * @var User
49
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="events")
50
     * @ORM\JoinColumn(name="creator", referencedColumnName="login")
51
     */
52
    private $creator;
53
54
    /**
55
     * @var Image[]|ArrayCollection
56
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Image")
57
     * @ORM\JoinTable(name="event_images",
58
     *      joinColumns={@ORM\JoinColumn(name="event_id", referencedColumnName="id")},
59
     *      inverseJoinColumns={@ORM\JoinColumn(name="image_name", referencedColumnName="name", unique=true, onDelete="CASCADE")}
60
     *      )
61
     */
62
    private $images;
63
64
    /**
65
     * @var Link[]|ArrayCollection
66
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Link")
67
     * @ORM\JoinTable(name="event_links",
68
     *      joinColumns={@ORM\JoinColumn(name="event_id", referencedColumnName="id")},
69
     *      inverseJoinColumns={@ORM\JoinColumn(name="link_url", referencedColumnName="url", unique=true, onDelete="CASCADE")}
70
     *      )
71
     */
72
    private $links;
73
74 2
    public function __construct(
75
        string $name,
76
        User $creator,
77
        \DateTime $date,
78
        string $description,
79
        HashGenerator $hashGenerator = null
80
    )
81
    {
82 2
        $this->name = $name;
83 2
        $this->creator = $creator;
84 2
        $this->date = $date;
85 2
        $this->description = $description;
86 2
        $this->id = $hashGenerator ? $hashGenerator::generate() : HashGenerator::generate();
87 2
        $this->images = new ArrayCollection();
88 2
        $this->links = new ArrayCollection();
89 2
    }
90
91 7
    public function getDate(): string
92
    {
93 7
        return $this->date->format('Y-m-d H:i');
94
    }
95
96 13
    public function getId(): string
97
    {
98 13
        return $this->id;
99
    }
100
101 1
    public function setDate(\DateTime $date)
102
    {
103 1
        $this->date = $date;
104 1
    }
105
106 7
    public function getName(): string
107
    {
108 7
        return $this->name;
109
    }
110
111 1
    public function setName(string $name)
112
    {
113 1
        $this->name = $name;
114 1
    }
115
116 7
    public function getDescription(): string
117
    {
118 7
        return $this->description;
119
    }
120
121 1
    public function setDescription(string $description)
122
    {
123 1
        $this->description = $description;
124 1
    }
125
126 5
    public function getCreator(): User
127
    {
128 5
        return $this->creator;
129
    }
130
131
    /**
132
     * @return Image[]|PersistentCollection|ArrayCollection
133
     */
134 9
    public function getImages()
135
    {
136 9
        return $this->images;
137
    }
138
139
    /**
140
     * @return Link[]|PersistentCollection|ArrayCollection
141
     */
142 7
    public function getLinks()
143
    {
144 7
        return $this->links;
145
    }
146
147
    /**
148
     * @return Image|null
149
     */
150 1
    public function getImageWithName(string $imageName)
151
    {
152 1
        $criteria = Criteria::create()->where(Criteria::expr()->eq('name', $imageName));
153 1
        $criteria->setMaxResults(1);
154
155
        /** @var ArrayCollection $imagesCollection */
156 1
        $imagesCollection = $this->images->matching($criteria);
157
158 1
        return $imagesCollection->first() ?: null;
159
    }
160
161 2
    public function addImage(Image $image)
162
    {
163 2
        $this->images->add($image);
164 2
    }
165
166 1
    public function removeImage(Image $image)
167
    {
168 1
        $this->images->removeElement($image);
169 1
    }
170
171 1
    public function addLink(Link $link)
172
    {
173 1
        $this->links->add($link);
174 1
    }
175
176
    public function removeLink(Link $link)
177
    {
178
        $this->links->removeElement($link);
179
    }
180
}
181