ModifiedSince   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 4
dl 0
loc 38
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getValue() 0 4 1
A getModifiedSince() 0 13 3
1
<?php declare(strict_types=1);
2
3
namespace Debril\RssAtomBundle\Request;
4
5
6
use Psr\Log\LoggerInterface;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpFoundation\RequestStack;
9
10
class ModifiedSince
11
{
12
13
    const HTTP_HEADER_NAME = 'If-Modified-Since';
14
15
    private $value;
16
17
    private $logger;
18
19 10
    public function __construct(RequestStack $requestStack, LoggerInterface $logger)
20
    {
21 10
        $this->logger = $logger;
22 10
        $this->value = $this->getModifiedSince($requestStack->getCurrentRequest());
0 ignored issues
show
Bug introduced by
It seems like $requestStack->getCurrentRequest() can be null; however, getModifiedSince() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
23 10
    }
24
25
    /**
26
     * @return \DateTime
27
     */
28 9
    public function getValue(): \DateTime
29
    {
30 9
        return $this->value;
31
    }
32
33 10
    private function getModifiedSince(Request $request):\DateTime
34
    {
35 10
        if ($request->headers->has(self::HTTP_HEADER_NAME)) {
36
            try {
37 5
                $string = $request->headers->get(self::HTTP_HEADER_NAME);
38 5
                return new \DateTime($string);
39 2
            } catch (\TypeError|\Exception $e) {
40 2
                $this->logger->notice(sprintf('If-Modified-Since Header has a unexpected value, exception was %s', $e->getMessage()));
41
            }
42
        }
43
44 8
        return new \DateTime('@1');
45
    }
46
47
}
48