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

ResponseBuilder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 40
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createResponse() 0 10 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