|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\Service\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use AppBundle\Entity\Band; |
|
6
|
|
|
use AppBundle\Entity\BandMember; |
|
7
|
|
|
use AppBundle\Entity\Infrasctucture\Ambassador; |
|
8
|
|
|
use AppBundle\Entity\Infrasctucture\AmbassadorMember; |
|
9
|
|
|
use AppBundle\Entity\Repository\BandMemberRepository; |
|
10
|
|
|
use AppBundle\Entity\Repository\BandRepository; |
|
11
|
|
|
use AppBundle\Entity\Repository\UserRepository; |
|
12
|
|
|
use AppBundle\Entity\User; |
|
13
|
|
|
use Symfony\Component\Form\FormError; |
|
14
|
|
|
use Symfony\Component\Form\FormInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @author Vehsamrak |
|
18
|
|
|
*/ |
|
19
|
|
|
class BandService extends EntityService |
|
20
|
|
|
{ |
|
21
|
|
|
const ATTRIBUTE_MEMBERS = 'members'; |
|
22
|
|
|
const CREATOR_DEFAULT_MEMBER_SHORT_DESCRIPTION = 'Founder'; |
|
23
|
|
|
|
|
24
|
|
|
/** @var BandRepository */ |
|
25
|
|
|
private $bandRepository; |
|
26
|
|
|
|
|
27
|
|
|
/** @var BandMemberRepository */ |
|
28
|
|
|
private $bandMemberRepository; |
|
29
|
|
|
|
|
30
|
|
|
/** @var UserRepository */ |
|
31
|
|
|
private $userRepository; |
|
32
|
|
|
|
|
33
|
15 |
|
public function __construct( |
|
34
|
|
|
BandRepository $bandRepository, |
|
35
|
|
|
BandMemberRepository $bandMemberRepository, |
|
36
|
|
|
UserRepository $userRepository |
|
37
|
|
|
) { |
|
38
|
15 |
|
$this->bandRepository = $bandRepository; |
|
39
|
15 |
|
$this->bandMemberRepository = $bandMemberRepository; |
|
40
|
15 |
|
$this->userRepository = $userRepository; |
|
41
|
15 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function processFormAndCreateBand(FormInterface $form, User $creator): FormInterface |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->createOrUpdateBand($form, $creator); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
2 |
|
public function processFormAndUpdateBand(FormInterface $form, Band $band, User $creator): FormInterface |
|
49
|
|
|
{ |
|
50
|
2 |
|
return $this->createOrUpdateBand($form, $creator, $band); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Create or update band. If no Band object passed, new one will be created |
|
55
|
|
|
*/ |
|
56
|
2 |
|
private function createOrUpdateBand(FormInterface $form, User $creator, Band $band = null): FormInterface |
|
57
|
|
|
{ |
|
58
|
2 |
|
$this->createOrUpdateBandUsingForm($form, $creator, $band); |
|
59
|
2 |
|
$this->bandRepository->flush(); |
|
60
|
|
|
|
|
61
|
2 |
|
return $form; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
private function createOrUpdateBandUsingForm( |
|
65
|
|
|
FormInterface $form, |
|
66
|
|
|
User $creator, |
|
67
|
|
|
Band $band = null |
|
68
|
|
|
) { |
|
69
|
2 |
|
if (!$form->isValid()) { |
|
70
|
|
|
return null; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
2 |
|
$membersData = $form->get(self::ATTRIBUTE_MEMBERS)->getData(); |
|
74
|
|
|
|
|
75
|
2 |
|
if (!$membersData) { |
|
76
|
|
|
$form->addError(new FormError(sprintf('Parameter "%s" is mandatory.', self::ATTRIBUTE_MEMBERS))); |
|
77
|
|
|
|
|
78
|
|
|
return null; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
$bandNewName = $form->get('name')->getData(); |
|
82
|
2 |
|
$description = $form->get('description')->getData(); |
|
83
|
|
|
|
|
84
|
2 |
|
if ($this->bandRepository->findOneByName($bandNewName)) { |
|
85
|
1 |
|
$form->addError(new FormError(sprintf('Band with name "%s" already exists.', $bandNewName))); |
|
86
|
|
|
|
|
87
|
1 |
|
return null; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
if ($band) { |
|
91
|
1 |
|
$band->setName($bandNewName); |
|
92
|
1 |
|
$band->setDescription($description); |
|
93
|
|
|
} else { |
|
94
|
|
|
$band = new Band($bandNewName, $creator, $description); |
|
95
|
|
|
$this->bandRepository->persist($band); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
$bandMembers = $this->getBandMembersFromForm($band, $form); |
|
99
|
1 |
|
$bandMembers[] = $this->createAmbassadorMemberFromCreator($band, $creator); |
|
100
|
|
|
|
|
101
|
1 |
|
foreach ($bandMembers as $bandMember) { |
|
102
|
1 |
|
$band->addMember($bandMember); |
|
103
|
|
|
} |
|
104
|
1 |
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return BandMember[] |
|
108
|
|
|
*/ |
|
109
|
1 |
|
private function getBandMembersFromForm(Band $band, FormInterface $form): array |
|
110
|
|
|
{ |
|
111
|
1 |
|
return array_map( |
|
112
|
1 |
|
function (array $userData) use ($band, $form) { |
|
113
|
1 |
|
$user = null; |
|
114
|
1 |
|
$shortDescription = ''; |
|
115
|
|
|
|
|
116
|
1 |
|
if (isset($userData['login'], $userData['short_description'])) { |
|
117
|
1 |
|
$userLogin = $userData['login']; |
|
118
|
1 |
|
$shortDescription = $userData['short_description']; |
|
119
|
1 |
|
$user = $this->userRepository->findOneByLogin($userLogin); |
|
120
|
|
|
|
|
121
|
1 |
|
if (!$user) { |
|
122
|
1 |
|
$form->addError(new FormError(sprintf('User "%s" was not found.', $userLogin))); |
|
123
|
|
|
} |
|
124
|
|
|
} else { |
|
125
|
|
|
$form->addError( |
|
126
|
|
|
new FormError('Group member parameters login and short_description are mandatory.') |
|
127
|
|
|
); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
1 |
|
return $this->bandMemberRepository->getOrCreateByAmbassadorAndUser($band, $user, $shortDescription); |
|
|
|
|
|
|
131
|
1 |
|
}, |
|
132
|
1 |
|
$form->get(self::ATTRIBUTE_MEMBERS)->getData() |
|
133
|
|
|
); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
1 |
|
public function processFormAndUpdateBandMember(FormInterface $form, BandMember $bandMember): FormInterface |
|
137
|
|
|
{ |
|
138
|
1 |
|
$shortDescription = $form->get('short_description')->getData(); |
|
139
|
1 |
|
$description = $form->get('description')->getData(); |
|
140
|
|
|
|
|
141
|
1 |
|
if ($shortDescription) { |
|
142
|
1 |
|
$bandMember->setShortDescription($shortDescription); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
1 |
|
if ($description) { |
|
146
|
1 |
|
$bandMember->setDescription($description); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
1 |
|
return $form; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
3 |
|
public function createAmbassadorMemberFromCreator(Ambassador $ambassador, User $creator): AmbassadorMember |
|
153
|
|
|
{ |
|
154
|
3 |
|
return $this->bandMemberRepository->getOrCreateByAmbassadorAndUser( |
|
155
|
|
|
$ambassador, |
|
156
|
|
|
$creator, |
|
157
|
3 |
|
self::CREATOR_DEFAULT_MEMBER_SHORT_DESCRIPTION |
|
158
|
|
|
); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: