Completed
Push — admin-api-bundle ( ac2d6a )
by Kamil
22:14
created

AddUserFormSubscriber   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 78
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSubscribedEvents() 0 7 1
A preSetData() 0 5 1
A preSubmit() 0 15 4
A isUserDataEmpty() 0 9 3
A removeUserField() 0 5 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\AdminApiBundle\Form\EventSubscriber;
13
14
use Sylius\Component\User\Model\UserAwareInterface;
15
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16
use Symfony\Component\Form\FormEvent;
17
use Symfony\Component\Form\FormEvents;
18
use Symfony\Component\Validator\Constraints\Valid;
19
use Webmozart\Assert\Assert;
20
21
/**
22
 * @author Łukasz Chruściel <[email protected]>
23
 */
24
final class AddUserFormSubscriber implements EventSubscriberInterface
25
{
26
    /**
27
     * @var string
28
     */
29
    private $entryType;
30
31
    /**
32
     * @param string $entryType
33
     */
34
    public function __construct($entryType)
35
    {
36
        $this->entryType = $entryType;
37
    }
38
39
    /**
40
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<*,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
41
     */
42
    public static function getSubscribedEvents()
43
    {
44
        return [
45
            FormEvents::PRE_SET_DATA => 'preSetData',
46
            FormEvents::PRE_SUBMIT => 'preSubmit',
47
        ];
48
    }
49
50
    /**
51
     * @param FormEvent $event
52
     */
53
    public function preSetData(FormEvent $event)
54
    {
55
        $form = $event->getForm();
56
        $form->add('user', $this->entryType, ['constraints' => [new Valid()]]);
57
    }
58
59
    /**
60
     * @param FormEvent $event
61
     */
62
    public function preSubmit(FormEvent $event)
63
    {
64
        $data = $event->getData();
65
        $normData = $event->getForm()->getNormData();
66
        if (!isset($data['user'])) {
67
            $this->removeUserField($event);
68
            return;
69
        }
70
        Assert::isInstanceOf($normData, UserAwareInterface::class);
71
        if ($this->isUserDataEmpty($data) && null === $normData->getUser()) {
72
            unset($data['user']);
73
            $event->setData($data);
74
            $this->removeUserField($event);
75
        }
76
    }
77
78
    /**
79
     * @param array $data
80
     *
81
     * @return bool
82
     */
83
    private function isUserDataEmpty(array $data)
84
    {
85
        foreach ($data['user'] as $field) {
86
            if (!empty($field)) {
87
                return false;
88
            }
89
        }
90
        return true;
91
    }
92
93
    /**
94
     * @param FormEvent $event
95
     */
96
    private function removeUserField(FormEvent $event)
97
    {
98
        $form = $event->getForm();
99
        $form->remove('user');
100
    }
101
}
102