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::addHandler()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
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