Completed
Branch master (1a7fa9)
by
unknown
03:10
created

HeaderParametersParser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 63
wmc 7
lcom 1
cbo 6
ccs 24
cts 24
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A parse() 0 23 3
A getHeader() 0 12 3
1
<?php namespace Neomerx\JsonApi\Http\Headers;
2
3
/**
4
 * Copyright 2015 [email protected] (www.neomerx.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use \InvalidArgumentException;
20
use \Psr\Log\LoggerAwareTrait;
21
use \Psr\Log\LoggerAwareInterface;
22
use \Psr\Http\Message\ServerRequestInterface;
23
use \Neomerx\JsonApi\Exceptions\JsonApiException as E;
24
use \Neomerx\JsonApi\Contracts\Http\HttpFactoryInterface;
25
use \Neomerx\JsonApi\Contracts\Http\Headers\HeaderInterface;
26
use \Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface;
27
use \Neomerx\JsonApi\Contracts\Http\Headers\HeaderParametersParserInterface;
28
29
/**
30
 * @package Neomerx\JsonApi
31
 */
32
class HeaderParametersParser implements HeaderParametersParserInterface, LoggerAwareInterface
33
{
34
    use LoggerAwareTrait;
35
36
    /**
37
     * @var HttpFactoryInterface
38
     */
39
    private $factory;
40
41
    /**
42
     * @param HttpFactoryInterface $factory
43
     */
44 13
    public function __construct(HttpFactoryInterface $factory)
45
    {
46 13
        $this->factory = $factory;
47 13
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52 13
    public function parse(ServerRequestInterface $request)
53
    {
54 13
        $acceptHeader      = null;
55 13
        $contentTypeHeader = null;
56
57 13
        $method = $request->getMethod();
58
59
        try {
60 13
            $header            = $this->getHeader($request, HeaderInterface::HEADER_CONTENT_TYPE);
61 13
            $contentTypeHeader = Header::parse($header, HeaderInterface::HEADER_CONTENT_TYPE);
62 13
        } catch (InvalidArgumentException $exception) {
63 1
            E::throwException(new E([], E::HTTP_CODE_BAD_REQUEST, $exception));
64
        }
65
66
        try {
67 12
            $header       = $this->getHeader($request, HeaderInterface::HEADER_ACCEPT);
68 12
            $acceptHeader = AcceptHeader::parse($header);
69 12
        } catch (InvalidArgumentException $exception) {
70 1
            E::throwException(new E([], E::HTTP_CODE_BAD_REQUEST, $exception));
71
        }
72
73 11
        return $this->factory->createHeaderParameters($method, $acceptHeader, $contentTypeHeader);
0 ignored issues
show
Bug introduced by
It seems like $contentTypeHeader defined by null on line 55 can be null; however, Neomerx\JsonApi\Contract...reateHeaderParameters() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
Documentation introduced by
$acceptHeader is of type null|object<Neomerx\JsonApi\Http\Headers\Header>, but the function expects a object<Neomerx\JsonApi\C...\AcceptHeaderInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
    }
75
76
    /**
77
     * @param ServerRequestInterface $request
78
     * @param string                 $name
79
     *
80
     * @return string
81
     */
82 13
    private function getHeader(ServerRequestInterface $request, $name)
83
    {
84 13
        $value = $request->getHeader($name);
85 13
        if (empty($value) === false) {
86 13
            $value = $value[0];
87 13
            if (empty($value) === false) {
88 13
                return $value;
89
            }
90 2
        }
91
92 2
        return MediaTypeInterface::JSON_API_MEDIA_TYPE;
93
    }
94
}
95