Completed
Push — master ( 471a2c...9baf90 )
by Alex
14s
created

ResponseBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the feed-io package.
4
 *
5
 * (c) Alexandre Debril <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace FeedIo\Http;
12
13
use FeedIo\FeedInterface;
14
use FeedIo\FormatterInterface;
15
use GuzzleHttp\Psr7\Response;
16
use Psr\Http\Message\ResponseInterface;
17
18
class ResponseBuilder
19
{
20
21
    /**
22
     * @var int $maxAge max-age in seconds
23
     */
24
    protected $maxAge;
25
26
    /**
27
     * @var bool $public is the response public
28
     */
29
    protected $public;
30
31
    /**
32
     * @param int $maxAge
33
     * @param bool $public
34
     */
35 3
    public function __construct(int $maxAge = 600, bool $public = true)
36
    {
37 3
        $this->maxAge = $maxAge;
38 3
        $this->public = $public;
39 3
    }
40
41
    /**
42
     * @param  string $format
43
     * @param  FormatterInterface $formatter
44
     * @param  FeedInterface $feed
45
     * @return ResponseInterface
46
     */
47 3
    public function createResponse(string $format, FormatterInterface $formatter, FeedInterface $feed) : ResponseInterface
48
    {
49
        $headers = [
50 3
            'Content-Type' => ($format === 'json') ? 'application/json':'application/xhtml+xml',
51 3
            'Cache-Control' => ($this->public ? 'public':'private') . ", max-age={$this->maxAge}",
52 3
            'Last-Modified' => $feed->getLastModified()->format(\DateTime::RSS),
53
        ];
54
55 3
        return new Response(200, $headers, $formatter->toString($feed));
56
    }
57
}
58