Issues (62)

src/Entity/Collection.php (2 issues)

1
<?php
2
3
namespace App\Entity;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use Gedmo\Mapping\Annotation as Gedmo;
7
use Assert\Assertion;
8
use Assert\AssertionFailedException;
9
use App\Traits\HasTimestamps;
10
use Doctrine\ORM\PersistentCollection;
11
use Ramsey\Uuid\UuidInterface;
12
use Ramsey\Uuid\Uuid;
13
use Swagger\Annotations as SWG;
14
15
/**
16
 * @ORM\Table(name="collection")
17
 * @ORM\HasLifecycleCallbacks()
18
 * @ORM\Entity(repositoryClass="App\Repository\CollectionRepository")
19
 * @Gedmo\SoftDeleteable(fieldName="deletedAt", hardDelete=false)
20
 */
21
class Collection implements \JsonSerializable, UserAwareInterface
22
{
23
    use HasTimestamps;
24
    
25
    /**
26
     * @var UuidInterface
27
     * @ORM\Id()
28
     * @ORM\Column(type="uuid", unique=true)
29
     * @ORM\GeneratedValue(strategy="NONE")
30
     * @SWG\Property(description="UUID", type="string", readOnly=true)
31
     */
32
    private $id;
33
34
    /**
35
     * @ORM\Column(type="string", length=255)
36
     */
37
    private $name;
38
39
    /**
40
     * @ORM\Column(type="string", length=255, nullable=true)
41
     */
42
    private $description;
43
44
    /**
45
     * @ORM\Column(type="datetime", nullable=true)
46
     */
47
    private $deletedAt;
0 ignored issues
show
The private property $deletedAt is not used, and could be removed.
Loading history...
48
49
    /**
50
     * @Gedmo\Slug(fields={"name"})
51
     * @ORM\Column(length=128, unique=true)
52
     */
53
    private $slug;
54
    
55
    /**
56
     * @ORM\OneToMany(targetEntity="App\Entity\ItemCollection", mappedBy="collection")
57
     */
58
    protected $items;
59
60
    /**
61
     * @ORM\ManyToOne(targetEntity="User", inversedBy="collections")
62
     */
63
    private $user;
64
65
    /**
66
     * @param UuidInterface $id
67
     * @param string $name
68
     * @param ?string $description
69
     * @param User $user
70
     * @throws \Assert\AssertionFailedException
71
     */
72 10
    public function __construct(
73
        UuidInterface $id,
74
        string $name,
75
        ?string $description,
76
        User $user
77
        )
78
    {
79 10
        $this->setId($id);
80 10
        $this->setName($name);
81 10
        $this->setDescription($description);
82 10
        $this->setUser($user);
83 10
    }
84
    
85
    /**
86
     * @param UuidInterface $id
87
     * @return Collection
88
     */
89 10
    public function setId(UuidInterface $id): self
90
    {
91 10
        $this->id = $id;
92
        
93 10
        return $this;
94
    }
95
    
96
    
97
    /**
98
     * @return UuidInterface
99
     */
100 9
    public function getId(): UuidInterface
101
    {
102 9
        return $this->id;
103
    }
104
105
    /**
106
     * @param User $user
107
     * @return Category
108
     */
109 10
    public function setUser(User $user): self
110
    {
111 10
        $this->user = $user;
112
        
113 10
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type App\Entity\Collection which is incompatible with the documented return type App\Entity\Category.
Loading history...
114
    }
115
116
    /**
117
     * @return User
118
     */
119 4
    public function getUser(): User
120
    {
121 4
        return $this->user;
122
    }
123
    
124
    /**
125
     * @return string
126
     */
127 6
    public function getName(): string
128
    {
129 6
        return $this->name;
130
    }
131
    
132
    /**
133
     * @param string $name
134
     * @return Collection
135
     */
136 10
    public function setName(string $name): self
137
    {
138 10
        Assertion::notEmpty($name, 'Collection name is required');
139 10
        Assertion::maxLength($name, 255, 'Collection name must less than 255 characters');
140 10
        $this->name = $name;
141
        
142 10
        return $this;
143
    }
144
    
145
    /**
146
     * @return string
147
     */
148 6
    public function getDescription(): ?string
149
    {
150 6
        return $this->description;
151
    }
152
    
153
    /**
154
     * @param ?string $description
155
     * @return Collection
156
     */
157 10
    public function setDescription(?string $description): self
158
    {
159 10
        $this->description = $description;
160
        
161 10
        return $this;
162
    }
163
    
164 1
    public function getItems(): ?PersistentCollection
165
    {
166 1
        return $this->items;
167
    }
168
    
169
    /**
170
     * @return bool
171
     */
172 1
    public function hasItems(): bool
173
    {
174 1
        return !($this->getItems() === null || $this->getItems()->isEmpty());
175
    }
176
177
    /**
178
     * @return string
179
     */
180
    public function getSlug(): string
181
    {
182
        return $this->slug;
183
    }
184
    
185
    /**
186
     * @return array
187
     */
188 4
    public function jsonSerialize()
189
    {
190
        return [
191 4
            'id' => $this->getId()->toString(),
192 4
            'name' => $this->getName(),
193 4
            'description' => $this->getDescription(),
194 4
            'userId' => $this->getUser()->getId()->toString(),
195
        ];
196
    }
197
}
198