ContentType::notifyDeclined()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 1
1
<?php
2
/*
3
 * This file is part of the Respect\Rest package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Respect\Rest\Routines;
10
11
use SplObjectStorage;
12
use Respect\Rest\Request;
13
14
/** Handles content type content negotiation */
15
class ContentType extends AbstractCallbackMediator implements ProxyableBy, Unique
16
{
17
    protected $contentMap = array();
18
    protected $negotiated = null;
19
20 2
    protected function identifyRequested(Request $request, $params)
0 ignored issues
show
Coding Style introduced by
identifyRequested uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
21
    {
22 2
        return isset($_SERVER['CONTENT_TYPE']) ? array($_SERVER['CONTENT_TYPE']) : array();
23
    }
24 2
    protected function considerProvisions($requested)
25
    {
26 2
        return $this->getKeys();
27
    }
28 2
    protected function notifyApproved($requested, $provided, Request $request, $params)
29
    {
30 2
        $this->negotiated = new SplObjectStorage();
31 2
        $this->negotiated[$request] = $this->getCallback($provided);
32 2
    }
33 1
    protected function notifyDeclined($requested, $provided, Request $request, $params)
34
    {
35 1
        $this->negotiated = false;
36 1
    }
37
38 2
    public function by(Request $request, $params)
39
    {
40 2
        if (false !== $this->negotiated) {
41 2
            return call_user_func($this->negotiated[$request]);
42
        }
43 1
    }
44
}
45