Completed
Push — master ( 6fed26...e3ac15 )
by Guilh
08:12
created

Resources/doc/examples/RssHandler.php (2 issues)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\RestBundle\Examples;
13
14
use FOS\RestBundle\View\View;
15
use FOS\RestBundle\View\ViewHandler;
16
use Psr\Log\LoggerInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
20
/**
21
 * This is an example RSS ViewHandler.
22
 * It also shows how to handle exceptions within the ViewHandler so that the
23
 * client can get a decent response.
24
 *
25
 * Please note that you will need to install the Zend library to use this
26
 * handler.
27
 *
28
 * Configuration:
29
 *
30
 * services:
31
 *   my.rss_handler:
32
 *     class: FOS\RestBundle\Examples\RssHandler
33
 *     arguments:
34
 *       logger: "@?logger"
35
 *
36
 *   my.view_handler:
37
 *     parent: fos_rest.view_handler.default
38
 *     calls:
39
 *      - ['registerHandler', [ 'rss', ["@my.rss_handler", 'createResponse'] ] ]
40
 *
41
 * fos_rest:
42
 *   service:
43
 *     view_handler: my.view_handler
44
 *
45
 * @author Tarjei Huse (tarjei - at scanmine.com)
46
 */
47
class RssHandler
48
{
49
    private $logger;
50
51
    public function __construct(LoggerInterface $logger = null)
52
    {
53
        $this->logger = $logger;
54
    }
55
56
    /**
57
     * Converts the viewdata to a RSS feed. Modify to suit your datastructure.
58
     *
59
     * @return Response
60
     */
61
    public function createResponse(ViewHandler $handler, View $view, Request $request)
0 ignored issues
show
The parameter $handler is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
    {
63
        try {
64
            $content = $this->createFeed($view->getData());
65
            $code = Response::HTTP_OK;
66
        } catch (\Exception $e) {
67
            if ($this->logger) {
68
                $this->logger->error($e);
69
            }
70
71
            $content = sprintf('%s:<br/><pre>%s</pre>', $e->getMessage(), $e->getTraceAsString());
72
            $code = Response::HTTP_BAD_REQUEST;
73
        }
74
75
        return new Response($content, $code, $view->getHeaders());
76
    }
77
78
    /**
79
     * @param $data array
80
     * @param format string, either rss or atom
81
     */
82
    protected function createFeed($data, $format = 'rss')
83
    {
84
        $feed = new \Zend_Feed_Writer_Feed();
85
        $feed->setTitle($data['title']);
86
        $feed->setLink($data['link']);
87
        $feed->setFeedLink($data['link'], 'rss');
88
        $feed->addAuthor([
89
            'name' => 'ZeroCMS',
90
            'email' => 'email!',
91
        ]);
92
        $feed->setDateModified(time());
93
        $feed->setDescription('RSS feed from query');
94
95
        // Add one or more entries. Note that entries must be manually added once created.
96
        foreach ($data['documents'] as $document) {
97
            $entry = $feed->createEntry();
98
99
            $entry->setTitle($document['title']);
100
            $entry->setLink($document['url']);
101
            $entry->addAuthor([
102
                'name' => $document['author'],
103
                //'email' => '',
104
                //'uri'   => '',
105
            ]);
106
107
            $entry->setDateModified($document['dateUpdated']->getTimestamp());
108
            $entry->setDateCreated($document['dateCreated']->getTimestamp());
109
110
            if (isset($document['summary'])) {
111
                $entry->setDescription($document['summary']);
112
            }
113
114
            $entry->setContent($document['body']);
115
            $feed->addEntry($entry);
116
        }
117
118
        return $feed->export($format);
119
    }
120
}
121