PaginationOptionsFactory::configureResolver()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2.0393

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
ccs 11
cts 14
cp 0.7856
rs 8.9713
cc 2
eloc 16
nc 2
nop 3
crap 2.0393
1
<?php
2
3
namespace Alchemy\RestBundle\Rest\Request;
4
5
use Symfony\Component\OptionsResolver\OptionsResolver;
6
7
class PaginationOptionsFactory
8
{
9
    /**
10
     * @var string
11
     */
12
    private $offsetName;
13
14
    /**
15
     * @var string
16
     */
17
    private $limitName;
18
19
    /**
20
     * @param string $offsetName
21
     * @param string $limitName
22
     */
23 8
    public function __construct($offsetName, $limitName)
24
    {
25 8
        $this->offsetName = $offsetName;
26 8
        $this->limitName = $limitName;
27 8
    }
28
29
    /**
30
     * @param array $request
31
     * @param array $config
32
     * @return PaginationRequest
33
     */
34 6
    public function create(array $request, array $config)
35
    {
36 6
        $offsetName = isset($config['offset']) ? $config['offset'] : $this->offsetName;
37 6
        $limitName = isset($config['limit']) ? $config['limit'] : $this->limitName;
38
39 6
        $resolver = $this->configureResolver($request, $offsetName, $limitName);
40 6
        $options = $resolver->resolve($request);
41
42 6
        return new PaginationRequest($options[$offsetName], $options[$limitName]);
43
    }
44
45
    /**
46
     * @param array $options
47
     * @param string $offsetName
48
     * @param string $limitName
49
     * @return OptionsResolver
50
     */
51 6
    private function configureResolver(array $options, $offsetName, $limitName)
52
    {
53 6
        $optionsResolver = new OptionsResolver();
54
55 6
        if (method_exists($optionsResolver, 'setDefined')) {
56 6
            $optionsResolver->setDefined(array_merge(array(
57 6
                $offsetName,
58
                $limitName
59 6
            ), array_keys($options)));
60 6
        } else {
61
            // BC with symfony < 2.6
62
            $optionsResolver->setOptional(array_merge(array(
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...
63
                $offsetName,
64
                $limitName
65
            ), array_keys($options)));
66
        }
67
68 6
        $optionsResolver->setDefaults(array(
69 6
            $offsetName => 0,
70
            $limitName => 15
71 6
        ));
72
73 6
        return $optionsResolver;
74
    }
75
}
76