Completed
Push — 2.x ( 0e1f1b...77cf8b )
by Christian
41s queued 16s
created

FormatNegotiator::getBest()   D

Complexity

Conditions 19
Paths 80

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 19.0121

Importance

Changes 0
Metric Value
dl 0
loc 60
ccs 30
cts 31
cp 0.9677
rs 4.5166
c 0
b 0
f 0
cc 19
nc 80
nop 2
crap 19.0121

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
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
namespace FOS\RestBundle\Negotiation;
13
14
use FOS\RestBundle\Util\StopFormatListenerException;
15
use Negotiation\Accept;
16
use Negotiation\Negotiator as BaseNegotiator;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
19
use Symfony\Component\HttpFoundation\RequestStack;
20
21
/**
22
 * @author Ener-Getick <[email protected]>
23
 *
24
 * @final since 2.8
25
 */
26
class FormatNegotiator extends BaseNegotiator
27
{
28
    private $map = [];
29
    private $requestStack;
30
    private $mimeTypes;
31
32 29
    public function __construct(RequestStack $requestStack, array $mimeTypes = array())
33
    {
34 29
        $this->requestStack = $requestStack;
35 29
        $this->mimeTypes = $mimeTypes;
36 29
    }
37
38 27
    public function add(RequestMatcherInterface $requestMatcher, array $options = [])
39
    {
40 27
        $this->map[] = [$requestMatcher, $options];
41 27
    }
42
43 24
    public function getBest($header, array $priorities = [])
44
    {
45 24
        $request = $this->getRequest();
46 24
        $header = $header ?: $request->headers->get('Accept');
47
48 24
        foreach ($this->map as $elements) {
49
            // Check if the current RequestMatcherInterface matches the current request
50 22
            if (!$elements[0]->matches($request)) {
51 1
                continue;
52
            }
53 22
            $options = &$elements[1]; // Do not reallow memory for this variable
54
55 22
            if (!empty($options['stop'])) {
56 1
                throw new StopFormatListenerException('Stopped format listener');
57
            }
58 21
            if (empty($options['priorities']) && empty($priorities)) {
59 4
                if (!empty($options['fallback_format'])) {
60 4
                    return new Accept($request->getMimeType($options['fallback_format']));
61
                }
62
63 1
                continue;
64
            }
65
66 17
            if (isset($options['prefer_extension']) && $options['prefer_extension'] && !isset($extensionHeader)) {
67 12
                $extension = pathinfo($request->getPathInfo(), PATHINFO_EXTENSION);
68
69 12
                if (!empty($extension)) {
70
                    // $extensionHeader will now be either a non empty string or an empty string
71 6
                    $extensionHeader = $request->getMimeType($extension);
72
73 6
                    if ($extensionHeader) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $extensionHeader of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
74 3
                        $header = $extensionHeader.'; q='.$options['prefer_extension'].($header ? ','.$header : '');
75
                    }
76
                }
77
            }
78
79 17
            if ($header) {
80 16
                $mimeTypes = $this->normalizePriorities(
81 16
                    $request,
82 16
                    empty($priorities) ? $options['priorities'] : $priorities
83
                );
84
85 16
                $mimeType = parent::getBest($header, $mimeTypes);
86
87 16
                if (null !== $mimeType) {
88 15
                    return $mimeType;
89
                }
90
            }
91
92 2
            if (isset($options['fallback_format'])) {
93
                // if false === fallback_format then we fail here instead of considering more rules
94 2
                if (false === $options['fallback_format']) {
95
                    return;
96
                }
97
98
                // stop looking at rules since we have a fallback defined
99 2
                return new Accept($request->getMimeType($options['fallback_format']));
100
            }
101
        }
102 4
    }
103
104 16
    private function sanitize(array $values): array
105
    {
106
        return array_map(function ($value) {
107 16
            return preg_replace('/\s+/', '', strtolower($value));
108 16
        }, $values);
109
    }
110
111
    /**
112
     * @param string[] $priorities
113
     *
114
     * @return string[] formatted priorities
115
     */
116 16
    private function normalizePriorities(Request $request, array $priorities): array
117
    {
118 16
        $priorities = $this->sanitize($priorities);
119
120 16
        $mimeTypes = array();
121 16
        foreach ($priorities as $priority) {
122 16
            if (strpos($priority, '/')) {
123 4
                $mimeTypes[] = $priority;
124
125 4
                continue;
126
            }
127
128 13
            if (method_exists(Request::class, 'getMimeTypes')) {
129 13
                $mimeTypes = array_merge($mimeTypes, Request::getMimeTypes($priority));
130
            } elseif (null !== $request->getMimeType($priority)) {
131
                $class = new \ReflectionClass(Request::class);
132
                $properties = $class->getStaticProperties();
133
                $mimeTypes = array_merge($mimeTypes, $properties['formats'][$priority]);
134
            }
135
136 13
            if (isset($this->mimeTypes[$priority])) {
137 3
                foreach ($this->mimeTypes[$priority] as $mimeType) {
138 3
                    $mimeTypes[] = $mimeType;
139
                }
140
            }
141
        }
142
143 16
        return $mimeTypes;
144
    }
145
146 24
    private function getRequest(): Request
147
    {
148 24
        $request = $this->requestStack->getCurrentRequest();
149 24
        if (null === $request) {
150
            throw new \RuntimeException('There is no current request.');
151
        }
152
153 24
        return $request;
154
    }
155
}
156