1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Entity\Infrasctucture; |
4
|
|
|
|
5
|
|
|
use AppBundle\Entity\Band; |
6
|
|
|
use AppBundle\Entity\BandMember; |
7
|
|
|
use AppBundle\Entity\Organizer; |
8
|
|
|
use AppBundle\Entity\OrganizerMember; |
9
|
|
|
use AppBundle\Entity\User; |
10
|
|
|
use AppBundle\Exception\UnsupportedEntityException; |
11
|
|
|
use AppBundle\Service\HashGenerator; |
12
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
13
|
|
|
use Doctrine\Common\Collections\Collection; |
14
|
|
|
use JMS\Serializer\Annotation\Accessor; |
15
|
|
|
use Doctrine\ORM\Mapping as ORM; |
16
|
|
|
use JMS\Serializer\Annotation\Type as SerializerType; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Vehsamrak |
20
|
|
|
* @ORM\MappedSuperclass |
21
|
|
|
*/ |
22
|
|
|
abstract class Ambassador |
23
|
|
|
{ |
24
|
|
|
use CreatorLoginTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
* @ORM\Id |
29
|
|
|
* @ORM\Column(name="id", type="string", length=8, unique=true) |
30
|
|
|
*/ |
31
|
|
|
protected $id; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
* @ORM\Column(name="name", type="string", length=255, unique=true) |
36
|
|
|
*/ |
37
|
|
|
protected $name; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var \DateTime |
41
|
|
|
* @ORM\Column(name="registration_date", type="datetime") |
42
|
|
|
*/ |
43
|
|
|
protected $registrationDate; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
* @ORM\Column(name="description", type="text", nullable=true) |
48
|
|
|
*/ |
49
|
|
|
protected $description; |
50
|
|
|
|
51
|
|
|
/** @var ArrayCollection */ |
52
|
|
|
protected $members; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var User |
56
|
|
|
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="createdBands") |
57
|
|
|
* @ORM\JoinColumn(name="creator", referencedColumnName="login") |
58
|
|
|
* @Accessor(getter="getCreatorLogin") |
59
|
|
|
* @SerializerType("string") |
60
|
|
|
*/ |
61
|
|
|
protected $creator; |
62
|
|
|
|
63
|
4 |
View Code Duplication |
public function __construct( |
|
|
|
|
64
|
|
|
string $name, |
65
|
|
|
User $creator, |
66
|
|
|
string $description = null, |
67
|
|
|
HashGenerator $hashGenerator = null |
68
|
|
|
) |
69
|
|
|
{ |
70
|
|
|
/** @var HashGenerator $hashGenerator */ |
71
|
4 |
|
$hashGenerator = $hashGenerator ?: new HashGenerator(); |
72
|
4 |
|
$this->id = $hashGenerator->generate(); |
73
|
4 |
|
$this->registrationDate = new \DateTime(); |
74
|
4 |
|
$this->name = $name; |
75
|
4 |
|
$this->description = $description; |
76
|
4 |
|
$this->members = new ArrayCollection(); |
77
|
4 |
|
$this->creator = $creator; |
78
|
4 |
|
} |
79
|
|
|
|
80
|
2 |
|
public function getId(): string |
81
|
|
|
{ |
82
|
2 |
|
return $this->id; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return Collection|BandMember[] |
87
|
|
|
*/ |
88
|
11 |
|
public function getMembers(): Collection |
89
|
|
|
{ |
90
|
11 |
|
return $this->members; |
91
|
|
|
} |
92
|
|
|
|
93
|
2 |
|
public function addMember(BandMember $bandMember) |
94
|
|
|
{ |
95
|
2 |
|
$this->members->add($bandMember); |
96
|
2 |
|
} |
97
|
|
|
|
98
|
1 |
|
public function removeMember(BandMember $bandMember) |
99
|
|
|
{ |
100
|
1 |
|
$this->members->removeElement($bandMember); |
101
|
1 |
|
} |
102
|
|
|
|
103
|
1 |
|
public function setName(string $name) |
104
|
|
|
{ |
105
|
1 |
|
$this->name = $name; |
106
|
1 |
|
} |
107
|
|
|
|
108
|
1 |
|
public function setDescription(string $description) |
109
|
|
|
{ |
110
|
1 |
|
$this->description = $description; |
111
|
1 |
|
} |
112
|
|
|
|
113
|
10 |
|
public function getCreator(): User |
114
|
|
|
{ |
115
|
10 |
|
return $this->creator; |
116
|
|
|
} |
117
|
|
|
|
118
|
4 |
|
public function getMemberClass(): string |
119
|
|
|
{ |
120
|
4 |
|
if ($this instanceof Band) { |
121
|
3 |
|
return BandMember::class; |
122
|
|
|
} elseif ($this instanceof Organizer) { |
123
|
1 |
|
return OrganizerMember::class; |
124
|
|
|
} else { |
125
|
|
|
throw new UnsupportedEntityException(); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.