Completed
Push — travis-force-precise ( 6dcf00 )
by Kamil
77:22 queued 56:03
created

RequestConfigurationFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 74
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A create() 0 7 1
B parseApiParameters() 0 20 5
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Bundle\ResourceBundle\Controller;
15
16
use Sylius\Component\Resource\Metadata\MetadataInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
19
/**
20
 * @author Paweł Jędrzejewski <[email protected]>
21
 */
22
final class RequestConfigurationFactory implements RequestConfigurationFactoryInterface
23
{
24
    const API_VERSION_HEADER = 'Accept';
25
    const API_GROUPS_HEADER = 'Accept';
26
27
    const API_VERSION_REGEXP = '/(v|version)=(?P<version>[0-9\.]+)/i';
28
    const API_GROUPS_REGEXP = '/(g|groups)=(?P<groups>[a-z,_\s]+)/i';
29
30
    /**
31
     * @var ParametersParserInterface
32
     */
33
    private $parametersParser;
34
35
    /**
36
     * @var string
37
     */
38
    private $configurationClass;
39
40
    /**
41
     * @var array
42
     */
43
    private $defaultParameters;
44
45
    /**
46
     * @param ParametersParserInterface $parametersParser
47
     * @param string $configurationClass
48
     * @param array $defaultParameters
49
     */
50
    public function __construct(ParametersParserInterface $parametersParser, $configurationClass, array $defaultParameters = [])
51
    {
52
        $this->parametersParser = $parametersParser;
53
        $this->configurationClass = $configurationClass;
54
        $this->defaultParameters = $defaultParameters;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function create(MetadataInterface $metadata, Request $request)
61
    {
62
        $parameters = array_merge($this->defaultParameters, $this->parseApiParameters($request));
63
        $parameters = $this->parametersParser->parseRequestValues($parameters, $request);
64
65
        return new $this->configurationClass($metadata, $request, new Parameters($parameters));
66
    }
67
68
    /**
69
     * @param Request $request
70
     *
71
     * @return array
72
     *
73
     * @throws \InvalidArgumentException
74
     */
75
    private function parseApiParameters(Request $request)
76
    {
77
        $parameters = [];
78
79
        $apiVersionHeaders = $request->headers->get(self::API_VERSION_HEADER, null, false);
80
        foreach ($apiVersionHeaders as $apiVersionHeader) {
0 ignored issues
show
Bug introduced by
The expression $apiVersionHeaders of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
81
            if (preg_match(self::API_VERSION_REGEXP, $apiVersionHeader, $matches)) {
82
                $parameters['serialization_version'] = $matches['version'];
83
            }
84
        }
85
86
        $apiGroupsHeaders = $request->headers->get(self::API_GROUPS_HEADER, null, false);
87
        foreach ($apiGroupsHeaders as $apiGroupsHeader) {
0 ignored issues
show
Bug introduced by
The expression $apiGroupsHeaders of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
88
            if (preg_match(self::API_GROUPS_REGEXP, $apiGroupsHeader, $matches)) {
89
                $parameters['serialization_groups'] = array_map('trim', explode(',', $matches['groups']));
90
            }
91
        }
92
93
        return array_merge($request->attributes->get('_sylius', []), $parameters);
94
    }
95
}
96