Completed
Pull Request — master (#186)
by Vladimir
06:00 queued 03:00
created

InvitationFormCreator::checkPlayerEligibility()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.5125
cc 5
eloc 12
nc 6
nop 1
1
<?php
2
3
namespace BZIon\Form\Creator;
4
5
use BZIon\Event\Events;
6
use BZIon\Event\TeamInviteEvent;
7
use BZIon\Form\Constraint\NotBlankModel;
8
use BZIon\Form\Type\AdvancedModelType;
9
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
10
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
11
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
12
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
13
use Symfony\Component\Form\FormError;
14
use Symfony\Component\Form\FormEvent;
15
use Symfony\Component\Form\FormEvents;
16
17
/**
18
 * @property \Team $editing
19
 */
20
class InvitationFormCreator extends ModelFormCreator
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    protected function build($builder)
26
    {
27
        $invitedPlayer = new AdvancedModelType(\Player::class, [
28
            'constraints' => [
29
                new NotBlankModel(),
30
            ],
31
            'label' => 'Invitation Recipient',
32
            'required' => true,
33
        ]);
34
35
        $targetTeam = $builder
36
            ->create('target_team', HiddenType::class, [
37
                'data' => $this->editing->getId(),
38
            ])
39
            ->setDataLocked(true)
40
        ;
41
42
        $builder
43
            ->add($targetTeam)
0 ignored issues
show
Documentation introduced by
$targetTeam is of type object<Symfony\Component\Form\FormConfigBuilder>, but the function expects a string|integer|object<Sy...m\FormBuilderInterface>.

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...
44
            ->add('invited_player', $invitedPlayer)
45
            ->add('message', TextareaType::class, [
46
                'required' => false,
47
            ])
48
            ->add('cancel', ButtonType::class, [
49
                'label' => 'Cancel',
50
            ])
51
            ->add('submit', SubmitType::class, [
52
                'label' => 'Invite',
53
                'attr' => [
54
                    'class' => 'c-button--blue pattern pattern--upward-stripes'
55
                ]
56
            ])
57
            ->addEventListener(FormEvents::POST_SUBMIT, [$this, 'checkPlayerEligibility'])
58
        ;
59
60
        return $builder;
61
    }
62
63
    public function checkPlayerEligibility(FormEvent $event)
64
    {
65
        $form = $event->getForm();
66
67
        if ($form->has('invited_player')) {
68
            $formElement = $form->get('invited_player');
69
70
            /** @var \Player|null $proposedInvitee */
71
            $proposedInvitee = $formElement->getData();
72
73
            if ($proposedInvitee === null) {
74
                $formElement->addError(new FormError('Invited player not found.'));
75
                return;
76
            }
77
78
            if ($this->editing->isMember($proposedInvitee->getId())) {
79
                $formElement->addError(new FormError('This player is already a member of that team.'));
80
            }
81
82
            if (\Invitation::playerHasInvitationToTeam($proposedInvitee->getId(), $this->editing->getId())) {
83
                $formElement->addError(new FormError('This player already has an invitation to this team.'));
84
            }
85
        }
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function enter($form)
92
    {
93
        $invite = \Invitation::sendInvite(
94
            $form->get('invited_player')->getData(),
95
            $form->get('target_team')->getData(),
96
            $this->me->getId(),
97
            $form->get('message')->getData()
98
        );
99
        \Service::getDispatcher()->dispatch(Events::TEAM_INVITE, new TeamInviteEvent($invite));
100
101
        return $invite;
102
    }
103
}
104