Completed
Push — master ( 3b7290...73d268 )
by
unknown
11:56
created

SingleChannelModeExtension   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 9
c 2
b 0
f 1
lcom 1
cbo 7
dl 0
loc 69
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A buildForm() 0 18 3
A buildView() 0 12 3
A setDefaultOptions() 0 4 1
A getExtendedType() 0 4 1
1
<?php
2
3
namespace OroCRM\Bundle\ChannelBundle\Form\Extension;
4
5
use Symfony\Component\Form\AbstractTypeExtension;
6
use Symfony\Component\Form\FormBuilderInterface;
7
use Symfony\Component\Form\FormEvent;
8
use Symfony\Component\Form\FormEvents;
9
use Symfony\Component\Form\FormInterface;
10
use Symfony\Component\Form\FormView;
11
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
12
13
use Oro\Bundle\FormBundle\Utils\FormUtils;
14
15
use OroCRM\Bundle\ChannelBundle\Form\Type\ChannelSelectType;
16
use OroCRM\Bundle\ChannelBundle\Provider\ChannelsByEntitiesProvider;
17
18
class SingleChannelModeExtension extends AbstractTypeExtension
19
{
20
    /**
21
     * @var ChannelsByEntitiesProvider
22
     */
23
    protected $channelsProvider;
24
25
    /**
26
     * @param ChannelsByEntitiesProvider $channelsProvider
27
     */
28
    public function __construct(ChannelsByEntitiesProvider $channelsProvider)
29
    {
30
        $this->channelsProvider = $channelsProvider;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function buildForm(FormBuilderInterface $builder, array $options)
37
    {
38
        if (!$options['single_channel_mode']) {
39
            return;
40
        }
41
        $entities = $options['entities'];
42
        $channels = $this->channelsProvider->getChannelsByEntities($entities);
43
44
        if (count($channels) === 1) {
45
            $channel = reset($channels);
46
            $builder->addEventListener(
47
                FormEvents::PRE_SET_DATA,
48
                function (FormEvent $event) use ($channel) {
49
                    $event->setData($channel);
50
                }
51
            );
52
        }
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function buildView(FormView $view, FormInterface $form, array $options)
59
    {
60
        if (!$options['single_channel_mode']) {
61
            return;
62
        }
63
        $entities = $options['entities'];
64
        $channels = $this->channelsProvider->getChannelsByEntities($entities);
65
        if (count($channels) === 1) {
66
            $view->vars['read_only'] = true;
67
            FormUtils::appendClass($view, 'hide');
68
        }
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function setDefaultOptions(OptionsResolverInterface $resolver)
75
    {
76
        $resolver->setDefaults(['single_channel_mode' => true]);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function getExtendedType()
83
    {
84
        return ChannelSelectType::NAME;
85
    }
86
}
87