LdapObjectType::configureOptions()   B
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 43
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
c 0
b 0
f 0
rs 8.5806
cc 4
eloc 30
nc 1
nop 1
1
<?php
2
/**
3
 * This file is part of the LdapToolsBundle package.
4
 *
5
 * (c) Chad Sikorra <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LdapTools\Bundle\LdapToolsBundle\Form\Type;
12
13
use LdapTools\Bundle\LdapToolsBundle\Form\ChoiceList\LdapObjectChoiceList;
14
use LdapTools\Bundle\LdapToolsBundle\Form\ChoiceLoader\LdapObjectChoiceLoader;
15
use LdapTools\Bundle\LdapToolsBundle\Form\ChoiceLoader\LegacyLdapChoiceLoader;
16
use LdapTools\LdapManager;
17
use Symfony\Component\Form\AbstractType;
18
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
19
use Symfony\Component\OptionsResolver\OptionsResolver;
20
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
21
use Symfony\Component\OptionsResolver\Options;
22
23
/**
24
 * Provides a form type that represents LDAP object.
25
 *
26
 * @author Chad Sikorra <[email protected]>
27
 */
28
class LdapObjectType extends AbstractType
29
{
30
    /**
31
     * @var LdapManager
32
     */
33
    protected $ldap;
34
35
    /**
36
     * @param LdapManager $ldap
37
     */
38
    public function __construct(LdapManager $ldap)
39
    {
40
        $this->ldap = $ldap;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function configureOptions(OptionsResolver $resolver)
47
    {
48
        $ldap = $this->ldap;
49
50
        $resolver->setDefaults([
51
            'ldap_domain' => $this->ldap->getDomainContext(), // The LDAP domain context
52
            'ldap_attributes' => null, // The attributes to select
53
            'ldap_query_builder' => null, // A closure or a LdapQueryBuilder instance
54
            'choice_name' => 'name',
55
            'choice_value' => 'guid',
56
            'choices' => [], // A user supplied array of LDAP objects.
57
            'choice_loader' => function (Options $options) use ($ldap) {
58
                if (!interface_exists('\Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')) {
59
                    return null;
60
                }
61
62
                return new LdapObjectChoiceLoader($ldap,
63
                    $options['ldap_type'],
64
                    $options['choice_name'],
65
                    $options['choice_value'],
66
                    $options['ldap_query_builder']
67
                );
68
            },
69
            'choice_list' => function (Options $options) use ($ldap) {
70
                // Always prefer the ChoiceLoader if it exists. Fall back to the ObjectChoiceList...
71
                if (interface_exists('\Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')) {
72
                    return null;
73
                }
74
                $legacyChoiceLoader = new LegacyLdapChoiceLoader($ldap, $options['ldap_type'], $options['choice_name'], $options['choice_value'], $options['ldap_query_builder']);
75
                $preferred = isset($options['preferred_choices']) ? $options['preferred_choices'] : [];
76
77
                return new LdapObjectChoiceList(
78
                    $legacyChoiceLoader->load(),
79
                    $options['choice_name'],
80
                    $preferred,
81
                    null,
82
                    $options['choice_value']
83
                );
84
            },
85
        ]);
86
        $resolver->setRequired(['ldap_type']);
87
        $this->setAllowedTypes($resolver);
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function setDefaultOptions(OptionsResolverInterface $resolver)
94
    {
95
        $this->configureOptions($resolver);
96
    }
97
98
    /**
99
     * @return string
100
     */
101
    public function getParent()
102
    {
103
        // FQCN is needed instead of a simple name in Symfony 3+ ...
104
        $interface = new \ReflectionClass('\Symfony\Component\Form\FormTypeInterface');
105
106
        if ($interface->hasMethod('getName')) {
107
            $parent = 'choice';
108
        } else {
109
            $parent = ChoiceType::class;
110
        }
111
112
        return $parent;
113
    }
114
115
    /**
116
     * @return string
117
     */
118
    public function getName()
119
    {
120
        return 'ldap_object';
121
    }
122
123
    /**
124
     * A rather ugly way of allowing compatibility with the resolver component for pre 2.6 versions.
125
     *
126
     * @param OptionsResolver $resolver
127
     */
128
    protected function setAllowedTypes(OptionsResolver $resolver)
129
    {
130
        $allowed = ['ldap_query_builder', ['\Closure', 'LdapTools\Query\LdapQueryBuilder', 'null']];
131
132
        $reflection = new \ReflectionClass(get_class($resolver));
133
        $parameters = $reflection->getMethod('addAllowedTypes')->getParameters();
134
135
        if ($parameters[0]->isArray()) {
136
            $resolver->setAllowedTypes([$allowed[0] => $allowed[1]]);
0 ignored issues
show
Bug introduced by
The call to setAllowedTypes() misses a required argument $allowedTypes.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
array($allowed[0] => $allowed[1]) is of type array<string,array<integ...string","2":"string"}>>, but the function expects a string.

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...
137
        } else {
138
            $resolver->setAllowedTypes(...$allowed);
0 ignored issues
show
Bug introduced by
The call to setAllowedTypes() misses a required argument $allowedTypes.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
$allowed is of type array<integer,string|arr...",\"2\":\"string\"}>"}>, but the function expects a string.

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...
139
        }
140
    }
141
}
142