PaginationOptionsFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 87.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 69
ccs 21
cts 24
cp 0.875
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A create() 0 10 3
B configureResolver() 0 24 2
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