ResponseHandler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 35
ccs 11
cts 11
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A negociate() 0 12 3
1
<?php
2
3
/*
4
 * This file is part of the "Kata 1" package.
5
 *
6
 * Copyright (c) Daniel González
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @author Daniel González <[email protected]>
12
 */
13
14
namespace App\Negociation\Handler;
15
16
use Negotiation\Negotiator;
17
18
19
20
use Component\Http\JsonResponse;
21
use Component\Http\Request;
22
use Component\Http\Response;
23
24
/**
25
 * ResponseHandler.
26
 */
27
class ResponseHandler
28
{
29
    /**
30
     * @var Negotiator
31
     */
32
    protected $negotiator;
33
34
    /**
35
     * @param Negotiator $negotiator
36
     */
37 7
    public function __construct(Negotiator $negotiator)
38
    {
39 7
        $this->negotiator = $negotiator;
40 7
    }
41
42
    /**
43
     * @param Request $request
44
     * @param array   $data
45
     * @param int     $status
46
     *
47
     * @return Response
48
     */
49 7
    public function negociate(Request $request, array $data = [], $status = Response::HTTP_OK)
50
    {
51 7
        $acceptHeader = $request->getHeader('Accept') ? $request->getHeader('Accept') : 'application/json';
52 7
        $priorities = ['application/json', 'text/html; charset=UTF-8'];
53 7
        $mediaType = $this->negotiator->getBest($acceptHeader, $priorities);
54 7
        $value = $mediaType->getValue();
55 7
        if ($value == 'text/html; charset=UTF-8') {
56 2
            return new Response(print_r($data, true), $status);
57
        }
58
59 5
        return new JsonResponse($data, $status);
0 ignored issues
show
Documentation introduced by
$data is of type array, but the function expects a string.

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...
60
    }
61
}
62