Passed
Push — master ( f3426b...21e202 )
by Petr
03:26
created

BandService::processFormAndCreateBand()   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
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace AppBundle\Service\Entity;
4
5
use AppBundle\Entity\Band;
6
use AppBundle\Entity\BandMember;
7
use AppBundle\Entity\Repository\BandMemberRepository;
8
use AppBundle\Entity\Repository\BandRepository;
9
use AppBundle\Entity\Repository\UserRepository;
10
use AppBundle\Entity\User;
11
use Symfony\Component\Form\FormError;
12
use Symfony\Component\Form\FormInterface;
13
14
/**
15
 * @author Vehsamrak
16
 */
17
class BandService extends EntityService
18
{
19
    const ATTRIBUTE_MEMBERS = 'members';
20
    const CREATOR_DEFAULT_MEMBER_SHORT_DESCRIPTION = 'Founder';
21
22
    /** @var BandRepository */
23
    private $bandRepository;
24
25
    /** @var BandMemberRepository */
26
    private $bandMemberRepository;
27
28
    /** @var UserRepository */
29
    private $userRepository;
30
31 5
    public function __construct(
32
        BandRepository $bandRepository,
33
        BandMemberRepository $bandMemberRepository,
34
        UserRepository $userRepository
35
    ) {
36 5
        $this->bandRepository = $bandRepository;
37 5
        $this->bandMemberRepository = $bandMemberRepository;
38 5
        $this->userRepository = $userRepository;
39 5
    }
40
41 2
    public function processFormAndCreateBand(FormInterface $form, User $creator): FormInterface
42
    {
43 2
        return $this->createOrUpdateBand($form, $creator);
44
    }
45
46 2
    public function processFormAndUpdateBand(FormInterface $form, Band $band, User $creator): FormInterface
47
    {
48 2
        return $this->createOrUpdateBand($form, $creator, $band);
49
    }
50
51
    /**
52
     * Create or update band. If no Band object passed, new one will be created
53
     */
54 4
    private function createOrUpdateBand(FormInterface $form, User $creator, Band $band = null): FormInterface
55
    {
56 4
        $this->createOrUpdateBandUsingForm($form, $creator, $band);
57 4
        $this->bandRepository->flush();
58
59 4
        return $form;
60
    }
61
62 4
    private function createOrUpdateBandUsingForm(
63
        FormInterface $form,
64
        User $creator,
65
        Band $band = null
66
    ) {
67 4
        if (!$form->isValid()) {
68 1
            return null;
69
        }
70
71 3
        $membersData = $form->get(self::ATTRIBUTE_MEMBERS)->getData();
72
73 3
        if (!$membersData) {
74
            $form->addError(new FormError(sprintf('Parameter "%s" is mandatory.', self::ATTRIBUTE_MEMBERS)));
75
76
            return null;
77
        }
78
79 3
        $bandNewName = $form->get('name')->getData();
80 3
        $description = $form->get('description')->getData();
81
82 3
        if ($this->bandRepository->findOneByName($bandNewName)) {
83 1
            $form->addError(new FormError(sprintf('Band with name "%s" already exists.', $bandNewName)));
84
85 1
            return null;
86
        }
87
88 2
        if ($band) {
89 1
            $band->setName($bandNewName);
90 1
            $band->setDescription($description);
91
        } else {
92 1
            $band = new Band($bandNewName, $creator, $description);
93 1
            $this->bandRepository->persist($band);
94
        }
95
96 2
        $bandMembers = $this->getBandMembersFromForm($band, $form);
97 2
        $bandMembers[] = $this->createBandMemberFromCreator($band, $creator);
98
99 2
        foreach ($bandMembers as $bandMember) {
100 2
            $band->addMember($bandMember);
101
        }
102 2
    }
103
104
    /**
105
     * @return BandMember[]
106
     */
107 2
    private function getBandMembersFromForm(Band $band, FormInterface $form): array
108
    {
109 2
        return array_map(
110 2
            function (array $userData) use ($band, $form) {
111 2
                $user = null;
112 2
                $shortDescription = '';
113
114 2
                if (isset($userData['login'], $userData['short_description'])) {
115 2
                    $userLogin = $userData['login'];
116 2
                    $shortDescription = $userData['short_description'];
117 2
                    $user = $this->userRepository->findOneByLogin($userLogin);
118
119 2
                    if (!$user) {
120 2
                        $form->addError(new FormError(sprintf('User "%s" was not found.', $userLogin)));
121
                    }
122
                } else {
123
                    $form->addError(
124
                        new FormError('Group member parameters login and short_description are mandatory.')
125
                    );
126
                }
127
128 2
                return $this->bandMemberRepository->getOrCreateByBandAndUser($band, $user, $shortDescription);
0 ignored issues
show
Documentation introduced by
$user is of type object|null, but the function expects a object<AppBundle\Entity\User>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
129 2
            },
130 2
            $form->get(self::ATTRIBUTE_MEMBERS)->getData()
131
        );
132
    }
133
134 1
    public function processFormAndUpdateBandMember(FormInterface $form, BandMember $bandMember): FormInterface
135
    {
136 1
        $shortDescription = $form->get('short_description')->getData();
137 1
        $description = $form->get('description')->getData();
138
139 1
        if ($shortDescription) {
140 1
            $bandMember->setShortDescription($shortDescription);
141
        }
142
143 1
        if ($description) {
144 1
            $bandMember->setDescription($description);
145
        }
146
147 1
        return $form;
148
    }
149
150 2
    private function createBandMemberFromCreator(Band $band, User $creator): BandMember
151
    {
152 2
        return $this->bandMemberRepository->getOrCreateByBandAndUser(
153
            $band,
154
            $creator,
155 2
            self::CREATOR_DEFAULT_MEMBER_SHORT_DESCRIPTION
156
        );
157
    }
158
}
159