-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mparaiso
committed
Mar 23, 2013
1 parent
65b959b
commit dcb762f
Showing
10 changed files
with
265 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace Controller; | ||
|
||
use Silex\ControllerProviderInterface; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Silex\Application; | ||
|
||
class RssController implements ControllerProviderInterface | ||
{ | ||
/** | ||
* FR : génère un flux rss à partir de la BDD | ||
* @param \Symfony\Component\HttpFoundation\Request $req | ||
* @param $title | ||
* @param \Silex\Application $app | ||
* @return mixed | ||
*/ | ||
function index(Request $req, $title,Application $app) | ||
{ | ||
$content = $app["rss_service"]->generate($title); | ||
return $content; | ||
} | ||
|
||
/** | ||
* Returns routes to connect to the given application. | ||
* | ||
* @param Application $app An Application instance | ||
* | ||
* @return ControllerCollection A ControllerCollection instance | ||
*/ | ||
public function connect(Application $app) | ||
{ | ||
/* @var $controllers \Silex\ControllerCollection */ | ||
$controllers = $app["controllers_factory"]; | ||
$controllers->match("/rss/{title}", array($this, "index")) | ||
->value("title", "") | ||
->bind("rss") | ||
->after(function(Request $req,Response $res,Application $app){ | ||
$res->headers->add( | ||
array("Content-type"=>"application/rss+xml")); | ||
return $res; | ||
}); | ||
|
||
|
||
|
||
return $controllers; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace Helper; | ||
|
||
use Mparaiso\Rss\Adapter\IItemAdapter; | ||
use Symfony\Component\Routing\Generator\UrlGenerator; | ||
use Exception; | ||
|
||
class SnippetAdapter implements IItemAdapter | ||
{ | ||
|
||
protected $generator; | ||
|
||
function __construct(UrlGenerator $generator) | ||
{ | ||
$this->generator = $generator; | ||
} | ||
|
||
/** | ||
* convert a model to an associative array | ||
* @param $data | ||
* @return mixed | ||
*/ | ||
function toChannel($data) | ||
{ | ||
if ($data instanceof \Entity\Category) { | ||
$channel["title"] = $data->getTitle(); | ||
$channel["description"] = $data->getDescription(); | ||
$channel["language"] = "en-en"; | ||
$channel["link"] = $this->generator->generate("home", array("category" => urlencode($data->getTitle())), TRUE); | ||
$channel["guid"] = $channel["link"]; | ||
$channel["language"] = "en-en"; | ||
foreach ($data->getSnippets() as $snippet) { | ||
/* @var $snippet \Entity\Snippet */ | ||
$channel["items"][] = array( | ||
"title" => $snippet->getTitle(), | ||
"description" => $snippet->getDescription(), | ||
"pubDate" => $snippet->getCreatedAt(), | ||
"guid" => $this->generator->generate("snippet", array('title' => urlencode($snippet->getTitle())), TRUE), | ||
); | ||
} | ||
/* trie les snippet dans l'ordre de date décroissant */ | ||
uasort($channel['items'], function ($a, $b) { | ||
if ($a['pubDate'] == $b['pubDate']) return 0; | ||
return $a['pubDate'] < $b['pubDate'] ? 1 : -1; | ||
}); | ||
} elseif (is_array($data)) { | ||
$channel["title"] = "Snippet Manager"; | ||
$channel["description"] = "Manage your snippets online"; | ||
$channel["language"] = "en-en"; | ||
$channel['link'] = $this->generator->generate("home", array(), TRUE); | ||
$channel['guid'] = $channel['link']; | ||
foreach ($data as $snippet) { | ||
/* @var $snippet \Entity\Snippet */ | ||
$channel["items"][] = array( | ||
"title" => $snippet->getTitle(), | ||
"description" => $snippet->getDescription(), | ||
"pubDate" => $snippet->getCreatedAt(), | ||
"guid" => $this->generator->generate("snippet", array('title' => urlencode($snippet->getTitle())), TRUE), | ||
|
||
); | ||
} | ||
} else { | ||
throw new Exception('Cant parse data, must be a \Entity\Category or an array of snippets'); | ||
} | ||
return $channel; | ||
} | ||
|
||
/** | ||
* convert channel back to a model | ||
* @param $channel | ||
* @return mixed | ||
*/ | ||
function toModel($channel) | ||
{ | ||
// TODO: Implement toModel() method. | ||
} | ||
|
||
public function getGenerator() | ||
{ | ||
return $this->generator; | ||
} | ||
|
||
public function setGenerator($generator) | ||
{ | ||
$this->generator = $generator; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
|
||
namespace Service; | ||
|
||
use Mparaiso\Rss\SimpleRss; | ||
use Helper\SnippetAdapter; | ||
|
||
class RssService | ||
{ | ||
|
||
protected $rssGenerator; | ||
|
||
protected $categoryService; | ||
|
||
protected $snippetService; | ||
|
||
function __construct(SnippetService $snippetService, CategoryService $categoryService, SimpleRss $rssGenerator) | ||
{ | ||
$this->rssGenerator = $rssGenerator; | ||
$this->categoryService = $categoryService; | ||
$this->snippetService = $snippetService; | ||
} | ||
|
||
/** | ||
* FR : génère un flux RSS à partir d'une categories | ||
* EN : generate a rss feed according to a category | ||
* @param string $channel | ||
*/ | ||
function generate($channel = NULL) | ||
{ | ||
if ($channel != NULL) { | ||
$data = $this->categoryService | ||
->findOneBy(array("title" => $channel)); | ||
} else { | ||
$data = $this->snippetService | ||
->findAll(array(),array("created_at"=>"DESC")); | ||
} | ||
$this->rssGenerator->setChannel($data); | ||
return $this->rssGenerator->generate(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version='1.0' encoding='utf-8' ?> | ||
<phpunit | ||
bootstrap="./tests/bootstrap.php" | ||
> | ||
<testsuites> | ||
<testsuite name="Snippet Manager Test Suit"> | ||
<directory>./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Service; | ||
|
||
use PHPUnit_Framework_TestCase; | ||
|
||
/** | ||
* integration test | ||
*/ | ||
class RssServiceTest extends PHPUnit_Framework_TestCase | ||
{ | ||
protected $app; | ||
/** | ||
* @var \Service\RssService | ||
*/ | ||
protected $rssService; | ||
public function setUp() | ||
{ | ||
$this->app = getApp(); | ||
$this->app->boot(); | ||
$this->rssService = $this->app["rss_service"]; | ||
} | ||
|
||
public function testConstruct() | ||
{ | ||
$this->assertNotNull($this->app["rss_service"]); | ||
$this->assertInstanceOf('\Service\RssService', $this->app["rss_service"]); | ||
} | ||
|
||
public function testGenerate() | ||
{ | ||
$category = "PHP"; | ||
$feed = $this->rssService->generate($category); | ||
$this->assertInternalType("string",$feed); | ||
print($feed); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
use Silex\Application; | ||
|
||
/** | ||
* Bootstrap file for phpunit | ||
*/ | ||
$autoload = require __DIR__ . "/../vendor/autoload.php"; | ||
$autoload->add("",__DIR__."/../app"); | ||
/** | ||
* @return Silex\Application | ||
*/ | ||
function getApp() | ||
{ | ||
return new SnippetManager(); | ||
} | ||
|
||
|