SortOptionsFactory::configureResolver()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 2.1481

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 36
ccs 16
cts 24
cp 0.6667
rs 8.8571
cc 2
eloc 25
nc 2
nop 4
crap 2.1481
1
<?php
2
3
namespace Alchemy\RestBundle\Rest\Request;
4
5
use Alchemy\Rest\Request\SortOptions;
6
use Symfony\Component\OptionsResolver\OptionsResolver;
7
8
class SortOptionsFactory
9
{
10
    /**
11
     * @var string
12
     */
13
    private $sortName;
14
15
    /**
16
     * @var string
17
     */
18
    private $directionName;
19
20
    /**
21
     * @var string
22
     */
23
    private $multisortName;
24
25
    /**
26
     * @param string $sortName
27
     * @param string $directionName
28
     * @param string $multisortName
29
     */
30 12
    public function __construct($sortName, $directionName, $multisortName)
31
    {
32 12
        $this->sortName = $sortName;
33 12
        $this->directionName = $directionName;
34 12
        $this->multisortName = $multisortName;
35 12
    }
36
37
    /**
38
     * @param array $request
39
     * @param array $config
40
     * @return SortRequest
41
     */
42 10
    public function create(array $request, array $config)
43
    {
44 10
        $sortName = isset($config['sort_parameter']) ? $config['sort_parameter'] : $this->sortName;
45 10
        $directionName = isset($config['direction_parameter']) ? $config['direction_parameter'] : $this->directionName;
46 10
        $multisortName = isset($config['multi_sort_parameter']) ?
47 10
            $config['multi_sort_parameter'] : $this->multisortName;
48
49 10
        $resolver = $this->configureResolver($request, $sortName, $directionName, $multisortName);
50 10
        $values = $resolver->resolve($request);
51
52 10
        return new SortRequest($values[$multisortName], $values[$sortName], $values[$directionName]);
53
    }
54
55
    /**
56
     * @param array $options
57
     * @param string $sortName
58
     * @param string $directionName
59
     * @param string $multisortName
60
     * @return OptionsResolver
61
     */
62 10
    private function configureResolver(array $options, $sortName, $directionName, $multisortName)
63
    {
64 10
        $optionsResolver = new OptionsResolver();
65
66
        $keys = array(
67 10
            $sortName => null,
68 10
            $directionName => null,
69
            $multisortName => null
70 10
        );
71
72 10
        if (method_exists($optionsResolver, 'setDefined')) {
73 10
            $optionsResolver->setDefined(array_merge(array_keys($keys), array_keys($options)));
74 10
            $optionsResolver->setAllowedValues($directionName, array(
75 10
                strtoupper(SortOptions::SORT_ASC),
76 10
                strtolower(SortOptions::SORT_ASC),
77 10
                strtoupper(SortOptions::SORT_DESC),
78 10
                strtolower(SortOptions::SORT_DESC),
79
                null
80 10
            ));
81 10
        } else {
82
            // BC with symfony < 2.6
83
            $optionsResolver->setOptional(array_merge(array_keys($keys), array_keys($options)));
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Option...Resolver::setOptional() has been deprecated with message: since version 2.6, to be removed in 3.0.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
84
            $optionsResolver->setAllowedValues(array(
0 ignored issues
show
Documentation introduced by
array($directionName => ...ons::SORT_DESC), null)) is of type array<string,array<integ...:"string","4":"null"}>>, 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...
85
                $directionName => array(
86
                    strtoupper(SortOptions::SORT_ASC),
87
                    strtolower(SortOptions::SORT_ASC),
88
                    strtoupper(SortOptions::SORT_DESC),
89
                    strtolower(SortOptions::SORT_DESC),
90
                    null
91
                )
92
            ));
93
        }
94 10
        $optionsResolver->setDefaults($keys);
95
96 10
        return $optionsResolver;
97
    }
98
}
99