From 2489cc6ffef619e4dd2c56a8d906c7d07d00633f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Burnichon?= Date: Wed, 27 May 2015 12:41:09 +0200 Subject: [PATCH] Rework FeedAggregate. Issue with embedded entry collection. PHRAS-542 --- .../Phrasea/Controller/Api/V1Controller.php | 2 +- lib/Alchemy/Phrasea/Feed/Aggregate.php | 10 +- .../Phrasea/Feed/AggregateEntryCollection.php | 319 ++++++++++++++++++ lib/Alchemy/Phrasea/Feed/FeedInterface.php | 5 +- lib/Alchemy/Phrasea/Model/Entities/Feed.php | 10 +- .../client/home_inter_pub_basket.html.twig | 2 +- templates/web/prod/results/feeds.html.twig | 2 +- .../Phrasea/Controller/Root/RSSFeedTest.php | 16 +- 8 files changed, 336 insertions(+), 30 deletions(-) create mode 100644 lib/Alchemy/Phrasea/Feed/AggregateEntryCollection.php diff --git a/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php b/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php index 14b0300d81..95faa2996e 100644 --- a/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php +++ b/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php @@ -1836,7 +1836,7 @@ class V1Controller extends Controller { return array_map(function ($entry) use ($request) { return $this->listPublicationEntry($request, $entry); - }, iterator_to_array($feed->getEntries($offset_start, $how_many))); + }, $feed->getEntries()->slice($offset_start, $how_many)); } /** diff --git a/lib/Alchemy/Phrasea/Feed/Aggregate.php b/lib/Alchemy/Phrasea/Feed/Aggregate.php index c58f3de5bf..cc68b94277 100644 --- a/lib/Alchemy/Phrasea/Feed/Aggregate.php +++ b/lib/Alchemy/Phrasea/Feed/Aggregate.php @@ -109,17 +109,11 @@ class Aggregate implements FeedInterface /** * {@inheritdoc} */ - public function getEntries($offset_start = null, $how_many = null) + public function getEntries() { - if ($this->feeds->isEmpty()) { - return new ArrayCollection(); - } - - $feedIds = $this->feeds->getKeys(); - /** @var FeedEntryRepository $feedEntryRepository */ $feedEntryRepository = $this->em->getRepository('Phraseanet:FeedEntry'); - return new ArrayCollection($feedEntryRepository->findByFeeds($feedIds, $offset_start, $how_many)); + return new AggregateEntryCollection($feedEntryRepository, $this->feeds); } /** diff --git a/lib/Alchemy/Phrasea/Feed/AggregateEntryCollection.php b/lib/Alchemy/Phrasea/Feed/AggregateEntryCollection.php new file mode 100644 index 0000000000..600d4fd1af --- /dev/null +++ b/lib/Alchemy/Phrasea/Feed/AggregateEntryCollection.php @@ -0,0 +1,319 @@ +repository = $repository; + if ($feeds instanceof \Traversable) { + $feeds = iterator_to_array($feeds); + } + $this->feeds = $feeds; + } + + public function slice($offset, $length = null) + { + return $this->repository->findByFeeds($this->feeds, $offset, $length); + } + + private function __load___() + { + $this->entries = new ArrayCollection($this->repository->findByFeeds($this->feeds)); + } + + public function count() + { + if (null === $this->entries) { + return $this->repository->countByFeeds($this->feeds); + } + + return $this->entries->count(); + } + + public function toArray() + { + if (null === $this->entries) { + $this->__load___(); + } + + return $this->entries->toArray(); + } + + public function first() + { + if (null === $this->entries) { + $this->__load___(); + } + + return $this->entries->first(); + } + + public function last() + { + if (null === $this->entries) { + $this->__load___(); + } + + return $this->entries->last(); + } + + public function key() + { + if (null === $this->entries) { + $this->__load___(); + } + + return $this->entries->key(); + } + + public function next() + { + if (null === $this->entries) { + $this->__load___(); + } + + return $this->entries->next(); + } + + public function current() + { + if (null === $this->entries) { + $this->__load___(); + } + + return $this->entries->current(); + } + + public function remove($key) + { + if (null === $this->entries) { + $this->__load___(); + } + + return $this->entries->remove($key); + } + + public function removeElement($element) + { + if (null === $this->entries) { + $this->__load___(); + } + + return $this->entries->removeElement($element); + } + + public function offsetExists($offset) + { + if (null === $this->entries) { + $this->__load___(); + } + + return $this->entries->offsetExists($offset); + } + + public function offsetGet($offset) + { + if (null === $this->entries) { + $this->__load___(); + } + + return $this->entries->offsetGet($offset); + } + + public function offsetSet($offset, $value) + { + if (null === $this->entries) { + $this->__load___(); + } + + return $this->entries->offsetSet($offset, $value); + } + + public function offsetUnset($offset) + { + if (null === $this->entries) { + $this->__load___(); + } + + return $this->entries->offsetUnset($offset); + } + + public function containsKey($key) + { + if (null === $this->entries) { + $this->__load___(); + } + + return $this->entries->containsKey($key); + } + + public function contains($element) + { + if (null === $this->entries) { + $this->__load___(); + } + + return $this->entries->contains($element); + } + + public function exists(Closure $p) + { + if (null === $this->entries) { + $this->__load___(); + } + + return $this->entries->exists($p); + } + + public function indexOf($element) + { + if (null === $this->entries) { + $this->__load___(); + } + + return $this->entries->indexOf($element); + } + + public function get($key) + { + if (null === $this->entries) { + $this->__load___(); + } + + return $this->entries->get($key); + } + + public function getKeys() + { + if (null === $this->entries) { + $this->__load___(); + } + + return $this->entries->getKeys(); + } + public function getValues() + { + if (null === $this->entries) { + $this->__load___(); + } + + return $this->entries->getValues(); + } + + public function set($key, $value) + { + if (null === $this->entries) { + $this->__load___(); + } + + $this->entries->set($key, $value); + } + + public function add($value) + { + if (null === $this->entries) { + $this->__load___(); + } + + return $this->entries->add($value); + } + + public function isEmpty() + { + if (null === $this->entries) { + $this->__load___(); + } + + return $this->entries->isEmpty(); + } + + public function getIterator() + { + if (null === $this->entries) { + $this->__load___(); + } + + return $this->entries->getIterator(); + } + + public function map(Closure $func) + { + if (null === $this->entries) { + $this->__load___(); + } + + return $this->entries->map($func); + } + + public function filter(Closure $p) + { + if (null === $this->entries) { + $this->__load___(); + } + + return $this->entries->filter($p); + } + + public function forAll(Closure $p) + { + if (null === $this->entries) { + $this->__load___(); + } + + return $this->entries->forAll($p); + } + + public function partition(Closure $p) + { + if (null === $this->entries) { + $this->__load___(); + } + + return $this->entries->partition($p); + } + + public function clear() + { + if (null === $this->entries) { + $this->__load___(); + } + + $this->entries->clear(); + } + + public function matching(Criteria $criteria) + { + if (null === $this->entries) { + $this->__load___(); + } + + return $this->entries->matching($criteria); + } +} diff --git a/lib/Alchemy/Phrasea/Feed/FeedInterface.php b/lib/Alchemy/Phrasea/Feed/FeedInterface.php index d2e4d56801..16175d4d12 100644 --- a/lib/Alchemy/Phrasea/Feed/FeedInterface.php +++ b/lib/Alchemy/Phrasea/Feed/FeedInterface.php @@ -33,12 +33,9 @@ interface FeedInterface /** * Returns a collection of FeedEntry. * - * @param integer $offset_start - * @param integer $how_many - * * @return FeedEntry[]|Collection */ - public function getEntries($offset_start = 0, $how_many = null); + public function getEntries(); /** * Returns an UTF-8 subtitle for the feed. diff --git a/lib/Alchemy/Phrasea/Model/Entities/Feed.php b/lib/Alchemy/Phrasea/Model/Entities/Feed.php index 3bc15dfdea..e3cdd9a62c 100644 --- a/lib/Alchemy/Phrasea/Model/Entities/Feed.php +++ b/lib/Alchemy/Phrasea/Model/Entities/Feed.php @@ -254,17 +254,11 @@ class Feed implements FeedInterface /** * Get entries * - * @param int $offset_start - * @param int $how_many * @return FeedEntry[]|Collection */ - public function getEntries($offset_start = 0, $how_many = null) + public function getEntries() { - if (null === $how_many) { - return $this->entries; - } - - return $this->entries->slice($offset_start, $how_many); + return $this->entries; } /** diff --git a/templates/web/client/home_inter_pub_basket.html.twig b/templates/web/client/home_inter_pub_basket.html.twig index f2f32a384e..ac57eabc34 100644 --- a/templates/web/client/home_inter_pub_basket.html.twig +++ b/templates/web/client/home_inter_pub_basket.html.twig @@ -5,7 +5,7 @@
- {% for entry in feeds.getEntries(0, 5) %} + {% for entry in feeds.entries.slice(0, 5) %}

diff --git a/templates/web/prod/results/feeds.html.twig b/templates/web/prod/results/feeds.html.twig index ba5a3138ae..25a99726eb 100644 --- a/templates/web/prod/results/feeds.html.twig +++ b/templates/web/prod/results/feeds.html.twig @@ -58,7 +58,7 @@ {% set doctype_display = user_setting('doctype_display') %} {% set offset_start = ((page - 1) * 5 )%} - {% set entries = feed.getEntries(offset_start,5) %} + {% set entries = feed.entries.slice(offset_start,5) %} {% for entry in entries %} {% block content_item %} diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Root/RSSFeedTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Root/RSSFeedTest.php index 8ed18c3f2a..c3a5ab3e96 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Root/RSSFeedTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Root/RSSFeedTest.php @@ -272,14 +272,16 @@ class RSSFeedTest extends \PhraseanetWebTestCase $count = 0; $offset_start = 0; $n_entries = 20; - $entries = $feed->getEntries($offset_start, $n_entries); + $entries = $feed->getEntries()->slice($offset_start, $n_entries); foreach ($list_entries as $node) { if (sizeof($entries) == 0) { - $offset_start = ($offset_start++) * $n_entries; - $entries = $feed->getEntries($offset_start, $n_entries); - if (sizeof($entries) == 0) //no more + $offset_start += $n_entries; + $entries = $feed->getEntries()->slice($offset_start, $n_entries); + if (sizeof($entries) == 0) { + //no more break; + } } $feed_entry = array_shift($entries); switch ($node->nodeName) { @@ -567,12 +569,12 @@ class RSSFeedTest extends \PhraseanetWebTestCase $count = 0; $offset_start = 0; $n_entries = 20; - $entries = $feed->getEntries($offset_start, $n_entries); + $entries = $feed->getEntries()->slice($offset_start, $n_entries); foreach ($entries_item as $entry) { if (sizeof($entries) == 0) { - $offset_start = ($offset_start++) * $n_entries; - $entries = $feed->getEntries($offset_start, $n_entries); + $offset_start += $n_entries; + $entries = $feed->getEntries()->slice($offset_start, $n_entries); if (sizeof($entries) == 0) //no more break; }