Completed
Push — master ( 64af60...471a2c )
by Alex
14s
created

ResponseBuilder::createResponse()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
nop 3
crap 3
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