Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rss feed #55

Merged
merged 7 commits into from
Mar 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions module/Application/config/controller.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,20 @@
$em->getRepository('Application\Model\Entity\PointLog')
);
},
'Application\Controller\Feed' => function(ControllerManager $cm) {
$sm = $cm->getServiceLocator();
$em = $sm->get('doctrine.entitymanager.orm_default');

$matchFeeder = new \Application\Model\MatchFeeder(
new \Zend\Feed\Writer\Feed,
$em->getRepository('Application\Model\Entity\Match')
);
$matchFeeder->setTitle('Fussi: Recent matches');

$baseUrl = sprintf('%s%s', 'http://', $_SERVER['HTTP_HOST']);
$matchFeeder->setUrl($baseUrl);

return new Controller\FeedController($matchFeeder);
},
)
);
10 changes: 10 additions & 0 deletions module/Application/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,16 @@
),
)
),
'feed' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/feed/recent-matches',
'defaults' => array(
'controller' => 'Application\Controller\Feed',
'action' => 'lastMatches'
),
),
),
),
),
'console' => array(
Expand Down
61 changes: 61 additions & 0 deletions module/Application/src/Application/Controller/FeedController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Definition of Application\Controller\FeedController
*
* @copyright Copyright (c) 2014 The Fußi-Team
* @license THE BEER-WARE LICENSE (Revision 42)
*
* "THE BEER-WARE LICENSE" (Revision 42):
* The Fußi-Team wrote this software. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy us a beer in return.
*/

namespace Application\Controller;

use Application\Model\MatchFeeder;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

/**
* Controller handles all kind of possible feeds
* For example: rss
*/
class FeedController extends AbstractActionController
{
/**
* @var MatchFeeder
*/
protected $matchFeeder;

/**
* @param MatchFeeder $matchFeeder
*/
public function __construct(MatchFeeder $matchFeeder)
{
$this->matchFeeder = $matchFeeder;
}

/**
* @return ViewModel
*/
public function lastMatchesAction()
{
$feed = $this->matchFeeder->generate();

return $this->renderFeed($feed);
}

/**
* @param $feed
*
* @return ViewModel
*/
private function renderFeed($feed)
{
$response = $this->getResponse();
$response->setContent($feed);

return $response;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Definition of Application\Controller\DashboardController
*
* @copyright Copyright (c) 2014 The Fußi-Team
* @license THE BEER-WARE LICENSE (Revision 42)
*
* "THE BEER-WARE LICENSE" (Revision 42):
* The Fußi-Team wrote this software. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy us a beer in return.
*/

namespace Application\Model\Exceptions;

class UrlNotFoundException extends \Exception
{

}
179 changes: 179 additions & 0 deletions module/Application/src/Application/Model/MatchFeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
<?php
/**
* Definition of Application\Model\MatchFeeder
*
* @copyright Copyright (c) 2014 The Fußi-Team
* @license THE BEER-WARE LICENSE (Revision 42)
*
* "THE BEER-WARE LICENSE" (Revision 42):
* The Fußi-Team wrote this software. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy us a beer in return.
*/

namespace Application\Model;

use Application\Model\Exceptions\UrlNotFoundException;
use Application\Model\Repository\MatchRepository;
use Zend\Feed\Writer\Feed as FeedWriter;

/**
* Class MatchFeeder
* @package Application\Model
*/
class MatchFeeder
{
/**
* Feed type to render
*/
const FEED_TYPE = 'atom';

/**
* @var MatchRepository
*/
protected $matchRepository;

/**
* @var FeedWriter
*/
protected $feed;

/**
* @var string
*/
protected $url;

/**
* @var string
*/
protected $title;

/**
* @param FeedWriter $feed
* @param MatchRepository $matchRepository
*/
function __construct(FeedWriter $feed, MatchRepository $matchRepository)
{
$this->feed = $feed;
$this->matchRepository = $matchRepository;
}

/**
* @return string
*/
public function generate()
{
$this->prepareFeed();
$this->generateMatchEntries();

return $this->feed->export(self::FEED_TYPE, true);
}

/**
* @internal param \Zend\Feed\Writer\Feed $feed
*
* @return FeedWriter
*/
private function prepareFeed()
{
$this->feed->setTitle($this->getTitle());
$this->feed->setLink($this->getUrl());
$this->feed->addAuthor(['name' => 'Fussi']);
$this->feed->setDateModified(time());
}

/**
* @return void
*/
private function generateMatchEntries()
{
foreach ($this->getMatches() as $match) {
$entry = $this->feed->createEntry();
$entry->setTitle($this->makeMatchTitle($match));
$entry->setDescription($this->makeMatchDescription($match));
$entry->setLink($this->makeMatchLink($match));

$entry->setDateCreated($match->getDate());
$entry->setDateModified($match->getDate());

$this->feed->addEntry($entry);
}
}

/**
* @param string $url
*/
public function setUrl($url)
{
$this->url = $url;
}

/**
* @throws UrlNotFoundException
* @return string
*/
public function getUrl()
{
if (is_null($this->url)) {
throw new UrlNotFoundException('Feed-URL must be set');
}

return $this->url;
}

/**
* @param string $title
*/
public function setTitle($title)
{
$this->title = $title;
}

/**
* @return string
*/
public function getTitle()
{
return $this->title;
}

/**
* @return \Application\Model\Entity\Match[]
*/
private function getMatches()
{
$matches = $this->matchRepository->getLastMatches();

return $matches;
}

/**
* @param $match
*
* @return string
*/
private function makeMatchTitle($match)
{
return sprintf('%s vs. %s', $match->getPlayer1()->getName(), $match->getPlayer2()->getName());
}

/**
* @param $match
*
* @return string
*/
private function makeMatchDescription($match)
{
return sprintf('Result: %s', $match->getScore());
}

/**
* @param $match
*
* @return string
*/
private function makeMatchLink($match)
{
return sprintf('%s%s%d', $this->getUrl(), '/match/', $match->getId());
}
}
1 change: 1 addition & 0 deletions module/Application/view/layout/layout.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ echo $this->doctype();
<a href="http://mayflower.de">Mayflower GmbH</a>
for sponsoring Mayday time!
</small></p>
<p>Feeds: <a href="/feed/recent-matches">Recent matches</a></p>
</footer>
</div> <!-- /container -->
<?php echo $this->inlineScript() ?>
Expand Down