Passed
Push — master ( 9d8bb8...60c6b0 )
by Petr
03:23
created

Event::removeImage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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 int
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
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Image")
56
     * @ORM\JoinColumn(name="image_id", referencedColumnName="id")
57
     */
58
59
    /**
60
     * @var Image[]|ArrayCollection
61
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Image", orphanRemoval=true)
62
     * @ORM\JoinTable(name="event_images",
63
     *      joinColumns={@ORM\JoinColumn(name="event_id", referencedColumnName="id")},
64
     *      inverseJoinColumns={@ORM\JoinColumn(name="image_name", referencedColumnName="name", unique=true)}
65
     *      )
66
     */
67
    private $images;
68
69 2
    public function __construct(
70
        string $name,
71
        User $creator,
72
        \DateTime $date,
73
        string $description
74
    )
75
    {
76 2
        $this->name = $name;
77 2
        $this->creator = $creator;
78 2
        $this->date = $date;
79 2
        $this->description = $description;
80 2
        $this->id = HashGenerator::generate();
0 ignored issues
show
Documentation Bug introduced by
The property $id was declared of type integer, but \AppBundle\Service\HashGenerator::generate() is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
81 2
        $this->images = new ArrayCollection();
82 2
    }
83
84 6
    public function getDate(): string
85
    {
86 6
        return $this->date->format('Y-m-d H:i');
87
    }
88
89 10
    public function getId(): string
90
    {
91 10
        return $this->id;
92
    }
93
94 1
    public function setDate(\DateTime $date)
95
    {
96 1
        $this->date = $date;
97 1
    }
98
99 6
    public function getName(): string
100
    {
101 6
        return $this->name;
102
    }
103
104 1
    public function setName(string $name)
105
    {
106 1
        $this->name = $name;
107 1
    }
108
109 6
    public function getDescription(): string
110
    {
111 6
        return $this->description;
112
    }
113
114 1
    public function setDescription(string $description)
115
    {
116 1
        $this->description = $description;
117 1
    }
118
119
    /**
120
     * @return Image[]|PersistentCollection
121
     */
122 7
    public function getImages()
123
    {
124 7
        return $this->images;
125
    }
126
127
    /**
128
     * @return Image|null
129
     */
130 1
    public function getImageWithName(string $imageName)
131
    {
132 1
        $criteria = Criteria::create()->where(Criteria::expr()->eq('name', $imageName));
133 1
        $criteria->setMaxResults(1);
134
135
        /** @var ArrayCollection $imagesCollection */
136 1
        $imagesCollection = $this->images->matching($criteria);
137
138 1
        return $imagesCollection->first() ?: null;
139
    }
140
141 2
    public function addImage(Image $image)
142
    {
143 2
        $this->images->add($image);
144 2
    }
145
146 1
    public function removeImage(Image $image)
147
    {
148 1
        $this->images->removeElement($image);
149 1
    }
150
}
151