Completed
Pull Request — 2.x (#2288)
by Guilhem
17:20
created

FormatNegotiator::getRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 5
cp 0.8
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.032
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\AcceptHeader;
17
use Negotiation\Negotiator as BaseNegotiator;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
20
use Symfony\Component\HttpFoundation\RequestStack;
21
22
/**
23
 * @author Guilhem Niot <[email protected]>
24
 *
25
 * @final since 2.8
26
 */
27
final class FormatNegotiator extends BaseNegotiator
28
{
29
    private $map = [];
30
    private $requestStack;
31
    private $mimeTypes;
32
33 25
    public function __construct(RequestStack $requestStack, array $mimeTypes = [])
34
    {
35 25
        $this->requestStack = $requestStack;
36 25
        $this->mimeTypes = $mimeTypes;
37 25
    }
38
39 23
    public function add(RequestMatcherInterface $requestMatcher, array $options = []): void
40
    {
41 23
        $this->map[] = [$requestMatcher, $options];
42 23
    }
43
44 21
    public function getBest($header, array $priorities = [], $strict = false): ?AcceptHeader
45
    {
46 21
        $request = $this->getRequest();
47 21
        $header = $header ?: $request->headers->get('Accept');
48
49 21
        foreach ($this->map as $elements) {
50
            // Check if the current RequestMatcherInterface matches the current request
51 19
            if (!$elements[0]->matches($request)) {
52 1
                continue;
53
            }
54 19
            $options = &$elements[1]; // Do not reallow memory for this variable
55
56 19
            if (!empty($options['stop'])) {
57 1
                throw new StopFormatListenerException('Stopped format listener');
58
            }
59 18
            if (empty($options['priorities']) && empty($priorities)) {
60 3
                if (!empty($options['fallback_format'])) {
61 3
                    return new Accept($request->getMimeType($options['fallback_format']));
62
                }
63
64 1
                continue;
65
            }
66
67 15
            if (isset($options['prefer_extension']) && $options['prefer_extension'] && !isset($extensionHeader)) {
68 10
                $extension = pathinfo($request->getPathInfo(), PATHINFO_EXTENSION);
69
70 10
                if (!empty($extension)) {
71
                    // $extensionHeader will now be either a non empty string or an empty string
72 5
                    $extensionHeader = $request->getMimeType($extension);
73
74 5
                    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...
75 2
                        $header = $extensionHeader.'; q='.$options['prefer_extension'].($header ? ','.$header : '');
76
                    }
77
                }
78
            }
79
80 15
            if ($header) {
81 14
                $mimeTypes = $this->normalizePriorities(
82 14
                    $request,
83 14
                    empty($priorities) ? $options['priorities'] : $priorities
84
                );
85
86 14
                $mimeType = parent::getBest($header, $mimeTypes);
87
88 14
                if (null !== $mimeType) {
89 13
                    return $mimeType;
90
                }
91
            }
92
93 2
            if (isset($options['fallback_format'])) {
94
                // if false === fallback_format then we fail here instead of considering more rules
95 2
                if (false === $options['fallback_format']) {
96
                    return null;
97
                }
98
99
                // stop looking at rules since we have a fallback defined
100 2
                return new Accept($request->getMimeType($options['fallback_format']));
101
            }
102
        }
103
104 4
        return null;
105
    }
106
107 14
    private function sanitize(array $values): array
108
    {
109
        return array_map(function ($value) {
110 14
            return preg_replace('/\s+/', '', strtolower($value));
111 14
        }, $values);
112
    }
113
114
    /**
115
     * @param string[] $priorities
116
     *
117
     * @return string[] formatted priorities
118
     */
119 14
    private function normalizePriorities(Request $request, array $priorities): array
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
120
    {
121 14
        $priorities = $this->sanitize($priorities);
122
123 14
        $mimeTypes = [];
124 14
        foreach ($priorities as $priority) {
125 14
            if (strpos($priority, '/')) {
126 4
                $mimeTypes[] = $priority;
127
128 4
                continue;
129
            }
130
131 11
            $mimeTypes = array_merge($mimeTypes, Request::getMimeTypes($priority));
132
133 11
            if (isset($this->mimeTypes[$priority])) {
134 3
                foreach ($this->mimeTypes[$priority] as $mimeType) {
135 3
                    $mimeTypes[] = $mimeType;
136
                }
137
            }
138
        }
139
140 14
        return $mimeTypes;
141
    }
142
143 21
    private function getRequest(): Request
144
    {
145 21
        $request = $this->requestStack->getCurrentRequest();
146 21
        if (null === $request) {
147
            throw new \RuntimeException('There is no current request.');
148
        }
149
150 21
        return $request;
151
    }
152
}
153