Skip to content
This repository has been archived by the owner on May 1, 2019. It is now read-only.

Commit

Permalink
Merge pull request #414 from snapshotpl/feed
Browse files Browse the repository at this point in the history
Add feed mapper
  • Loading branch information
Ocramius committed Feb 27, 2015
2 parents 4dd5fdc + 525573b commit 497251a
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 27 deletions.
36 changes: 13 additions & 23 deletions module/Application/src/Application/Controller/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Zend\Feed\Writer\Feed;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\FeedModel;
use Zend\View\Model\ViewModel;
use ZfModule\Mapper;

class IndexController extends AbstractActionController
Expand All @@ -30,17 +31,20 @@ public function __construct(Mapper\Module $moduleMapper)
$this->moduleMapper = $moduleMapper;
}

/**
* @return ViewModel
*/
public function indexAction()
{
$query = $this->params()->fromQuery('query', null);
$page = (int) $this->params()->fromQuery('page', 1);

$repositories = $this->moduleMapper->pagination($page, self::MODULES_PER_PAGE, $query, 'created_at', 'DESC');

return [
return new ViewModel([
'repositories' => $repositories,
'query' => $query,
];
]);
}

/**
Expand All @@ -49,35 +53,21 @@ public function indexAction()
*/
public function feedAction()
{
$url = $this->plugin('url');
// Prepare the feed
$feed = new Feed();
$feed->setTitle('ZF2 Modules');
$feed->setDescription('Recently added modules.');
$feed->setFeedLink('http://modules.zendframework.com/feed', 'atom');
$feed->setLink('http://modules.zendframework.com');
$feed->setDescription('Recently added ZF2 modules');
$feed->setFeedLink($url->fromRoute('feed', [], ['force_canonical' => true]), 'atom');
$feed->setLink($url->fromRoute('home', [], ['force_canonical' => true]));

// Get the recent modules
$page = 1;

$repositories = $this->moduleMapper->pagination($page, self::MODULES_PER_PAGE, null, 'created_at', 'DESC');
$modules = $this->moduleMapper->pagination($page, self::MODULES_PER_PAGE, null, 'created_at', 'DESC');

// Load them into the feed
foreach ($repositories as $module) {
$entry = $feed->createEntry();
$entry->setTitle($module->getName());

if ($module->getDescription() == '') {
$moduleDescription = "No Description available";
} else {
$moduleDescription = $module->getDescription();
}

$entry->setDescription($moduleDescription);
$entry->setLink($module->getUrl());
$entry->setDateCreated(strtotime($module->getCreatedAt()));

$feed->addEntry($entry);
}
$mapper = new Mapper\ModuleToFeed($feed, $url);
$mapper->addModules($modules);

// Render the feed
$feedmodel = new FeedModel();
Expand Down
2 changes: 1 addition & 1 deletion module/Application/view/application/index/index.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<div class="col-xs-7 col-sm-6">
<p>
<a href="<?php echo $this->url('view-module', ['vendor' => $this->escapeUrl($module->getOwner()), 'module' => $this->escapeUrl($module->getName())]) ?>">
<strong><?php echo $this->escapeHtml($module->getOwner()); ?>/<?php echo $this->escapeHtml($module->getName()); ?></strong>
<strong><?php echo $this->escapeHtml($module->getIdentifier()); ?></strong>
</a>
</p>
<p><span class="zf-green">Created:</span> <?php echo $this->dateFormat($module->getCreatedAtDateTime(), IntlDateFormatter::SHORT, IntlDateFormatter::SHORT); ?></p>
Expand Down
2 changes: 1 addition & 1 deletion module/Application/view/application/search/index.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<div class="media-body">
<h4 class="media-heading">
<a href="<?php echo $this->url('view-module', ['vendor' => $this->escapeUrl($module->getOwner()), 'module' => $this->escapeUrl($module->getName())]); ?>">
<?php echo $this->escapeHtml($module->getOwner()); ?>/<?php echo $this->escapeHtml($module->getName()); ?>
<?php echo $this->escapeHtml($module->getIdentifier()); ?>
</a>
</h4>
<?php echo $this->escapeHtml($module->getDescription()) ?>
Expand Down
15 changes: 15 additions & 0 deletions module/ZfModule/src/ZfModule/Entity/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,19 @@ public function setName($name)
{
$this->name = $name;
}

/**
* @return string|null
*/
public function getIdentifier()
{
$owner = $this->getOwner();
$name = $this->getName();

if (empty($owner) || empty($name)) {
return;
}

return sprintf('%s/%s', $owner, $name);
}
}
80 changes: 80 additions & 0 deletions module/ZfModule/src/ZfModule/Mapper/ModuleToFeed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace ZfModule\Mapper;

use Zend\Feed\Writer\Entry;
use Zend\Feed\Writer\Feed;
use Zend\Mvc\Controller\Plugin\Url as UrlPlugin;
use ZfModule\Entity\Module as ModuleEntity;

/**
* ModuleToFeed
*
* @author Witold Wasiczko <witold@wasiczko.pl>
*/
class ModuleToFeed
{
/**
* @var Feed
*/
protected $feed;

/**
* @var UrlPlugin
*/
protected $urlPlugin;

/**
* @param Feed $feed
*/
public function __construct(Feed $feed, UrlPlugin $urlPlugin)
{
$this->feed = $feed;
$this->urlPlugin = $urlPlugin;
}

/**
* @param array $modules
*/
public function addModules($modules)
{
foreach ($modules as $module) {
$this->addModule($module);
}
}

/**
* @param ModuleEntity $module
* @return Entry
*/
public function addModule(ModuleEntity $module)
{
$moduleDescription = $module->getDescription();

if (empty($moduleDescription)) {
$moduleDescription = 'No description available';
}

$moduleName = $module->getName();
$urlParams = ['vendor' => $module->getOwner(), 'module' => $moduleName];

$entry = $this->feed->createEntry();

$entry->setId($module->getIdentifier());
$entry->setTitle($moduleName);
$entry->setDescription($moduleDescription);
$entry->setLink($this->urlPlugin->fromRoute('view-module', $urlParams, ['force_canonical' => true]));
$entry->addAuthor(['name' => $module->getOwner()]);
$entry->setDateCreated($module->getCreatedAtDateTime());

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

return $entry;
}
}
23 changes: 23 additions & 0 deletions module/ZfModule/test/ZfModuleTest/Entity/ModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function testDefaults()
$this->assertEquals(new DateTime(), $this->module->getCreatedAtDateTime());
$this->assertNull($this->module->getCreatedAt());
$this->assertNull($this->module->getUpdatedAt());
$this->assertNull($this->module->getIdentifier());
}

public function testSetOwner()
Expand Down Expand Up @@ -103,4 +104,26 @@ public function testSetPhotoUrl()

$this->assertSame($photoUrl, $this->module->getPhotoUrl());
}

public function testGetIdentifier()
{
$this->module->setOwner('owner');
$this->module->setName('name');

$this->assertEquals('owner/name', $this->module->getIdentifier());
}

public function testGetNullIdentifierOnlyOwner()
{
$this->module->setOwner('owner');

$this->assertNull($this->module->getIdentifier());
}

public function testGetNullIdentifierOnlyName()
{
$this->module->setName('name');

$this->assertNull($this->module->getIdentifier());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ApplicationTest\Integration\Util\Bootstrap;
use PHPUnit_Framework_TestCase;
use Zend\Db;
use Zend\Db\Adapter\Exception\RuntimeException as DbRuntimeException;
use ZfModule\Entity;
use ZfModule\Mapper;

Expand All @@ -29,10 +30,17 @@ protected function setUp()

$this->mapper = $serviceManager->get(Mapper\Module::class);

/* @var Db\Adapter\Adapter $database */
/* @var $database Db\Adapter\Adapter */
$database = $serviceManager->get('zfcuser_zend_db_adapter');

$this->connection = $database->getDriver()->getConnection();

try {
$this->connection->connect();
} catch (DbRuntimeException $e) {
$this->markTestSkipped($e->getMessage());
}

$this->connection->beginTransaction();
}

Expand Down
69 changes: 69 additions & 0 deletions module/ZfModule/test/ZfModuleTest/Mapper/ModuleToFeedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace ZfModuleTest\Mapper;

class ModuleToFeedTest extends \PHPUnit_Framework_TestCase
{
public function testAddModule()
{
$dateTime = new \DateTime();

$module = $this->getMockBuilder(\ZfModule\Entity\Module::class)->getMock();
$module->expects($this->once())->method('getName')->willReturn('name');
$module->expects($this->once())->method('getDescription')->willReturn('description');
$module->expects($this->once())->method('getCreatedAtDateTime')->willReturn($dateTime);

$entry = $this->getMockBuilder(\Zend\Feed\Writer\Entry::class)->disableOriginalConstructor()->getMock();
$entry->expects($this->once())->method('setTitle')->with('name');
$entry->expects($this->once())->method('setDescription')->with('description');
$entry->expects($this->once())->method('setLink')->with('url');
$entry->expects($this->once())->method('setDateCreated')->with($dateTime);

$url = $this->getMockBuilder(\Zend\Mvc\Controller\Plugin\Url::class)->getMock();
$url->expects($this->once())->method('fromRoute')->willReturn('url');

$feed = $this->getMockBuilder(\Zend\Feed\Writer\Feed::class)->disableOriginalConstructor()->getMock();
$feed->expects($this->once())->method('createEntry')->willReturn($entry);
$feed->expects($this->once())->method('addEntry')->with($entry);

$moduleToFeed = new \ZfModule\Mapper\ModuleToFeed($feed, $url);
$moduleToFeed->addModule($module);
}

public function testAddModuleWithoutDesctription()
{
$dateTime = new \DateTime();

$module = $this->getMockBuilder(\ZfModule\Entity\Module::class)->getMock();
$module->expects($this->once())->method('getName')->willReturn('name');
$module->expects($this->once())->method('getDescription')->willReturn(null);
$module->expects($this->once())->method('getCreatedAtDateTime')->willReturn($dateTime);

$entry = $this->getMockBuilder(\Zend\Feed\Writer\Entry::class)->disableOriginalConstructor()->getMock();
$entry->expects($this->once())->method('setTitle')->with('name');
$entry->expects($this->once())->method('setDescription')->with('No description available');
$entry->expects($this->once())->method('setLink')->with('url');
$entry->expects($this->once())->method('setDateCreated')->with($dateTime);

$url = $this->getMockBuilder(\Zend\Mvc\Controller\Plugin\Url::class)->getMock();
$url->expects($this->once())->method('fromRoute')->willReturn('url');

$feed = $this->getMockBuilder(\Zend\Feed\Writer\Feed::class)->disableOriginalConstructor()->getMock();
$feed->expects($this->once())->method('createEntry')->willReturn($entry);
$feed->expects($this->once())->method('addEntry')->with($entry);

$moduleToFeed = new \ZfModule\Mapper\ModuleToFeed($feed, $url);
$moduleToFeed->addModule($module);
}

public function testAddMultipleModules()
{
$modulesCount = 11;
$module = $this->getMockBuilder(\ZfModule\Entity\Module::class)->getMock();
$modules = array_fill(0, $modulesCount, $module);

$moduleToFeed = $this->getMockBuilder(\ZfModule\Mapper\ModuleToFeed::class)->setMethods(['addModule'])->disableOriginalConstructor()->getMock();
$moduleToFeed->expects($this->exactly($modulesCount))->method('addModule')->with($module);
$moduleToFeed->addModules($modules);
}
}
2 changes: 1 addition & 1 deletion module/ZfModule/view/zf-module/helper/new-module.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</div>
<div class="col-md-9">
<a href="<?php echo $this->url('view-module', ['vendor' => $this->escapeUrl($module->getOwner()), 'module' => $this->escapeUrl($module->getName())]) ?>">
<?php echo $this->escapeHtml($module->getOwner()); ?>/<?php echo $this->escapeHtml($module->getName()); ?>
<?php echo $this->escapeHtml($module->getIdentifier()); ?>
</a>
</div>
</div>
Expand Down

0 comments on commit 497251a

Please sign in to comment.