GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

MediaTypeNegotiator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 58
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A negotiate() 0 11 4
A addHandler() 0 7 2
A getSupportedMediaTypes() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Saikootau\ApiBundle\MediaType;
6
7
use Saikootau\ApiBundle\MediaType\Exception\NonNegotiableMediaTypeException;
8
9
class MediaTypeNegotiator
10
{
11
    /**
12
     * @var MediaTypeHandler[]
13
     */
14
    private $handlers;
15
16
    /**
17
     * Negotiate a media type handler best matching the given media types.
18
     *
19
     * @param string ...$mediaTypes
20
     *
21
     * @throws NonNegotiableMediaTypeException
22
     *
23
     * @return MediaTypeHandler
24
     */
25 3
    public function negotiate(string ...$mediaTypes): MediaTypeHandler
26
    {
27 3
        foreach ($mediaTypes as $mediaType) {
28 3
            foreach ($this->handlers as $handler) {
29 3
                if (in_array($mediaType, $handler->getSupportedMediaTypes())) {
30 3
                    return $handler;
31
                }
32
            }
33
        }
34
35 1
        throw new NonNegotiableMediaTypeException(implode(', ', $mediaTypes));
36
    }
37
38
    /**
39
     * Add a list of media type handlers for negotiation.
40
     *
41
     * @param MediaTypeHandler ...$handlers
42
     *
43
     * @return MediaTypeNegotiator
44
     */
45 4
    public function addHandler(MediaTypeHandler ...$handlers): self
46
    {
47 4
        foreach ($handlers as $handler) {
48 4
            $this->handlers[spl_object_hash($handler)] = $handler;
49
        }
50
51 4
        return $this;
52
    }
53
54
    /**
55
     * Get a list of all the media types supported by the registered handlers.
56
     *
57
     * @return string[]
58
     */
59 1
    public function getSupportedMediaTypes(): array
60
    {
61 1
        $mediaTypes = [];
62 1
        foreach ($this->handlers as $handler) {
63 1
            $mediaTypes = array_merge($handler->getSupportedMediaTypes(), $mediaTypes);
64
        }
65
66 1
        return array_values(array_unique($mediaTypes));
67
    }
68
}
69