HeadersBuilder   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 61
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setContentType() 0 4 1
A setResponseHeaders() 0 9 2
A getContentType() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace Debril\RssAtomBundle\Response;
4
5
6
use Symfony\Component\HttpFoundation\Response;
7
8
class HeadersBuilder
9
{
10
11
    const DEFAULT_MAX_AGE = 3600;
12
13
    const FORMAT_XML = 'xml';
14
15
    const DEFAULT_XML_CONTENT_TYPE = 'application/xhtml+xml';
16
17
    /**
18
     * supported content-types
19
     * @var array
20
     */
21
    private $contentTypes = [
22
        self::FORMAT_XML => self::DEFAULT_XML_CONTENT_TYPE
23
    ];
24
25
    /**
26
     * if true, the response is marked s public
27
     * @var bool
28
     */
29
    private $public;
30
31
    /**
32
     * maximum amount of time before the cache gets invalidated (in seconds)
33
     * @var int
34
     */
35
    private $maxAge;
36
37
    /**
38
     * HeadersBuilder constructor.
39
     * @param $public
40
     * @param $maxAge
41
     */
42 9
    public function __construct(bool $public = true, int $maxAge = self::DEFAULT_MAX_AGE)
43
    {
44 9
        $this->public = $public;
45 9
        $this->maxAge = $maxAge;
46 9
    }
47
48 5
    public function setContentType(string $format, $value): void
49
    {
50 5
        $this->contentTypes[$format] = $value;
51 5
    }
52
53 8
    public function setResponseHeaders(Response $response, string $format, \DateTime $lastModified): void
54
    {
55 8
        $response->headers->set('Content-Type', $this->getContentType($format));
56
57 8
        $this->public ? $response->setPublic() : $response->setPrivate();
58
59 8
        $response->setMaxAge($this->maxAge);
60 8
        $response->setLastModified($lastModified);
61 8
    }
62
63 8
    private function getContentType(string $format): string
64
    {
65 8
        return $this->contentTypes[$format] ?? $this->contentTypes[self::FORMAT_XML];
66
    }
67
68
}
69