MediaTypeVersionResolver   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 21
ccs 6
cts 7
cp 0.8571
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resolve() 0 8 4
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\Version\Resolver;
13
14
use FOS\RestBundle\Version\VersionResolverInterface;
15
use Symfony\Component\HttpFoundation\Request;
16
17
/**
18
 * @author Ener-Getick <[email protected]>
19
 */
20
final class MediaTypeVersionResolver implements VersionResolverInterface
21
{
22
    private $regex;
23
24 7
    public function __construct(string $regex)
25
    {
26 7
        $this->regex = $regex;
27 7
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 6
    public function resolve(Request $request): ?string
33
    {
34 6
        if (!$request->attributes->has('media_type') || false === preg_match($this->regex, $request->attributes->get('media_type'), $matches)) {
35
            return null;
36
        }
37
38 6
        return isset($matches['version']) ? $matches['version'] : null;
39
    }
40
}
41