LastModified::by()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
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 DateTime;
12
use Respect\Rest\Request;
13
14
class LastModified extends AbstractRoutine implements ProxyableBy, Unique
15
{
16 4
    public function by(Request $request, $params)
0 ignored issues
show
Coding Style introduced by
by 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...
17
    {
18 4
        if (!isset($_SERVER['IF_MODIFIED_SINCE'])) {
19 1
            return true;
20
        }
21
22 4
        $ifModifiedSince = new DateTime($_SERVER['IF_MODIFIED_SINCE']);
23 4
        $lastModifiedOn = call_user_func($this->callback, $params);
24
25 4
        header('Last-Modified: '.$lastModifiedOn->format(DateTime::RFC2822));
26 4
        if ($lastModifiedOn <= $ifModifiedSince) {
27 3
            header('HTTP/1.1 304 Not Modified');
28
29 3
            return false;
30
        }
31 2
    }
32
}
33