Completed
Push — master ( d34cec...faecd8 )
by Augusto
02:22
created

ContentType   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A identifyRequested() 0 4 2
A considerProvisions() 0 4 1
A notifyDeclined() 0 4 1
A notifyApproved() 0 5 1
A by() 0 6 2
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