Completed
Pull Request — master (#187)
by Alex
09:00
created

ModifiedSince   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 0
loc 30
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getValue() 0 4 1
A getModifiedSince() 0 9 2
1
<?php declare(strict_types=1);
2
3
namespace Debril\RssAtomBundle\Request;
4
5
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpFoundation\RequestStack;
8
9
class ModifiedSince
10
{
11
12
    const HTTP_HEADER_NAME = 'If-Modified-Since';
13
14
    private $value;
15
16
    public function __construct(RequestStack $requestStack)
17
    {
18
        $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...
19
    }
20
21
    /**
22
     * @return \DateTime
23
     */
24
    public function getValue(): \DateTime
25
    {
26
        return $this->value;
27
    }
28
29
    private function getModifiedSince(Request $request):\DateTime
30
    {
31
        if ($request->headers->has(self::HTTP_HEADER_NAME)) {
32
            $string = $request->headers->get(self::HTTP_HEADER_NAME);
33
            return \DateTime::createFromFormat(\DateTime::RSS, $string);
34
        }
35
36
        return new \DateTime('@1');
37
    }
38
}
39