UserAgent   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 39
ccs 19
cts 19
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A identifyRequested() 0 4 1
A considerProvisions() 0 4 1
A notifyApproved() 0 5 1
A notifyDeclined() 0 4 1
A authorize() 0 8 3
A through() 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 User Agent filters */
15
class UserAgent extends AbstractCallbackMediator implements ProxyableThrough, Unique
16
{
17
    const ACCEPT_HEADER = 'HTTP_USER_AGENT';
18
    private $negotiated = false;
19
20 4
    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 4
        return array($_SERVER[self::ACCEPT_HEADER]);
23
    }
24 4
    protected function considerProvisions($requested)
25
    {
26 4
        return $this->getKeys();
27
    }
28 3
    protected function notifyApproved($requested, $provided, Request $request, $params)
29
    {
30 3
        $this->negotiated = new SplObjectStorage();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \SplObjectStorage() of type object<SplObjectStorage> is incompatible with the declared type boolean of property $negotiated.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
31 3
        $this->negotiated[$request] = $this->getCallback($provided);
32 3
    }
33 1
    protected function notifyDeclined($requested, $provided, Request $request, $params)
34
    {
35 1
        $this->negotiated = false;
36 1
    }
37
38 5
    protected function authorize($requested, $provided)
39
    {
40 5
        if ($provided === '*' || preg_match("#$provided#", $requested)) {
41 4
            return true;
42
        }
43
44 3
        return false;
45
    }
46
47 4
    public function through(Request $request, $params)
48
    {
49 4
        if (false !== $this->negotiated) {
50 3
            return $this->negotiated[$request];
51
        }
52 1
    }
53
}
54