Completed
Push — master ( f4be3d...ae82a1 )
by Andreas
6s
created

RssRenderer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
c 1
b 0
f 1
lcom 0
cbo 4
dl 0
loc 56
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B render() 0 45 3
1
<?php
2
/**
3
 * Copyright (c) 2016-2016} Andreas Heigl<[email protected]>
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a copy
6
 * of this software and associated documentation files (the "Software"), to deal
7
 * in the Software without restriction, including without limitation the rights
8
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the Software is
10
 * furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included in
13
 * all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 *
23
 * @author    Andreas Heigl<[email protected]>
24
 * @copyright 2016-2016 Andreas Heigl
25
 * @license   http://www.opensource.org/licenses/mit-license.php MIT-License
26
 * @version   0.0
27
 * @since     16.01.2016
28
 * @link      http://github.com/heiglandreas/callingallpapers
29
 */
30
namespace Callingallpapers\Api\Renderer;
31
32
use Psr\Http\Message\ResponseInterface;
33
use Zend\Feed\Writer\Feed;
34
35
class RssRenderer
36
{
37
    /**
38
     *
39
     * @param Response $response
40
     * @param array $data
41
     * @param int $status
42
     *
43
     * @return ResponseInterface
44
     */
45
    public function render(ResponseInterface $response, array $data = [], $status = 200)
46
    {
47
        $feed = new Feed();
48
        $feed->setTitle('Calling all Papers');
49
        $feed->setDescription('Calls for Papers that are currently open');
50
        $feed->setLink('http://callingallpapers.com');
51
        $feed->setFeedLink('http://api.callingallpapers.com/v1/cfp', 'rss');
52
        $feed->setFeedLink('http://api.callingallpapers.com/v1/cfp', 'atom');
53
        $feed->addAuthor([
54
            'name' => 'Andreas Heigl',
55
            'email' => '[email protected]',
56
            'uri'   => 'http://andreas.heigl.org',
57
        ]);
58
        $feed->setDateModified(time());
59
        if (! isset($data['cfps'])) {
60
            $data['cfps'] = [];
61
        }
62
        foreach ($data['cfps'] as $cfp) {
63
            $lastChange = new \DateTime($cfp['lastChange']);
64
            $lastChange->setTimezone(new \DateTimeZone('UTC'));
65
66
            $entry = $feed->createEntry();
67
            $entry->setTitle($cfp['name']);
68
            $entry->setLink($cfp['uri']);
69
            $entry->setDateModified(new \DateTime($cfp['dateCfpEnd']));
70
            $entry->setDateCreated(new \DateTime($cfp['dateCfpEnd']));
71
            $entry->setDescription(sprintf(
72
                'CfP for %3$s runs from %1$s to %2$s',
73
                (new \DateTime($cfp['dateCfpStart']))->format('c'),
74
                (new \DateTime($cfp['dateCfpEnd']))->format('c'),
75
                $cfp['name']
76
            ));
77
            $entry->setContent($cfp['description']);
78
79
            $feed->addEntry($entry);
80
        }
81
82
        $response = $response->withHeader('Content-Type', 'application/rss+xml');
83
        $response = $response->withStatus($status);
84
85
        $stream = $response->getBody();
86
        $stream->write($feed->export('rss'));
87
88
        return $response->withBody($stream);
89
    }
90
}
91