Passed
Push — master ( ce04e2...4d96b0 )
by Petr
03:59
created

BandService   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 8
dl 0
loc 137
rs 10
c 0
b 0
f 0
ccs 53
cts 53
cp 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A processFormAndUpdateBand() 0 4 1
A createOrUpdateBand() 0 7 1
B createOrUpdateBandUsingForm() 0 41 6
B getBandMembersFromForm() 0 26 3
A processFormAndUpdateBandMember() 0 15 3
A createAmbassadorMemberFromCreator() 0 8 1
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 2
    public function processFormAndUpdateBand(FormInterface $form, Band $band, User $creator): FormInterface
44
    {
45 2
        return $this->createOrUpdateBand($form, $creator, $band);
46
    }
47
48
    /**
49
     * Create or update band. If no Band object passed, new one will be created
50
     */
51 2
    private function createOrUpdateBand(FormInterface $form, User $creator, Band $band = null): FormInterface
52
    {
53 2
        $this->createOrUpdateBandUsingForm($form, $creator, $band);
54 2
        $this->bandRepository->flush();
55
56 2
        return $form;
57
    }
58
59 2
    private function createOrUpdateBandUsingForm(
60
        FormInterface $form,
61
        User $creator,
62
        Band $band = null
63
    ) {
64 2
        if (!$form->isValid()) {
65
            return null;
66
        }
67
68 2
        $membersData = $form->get(self::ATTRIBUTE_MEMBERS)->getData();
69
70 2
        if (!$membersData) {
71
            $form->addError(new FormError(sprintf('Parameter "%s" is mandatory.', self::ATTRIBUTE_MEMBERS)));
72
73
            return null;
74
        }
75
76 2
        $bandNewName = $form->get('name')->getData();
77 2
        $description = $form->get('description')->getData();
78
79 2
        if ($this->bandRepository->findOneByName($bandNewName)) {
80 1
            $form->addError(new FormError(sprintf('Band with name "%s" already exists.', $bandNewName)));
81
82 1
            return null;
83
        }
84
85 1
        if ($band) {
86 1
            $band->setName($bandNewName);
87 1
            $band->setDescription($description);
88
        } else {
89
            $band = new Band($bandNewName, $creator, $description);
90
            $this->bandRepository->persist($band);
91
        }
92
93 1
        $bandMembers = $this->getBandMembersFromForm($band, $form);
94 1
        $bandMembers[] = $this->createAmbassadorMemberFromCreator($band, $creator);
95
96 1
        foreach ($bandMembers as $bandMember) {
97 1
            $band->addMember($bandMember);
98
        }
99 1
    }
100
101
    /**
102
     * @return BandMember[]
103
     */
104 1
    private function getBandMembersFromForm(Band $band, FormInterface $form): array
105
    {
106 1
        return array_map(
107 1
            function (array $userData) use ($band, $form) {
108 1
                $user = null;
109 1
                $shortDescription = '';
110
111 1
                if (isset($userData['login'], $userData['short_description'])) {
112 1
                    $userLogin = $userData['login'];
113 1
                    $shortDescription = $userData['short_description'];
114 1
                    $user = $this->userRepository->findOneByLogin($userLogin);
115
116 1
                    if (!$user) {
117 1
                        $form->addError(new FormError(sprintf('User "%s" was not found.', $userLogin)));
118
                    }
119
                } else {
120
                    $form->addError(
121
                        new FormError('Group member parameters login and short_description are mandatory.')
122
                    );
123
                }
124
125 1
                return $this->bandMemberRepository->getOrCreateByAmbassadorAndUser($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...
126 1
            },
127 1
            $form->get(self::ATTRIBUTE_MEMBERS)->getData()
128
        );
129
    }
130
131 1
    public function processFormAndUpdateBandMember(FormInterface $form, BandMember $bandMember): FormInterface
132
    {
133 1
        $shortDescription = $form->get('short_description')->getData();
134 1
        $description = $form->get('description')->getData();
135
136 1
        if ($shortDescription) {
137 1
            $bandMember->setShortDescription($shortDescription);
138
        }
139
140 1
        if ($description) {
141 1
            $bandMember->setDescription($description);
142
        }
143
144 1
        return $form;
145
    }
146
147 3
    public function createAmbassadorMemberFromCreator(Ambassador $ambassador, User $creator): AmbassadorMember
148
    {
149 3
        return $this->bandMemberRepository->getOrCreateByAmbassadorAndUser(
150
            $ambassador,
151
            $creator,
152 3
            self::CREATOR_DEFAULT_MEMBER_SHORT_DESCRIPTION
153
        );
154
    }
155
}
156