From 8cbdc3f14aa5c20496739adc77e43a352f504366 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADnez=20Gadea?= Date: Thu, 21 Jul 2022 11:15:53 +0200 Subject: [PATCH 1/4] Refactor IndexController so it's coupled only to a use case --- Resources/templates/responsive/home/index.php | 13 +-- .../responsive/home/partials/channels.php | 38 +++---- .../responsive/home/partials/projects.php | 6 +- .../home/partials/projects_list.php | 6 +- .../responsive/home/partials/sponsors.php | 6 +- src/Goteo/Controller/DiscoverController.php | 82 +------------- src/Goteo/Controller/IndexController.php | 63 ++--------- src/Goteo/Model/Project/ProjectFilters.php | 100 ++++++++++++++++++ 8 files changed, 147 insertions(+), 167 deletions(-) create mode 100644 src/Goteo/Model/Project/ProjectFilters.php diff --git a/Resources/templates/responsive/home/index.php b/Resources/templates/responsive/home/index.php index 3074cafd6c..57d8b1729a 100644 --- a/Resources/templates/responsive/home/index.php +++ b/Resources/templates/responsive/home/index.php @@ -19,27 +19,25 @@ text('home-menu-toggle-label') ?> replace() ?> - - section('home-content') ?> insert('partials/components/main_slider', [ - 'banners' => $this->banners, + 'banners' => $this->response->getBanners(), 'nav' => 'home/partials/main_slider_nav', 'button_text' => $this->text('invest-more-info') ]) ?> insert('home/partials/projects') ?> - home['values']): ?> + response->getHomeItems()['values']): ?> insert('partials/components/values', [ 'title' => $this->t('home-footprint-values-title'), - 'footprints' => $this->footprints, - 'sdg_by_footprint' => $this->sdg_by_footprint, - 'projects_by_footprint' => $this->projects_by_footprint + 'footprints' => $this->response->getFootprints(), + 'sdg_by_footprint' => $this->response->getSdgByFootprint(), + 'projects_by_footprint' => $this->response->getProjectsByFootprint() ]) ?> @@ -61,4 +59,3 @@ insert('home/partials/modals') ?> replace() ?> - diff --git a/Resources/templates/responsive/home/partials/channels.php b/Resources/templates/responsive/home/partials/channels.php index 28c23a4421..e6f594f169 100644 --- a/Resources/templates/responsive/home/partials/channels.php +++ b/Resources/templates/responsive/home/partials/channels.php @@ -1,38 +1,34 @@ -channels && count($this->channels) > 1): ?> +response->getChannels() && count($this->response->getChannels()) > 1): ?>
matchfunding
-

- text('home-channels-title') ?> -

+

text('home-channels-title') ?>

- channels as $channel): ?> + response->getChannels() as $channel): ?> getSummary(); ?> owner_background; ?>
-
-
-
- - <?= $channel->name ?> - -
+
+
+ -
- -
- text_truncate($channel->description, 120) ?> -
+
+
+ +
text_truncate($channel->description, 120) ?>
+
diff --git a/Resources/templates/responsive/home/partials/projects.php b/Resources/templates/responsive/home/partials/projects.php index 4dfa4d67ae..532265b825 100644 --- a/Resources/templates/responsive/home/partials/projects.php +++ b/Resources/templates/responsive/home/partials/projects.php @@ -8,10 +8,10 @@
- projects): ?> + response->getProjects()): ?> insert('home/partials/projects_list', [ - 'projects' => $this->projects, - 'total_projects' => $this->total_projects + 'projects' => $this->response->getProjects(), + 'total_projects' => $this->response->getTotalProjects() ]) ?>
diff --git a/Resources/templates/responsive/home/partials/projects_list.php b/Resources/templates/responsive/home/partials/projects_list.php index 9c4d96e186..c779914571 100644 --- a/Resources/templates/responsive/home/partials/projects_list.php +++ b/Resources/templates/responsive/home/partials/projects_list.php @@ -1,5 +1,7 @@ -
- projects as $project) : ?> +
+ response->getProjects() as $project) : ?>
insert('project/widgets/normal', [ 'project' => $project diff --git a/Resources/templates/responsive/home/partials/sponsors.php b/Resources/templates/responsive/home/partials/sponsors.php index bf7b7bcc32..b5d4b535ec 100644 --- a/Resources/templates/responsive/home/partials/sponsors.php +++ b/Resources/templates/responsive/home/partials/sponsors.php @@ -1,7 +1,7 @@
- sponsors): ?> + response->getSponsors()): ?>
- sponsors as $type => $sponsors): ?> + response->getSponsors() as $type => $sponsors): ?>
-
\ No newline at end of file +
diff --git a/src/Goteo/Controller/DiscoverController.php b/src/Goteo/Controller/DiscoverController.php index e0789b3058..d275f09cfe 100644 --- a/src/Goteo/Controller/DiscoverController.php +++ b/src/Goteo/Controller/DiscoverController.php @@ -21,85 +21,13 @@ class DiscoverController extends Controller { + private Project\ProjectFilters $projectFilters; + public function __construct() { $this->dbReplica(true); $this->dbCache(true); - View::setTheme('responsive'); - } - - /** - * Returns an array suitable for Project::getList($filters) - */ - protected function getProjectFilters($filter, $vars = []): array - { - $filters = [ - 'status' => [Project::STATUS_IN_CAMPAIGN, Project::STATUS_FUNDED, Project::STATUS_FULFILLED], - 'published_since' => (new DateTime('-6 month'))->format('Y-m-d') - ]; - - $filters['order'] = 'project.status ASC, project.published DESC, project.name ASC'; - if($vars['q']) { - $filters['global'] = $vars['q']; - unset($filters['published_since']); - $filters['status'] = [Project::STATUS_IN_CAMPAIGN, Project::STATUS_FUNDED, Project::STATUS_FULFILLED, Project::STATUS_UNFUNDED]; - } - - if ($vars['category']) { - $filters['category'] = $vars['category']; - unset($filters['published_since']); - $filters['status'] = [ Project::STATUS_IN_CAMPAIGN, Project::STATUS_FUNDED, Project::STATUS_FULFILLED, Project::STATUS_UNFUNDED ]; - } - - if ($vars['location'] || ($vars['latitude'] && $vars['longitude'])) { - unset($filters['published_since']); - $filters['location'] = new ProjectLocation(['location' => $vars['location'], 'latitude' => $vars['latitude'], 'longitude' => $vars['longitude'], 'radius' => 300]); - $filters['status'] = [Project::STATUS_IN_CAMPAIGN, Project::STATUS_FUNDED, Project::STATUS_FULFILLED, Project::STATUS_UNFUNDED]; - $filters['order'] = 'Distance ASC, project.status ASC, project.published DESC, project.name ASC'; - } - - if ($filter === 'near') { - // Nearby defined as 300Km distance - // Any LocationInterface will do (UserLocation, ProjectLocation, ...) - $filters['location'] = new ProjectLocation([ 'latitude' => $vars['latitude'], 'longitude' => $vars['longitude'], 'radius' => 300 ]); - $filters['order'] = 'Distance ASC, project.status ASC, project.published DESC, project.name ASC'; - } elseif($filter === 'outdated') { - $filters['type'] = 'outdated'; - $filters['status'] = Project::STATUS_IN_CAMPAIGN; - $filters['order'] = 'project.days ASC, project.published DESC, project.name ASC'; - } elseif($filter === 'promoted') { - $filters['type'] = 'promoted'; - $filters['status'] = Project::STATUS_IN_CAMPAIGN; - $filters['order'] = 'promote.order ASC, project.published DESC, project.name ASC'; - } elseif($filter === 'popular') { - $filters['type'] = 'popular'; - $filters['status'] = Project::STATUS_IN_CAMPAIGN; - $filters['order'] = 'project.popularity DESC, project.published DESC, project.name ASC'; - } elseif($filter === 'succeeded') { - $filters['type'] = 'succeeded'; - $filters['status'] = [Project::STATUS_FUNDED, Project::STATUS_FULFILLED]; - $filters['order'] = 'project.published DESC, project.name ASC'; - unset($filters['published_since']); - } elseif($filter === 'fulfilled') { - $filters['status'] = [Project::STATUS_FULFILLED]; - $filters['order'] = 'project.published DESC, project.name ASC'; - unset($filters['published_since']); - } elseif($filter === 'archived') { - $filters['status'] = [Project::STATUS_UNFUNDED]; - $filters['order'] = 'project.published DESC, project.name ASC'; - $filters['published_since'] = (new DateTime('-24 month'))->format('Y-m-d'); - } elseif($filter === 'matchfunding') { - $filters['type'] = 'matchfunding'; - unset($filters['published_since']); - } elseif($filter === 'recent') { - $filters['type'] = 'recent'; - } - - if($vars['review']) { - $filters['status'] = [ Project::STATUS_EDITING, Project::STATUS_REVIEWING, Project::STATUS_IN_CAMPAIGN, Project::STATUS_FUNDED, Project::STATUS_FULFILLED, Project::STATUS_UNFUNDED ]; - $filters['is_draft'] = true; - } - return $filters; + $this->projectFilters = new Project\ProjectFilters(); } /* @@ -127,7 +55,7 @@ public function searchAction (Request $request, $filter = '') { $vars['review'] = $request->query->get('review') === '1' ? 1 : 0 ; } - $filters = $this->getProjectFilters($filter, $vars); + $filters = $this->projectFilters->getFilters($filter, $vars); $projects = Project::getList($filters, null, 0, $limit); $total = Project::getList($filters, null, 0, 0, true); @@ -165,7 +93,7 @@ public function ajaxSearchAction(Request $request) { 'status' => [Project::STATUS_IN_CAMPAIGN, Project::STATUS_FUNDED], 'published_since' => (new DateTime('-6 month'))->format('Y-m-d') ]; - $filters = $this->getProjectFilters($filter, $vars); + $filters = $this->projectFilters->getFilters($filter, $vars); $offset = $pag * $limit; $total_projects = 0; diff --git a/src/Goteo/Controller/IndexController.php b/src/Goteo/Controller/IndexController.php index 04be29969b..14c192a42e 100644 --- a/src/Goteo/Controller/IndexController.php +++ b/src/Goteo/Controller/IndexController.php @@ -11,75 +11,32 @@ namespace Goteo\Controller; use Goteo\Application\View; -use Goteo\Application\Config; -use Goteo\Model\Banner; -use Goteo\Model\Footprint; -use Goteo\Model\Home; use Goteo\Model\Project; -use Goteo\Model\Sdg; -use Goteo\Model\Stories; -use Goteo\Model\Node; -use Goteo\Model\Sponsor; -use Goteo\Util\Stats\Stats; +use Goteo\UseCase\Home\HomeUseCase; use Symfony\Component\HttpFoundation\Response; -class IndexController extends DiscoverController +class IndexController extends BaseSymfonyController { + private Project\ProjectFilters $projectFilters; public function __construct() { + parent::__construct(); $this->dbReplica(true); $this->dbCache(true); View::setTheme('responsive'); + + $this->projectFilters = new Project\ProjectFilters(); } public function indexAction(): Response { - $limit = 24; - $filters = $this->getProjectFilters('promoted'); - $projects = Project::getList($filters, null, 0, $limit); - $total_projects = Project::getList($filters, null, 0, 0, true); - $stories = Stories::getList(['active' => true]); - $channels = Node::getAll(['status' => 'active', 'type' => 'channel']); - $banners = Banner::getAll(true); - $stats = Stats::create('home_stats'); - $sponsors = $this->getSponsors(); - $footprints = Footprint::getList(); - $home = Home::getAll(Config::get('node'), 'index'); - - $projects_by_footprint = []; - $sdg_by_footprint = []; - foreach($footprints as $footprint) { - $footprintImpactData[$footprint->id] = $footprint->getAllImpactData(); - $projects_by_footprint[$footprint->id] = Project::getByFootprint(['footprints' => $footprint->id, 'rand' => true, 'amount_bigger_than' => 7000]); - $sdg_by_footprint[$footprint->id] = Sdg::getList(['footprint' => $footprint->id]); - } + $useCase = new HomeUseCase(); + $response = $useCase->execute(); - return $this->viewResponse('home/index', [ - 'banners' => $banners, - 'projects' => $projects, - 'total_projects' => $total_projects, - 'limit' => $limit, - 'limit_add' => 12, // Limit for javascript on addSlick - 'stories' => $stories, - 'channels' => $channels, - 'stats' => $stats, - 'sponsors' => $sponsors, - 'footprints' => $footprints, - 'home' => $home, - 'projects_by_footprint' => $projects_by_footprint, - 'sdg_by_footprint' => $sdg_by_footprint, - 'footprint_impact_data' => $footprintImpactData, + return $this->renderFoilTemplate('home/index', [ + 'response' => $response, ]); } - - private function getSponsors(): array - { - $sponsors = []; - foreach(Sponsor::getTypes() as $type) { - $sponsors[$type] = Sponsor::getList(['type' => $type]); - } - return $sponsors; - } } diff --git a/src/Goteo/Model/Project/ProjectFilters.php b/src/Goteo/Model/Project/ProjectFilters.php new file mode 100644 index 0000000000..795ec03ece --- /dev/null +++ b/src/Goteo/Model/Project/ProjectFilters.php @@ -0,0 +1,100 @@ + [Project::STATUS_IN_CAMPAIGN, Project::STATUS_FUNDED, Project::STATUS_FULFILLED], + 'published_since' => (new DateTime('-6 month'))->format('Y-m-d') + ]; + + $filters['order'] = 'project.status ASC, project.published DESC, project.name ASC'; + if ($vars['q']) { + $filters['global'] = $vars['q']; + unset($filters['published_since']); + $filters['status'] = [Project::STATUS_IN_CAMPAIGN, Project::STATUS_FUNDED, Project::STATUS_FULFILLED, Project::STATUS_UNFUNDED]; + } + + if ($vars['category']) { + $filters['category'] = $vars['category']; + unset($filters['published_since']); + $filters['status'] = [ Project::STATUS_IN_CAMPAIGN, Project::STATUS_FUNDED, Project::STATUS_FULFILLED, Project::STATUS_UNFUNDED ]; + } + + if ($vars['location'] || ($vars['latitude'] && $vars['longitude'])) { + unset($filters['published_since']); + $filters['location'] = new ProjectLocation([ + 'location' => $vars['location'], + 'latitude' => $vars['latitude'], + 'longitude' => $vars['longitude'], + 'radius' => 300 + ]); + $filters['status'] = [Project::STATUS_IN_CAMPAIGN, Project::STATUS_FUNDED, Project::STATUS_FULFILLED, Project::STATUS_UNFUNDED]; + $filters['order'] = 'Distance ASC, project.status ASC, project.published DESC, project.name ASC'; + } + + if ($filter === 'near') { + // Nearby defined as 300Km distance + // Any LocationInterface will do (UserLocation, ProjectLocation, ...) + $filters['location'] = new ProjectLocation([ + 'latitude' => $vars['latitude'], + 'longitude' => $vars['longitude'], + 'radius' => 300 + ]); + $filters['order'] = 'Distance ASC, project.status ASC, project.published DESC, project.name ASC'; + } elseif ($filter === 'outdated') { + $filters['type'] = 'outdated'; + $filters['status'] = Project::STATUS_IN_CAMPAIGN; + $filters['order'] = 'project.days ASC, project.published DESC, project.name ASC'; + } elseif ($filter === 'promoted') { + $filters['type'] = 'promoted'; + $filters['status'] = Project::STATUS_IN_CAMPAIGN; + $filters['order'] = 'promote.order ASC, project.published DESC, project.name ASC'; + } elseif ($filter === 'popular') { + $filters['type'] = 'popular'; + $filters['status'] = Project::STATUS_IN_CAMPAIGN; + $filters['order'] = 'project.popularity DESC, project.published DESC, project.name ASC'; + } elseif ($filter === 'succeeded') { + $filters['type'] = 'succeeded'; + $filters['status'] = [Project::STATUS_FUNDED, Project::STATUS_FULFILLED]; + $filters['order'] = 'project.published DESC, project.name ASC'; + unset($filters['published_since']); + } elseif ($filter === 'fulfilled') { + $filters['status'] = [Project::STATUS_FULFILLED]; + $filters['order'] = 'project.published DESC, project.name ASC'; + unset($filters['published_since']); + } elseif ($filter === 'archived') { + $filters['status'] = [Project::STATUS_UNFUNDED]; + $filters['order'] = 'project.published DESC, project.name ASC'; + $filters['published_since'] = (new DateTime('-24 month'))->format('Y-m-d'); + } elseif ($filter === 'matchfunding') { + $filters['type'] = 'matchfunding'; + unset($filters['published_since']); + } elseif ($filter === 'recent') { + $filters['type'] = 'recent'; + } + + if ($vars['review']) { + $filters['status'] = [ + Project::STATUS_EDITING, + Project::STATUS_REVIEWING, + Project::STATUS_IN_CAMPAIGN, + Project::STATUS_FUNDED, + Project::STATUS_FULFILLED, + Project::STATUS_UNFUNDED + ]; + $filters['is_draft'] = true; + } + + return $filters; + } +} From 74d5524362218aa9117916fb5fb574599245dfa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADnez=20Gadea?= Date: Thu, 21 Jul 2022 11:28:11 +0200 Subject: [PATCH 2/4] Add missing HomeUseCase. Remove ProjectFilters dependency from IndexController since it's just needed at the new HomeUseCase --- Resources/templates/responsive/home/index.php | 6 +- src/Goteo/Controller/IndexController.php | 5 - src/Goteo/UseCase/Home/HomeUseCase.php | 75 ++++++ .../UseCase/Home/HomeUseCaseResponse.php | 245 ++++++++++++++++++ 4 files changed, 323 insertions(+), 8 deletions(-) create mode 100644 src/Goteo/UseCase/Home/HomeUseCase.php create mode 100644 src/Goteo/UseCase/Home/HomeUseCaseResponse.php diff --git a/Resources/templates/responsive/home/index.php b/Resources/templates/responsive/home/index.php index 57d8b1729a..0c2ac99d00 100644 --- a/Resources/templates/responsive/home/index.php +++ b/Resources/templates/responsive/home/index.php @@ -24,9 +24,9 @@ insert('partials/components/main_slider', [ - 'banners' => $this->response->getBanners(), - 'nav' => 'home/partials/main_slider_nav', - 'button_text' => $this->text('invest-more-info') + 'banners' => $this->response->getBanners(), + 'nav' => 'home/partials/main_slider_nav', + 'button_text' => $this->text('invest-more-info') ]) ?> insert('home/partials/projects') ?> diff --git a/src/Goteo/Controller/IndexController.php b/src/Goteo/Controller/IndexController.php index 14c192a42e..0a7771950c 100644 --- a/src/Goteo/Controller/IndexController.php +++ b/src/Goteo/Controller/IndexController.php @@ -11,23 +11,18 @@ namespace Goteo\Controller; use Goteo\Application\View; -use Goteo\Model\Project; use Goteo\UseCase\Home\HomeUseCase; use Symfony\Component\HttpFoundation\Response; class IndexController extends BaseSymfonyController { - private Project\ProjectFilters $projectFilters; - public function __construct() { parent::__construct(); $this->dbReplica(true); $this->dbCache(true); View::setTheme('responsive'); - - $this->projectFilters = new Project\ProjectFilters(); } public function indexAction(): Response diff --git a/src/Goteo/UseCase/Home/HomeUseCase.php b/src/Goteo/UseCase/Home/HomeUseCase.php new file mode 100644 index 0000000000..4a98021450 --- /dev/null +++ b/src/Goteo/UseCase/Home/HomeUseCase.php @@ -0,0 +1,75 @@ +projectFilters = new Project\ProjectFilters(); + } + + public function execute(): HomeUseCaseResponse + { + $response = new HomeUseCaseResponse(); + $footprints = Footprint::getList(); + + $filters = $this->projectFilters->getFilters('promoted'); + $projects_by_footprint = []; + $sdg_by_footprint = []; + + foreach ($footprints as $footprint) { + $footprintImpactData[$footprint->id] = $footprint->getAllImpactData(); + $projects_by_footprint[$footprint->id] = Project::getByFootprint([ + 'footprints' => $footprint->id, + 'rand' => true, + 'amount_bigger_than' => 7000 + ]); + $sdg_by_footprint[$footprint->id] = Sdg::getList(['footprint' => $footprint->id]); + } + + $response + ->setProjects(Project::getList($filters, null, 0, $response->getLimit())) + ->setTotalProjects(Project::getList($filters, null, 0, 0, true)) + ->setStories(Stories::getList(['active' => true])) + ->setChannels(Node::getAll(['status' => 'active', 'type' => 'channel'])) + ->setBanners(Banner::getAll(true)) + ->setStats(Stats::create('home_stats')) + ->setFootprints($footprints) + ->setHomeItems(Home::getAll(Config::get('node'), 'index')) + ->setSponsors($this->getSponsors()) + ->setFootprintImpactData($footprintImpactData) + ->setProjectsByFootprint($projects_by_footprint) + ->setSdgByFootprint($sdg_by_footprint) + ; + + return $response; + } + + /** + * @return Sponsor[] + */ + private function getSponsors(): array + { + $sponsors = []; + + foreach(Sponsor::getTypes() as $type) { + $sponsors[$type] = Sponsor::getList(['type' => $type]); + } + + return $sponsors; + } +} diff --git a/src/Goteo/UseCase/Home/HomeUseCaseResponse.php b/src/Goteo/UseCase/Home/HomeUseCaseResponse.php new file mode 100644 index 0000000000..d5426ce07c --- /dev/null +++ b/src/Goteo/UseCase/Home/HomeUseCaseResponse.php @@ -0,0 +1,245 @@ +projects; + } + + /** + * @param Project[] + */ + public function setProjects(array $projects): self + { + $this->projects = $projects; + + return $this; + } + + public function getTotalProjects(): int + { + return $this->totalProjects; + } + + public function setTotalProjects(int $totalProjects): self + { + $this->totalProjects = $totalProjects; + + return $this; + } + + /** + * @return Stories[] + */ + public function getStories(): array + { + return $this->stories; + } + + /** + * @param Stories[] + */ + public function setStories(array $stories): self + { + $this->stories = $stories; + + return $this; + } + + /** + * @return Node[] + */ + public function getChannels(): array + { + return $this->channels; + } + + /** + * @param Node[] + */ + public function setChannels(array $channels): self + { + $this->channels = $channels; + + return $this; + } + + /** + * @return Banner[] + */ + public function getBanners(): array + { + return $this->banners; + } + + /** + * @param Node[] + */ + public function setBanners(array $banners): self + { + $this->banners = $banners; + + return $this; + } + + public function getStats(): Stats + { + return $this->stats; + } + + public function setStats(Stats $stats): self + { + $this->stats = $stats; + + return $this; + } + + /** + * @return Footprint[] + */ + public function getFootprints(): array + { + return $this->footprints; + } + + /** + * @param Footprint[] + */ + public function setFootprints(array $footprints): self + { + $this->footprints = $footprints; + + return $this; + } + + /** + * @return Home[] + */ + public function getHomeItems(): array + { + return $this->homeItems; + } + + /** + * @param Home[] + */ + public function setHomeItems(array $homeItems): self + { + $this->homeItems = $homeItems; + + return $this; + } + + /** + * @return Sponsor[] + */ + public function getSponsors(): array + { + return $this->sponsors; + } + + /** + * @param Sponsor[] + */ + public function setSponsors(array $sponsors): self + { + $this->sponsors = $sponsors; + + return $this; + } + + /** + * @return ImpactData[] + */ + public function getFootprintImpactData(): array + { + return $this->footprintImpactData; + } + + /** + * @param ImpactData[] + */ + public function setFootprintImpactData(array $footprintImpactData): self + { + $this->footprintImpactData = $footprintImpactData; + + return $this; + } + + /** + * @return Project[] + */ + public function getProjectsByFootprint(): array + { + return $this->projectsByFootprint; + } + + /** + * @param Project[] + */ + public function setProjectsByFootprint(array $projectsByFootprint): self + { + $this->projectsByFootprint = $projectsByFootprint; + + return $this; + } + + /** + * @return Project[] + */ + public function getSdgByFootprint(): array + { + return $this->sdgByFootprint; + } + + /** + * @param Project[] + */ + public function setSdgByFootprint(array $sdgByFootprint): self + { + $this->sdgByFootprint = $sdgByFootprint; + + return $this; + } +} From 518eabc159d1cfa38a3f8750a9e92d3853efffc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADnez=20Gadea?= Date: Wed, 27 Jul 2022 10:41:56 +0200 Subject: [PATCH 3/4] Refactor HomeUseCase so the response has properties on constructor instead of setters --- .../responsive/partials/components/values.php | 2 +- src/Goteo/UseCase/Home/HomeUseCase.php | 32 ++-- .../UseCase/Home/HomeUseCaseResponse.php | 160 +++++------------- 3 files changed, 58 insertions(+), 136 deletions(-) diff --git a/Resources/templates/responsive/partials/components/values.php b/Resources/templates/responsive/partials/components/values.php index 192a580031..71b2b9fc6a 100644 --- a/Resources/templates/responsive/partials/components/values.php +++ b/Resources/templates/responsive/partials/components/values.php @@ -67,7 +67,7 @@
- id] as $index => $project): ?> + id] as $project): ?>
id ?>>
diff --git a/src/Goteo/UseCase/Home/HomeUseCase.php b/src/Goteo/UseCase/Home/HomeUseCase.php index 4a98021450..26c2dd734d 100644 --- a/src/Goteo/UseCase/Home/HomeUseCase.php +++ b/src/Goteo/UseCase/Home/HomeUseCase.php @@ -24,12 +24,12 @@ public function __construct() public function execute(): HomeUseCaseResponse { - $response = new HomeUseCaseResponse(); $footprints = Footprint::getList(); $filters = $this->projectFilters->getFilters('promoted'); $projects_by_footprint = []; $sdg_by_footprint = []; + $footprintImpactData = []; foreach ($footprints as $footprint) { $footprintImpactData[$footprint->id] = $footprint->getAllImpactData(); @@ -41,22 +41,20 @@ public function execute(): HomeUseCaseResponse $sdg_by_footprint[$footprint->id] = Sdg::getList(['footprint' => $footprint->id]); } - $response - ->setProjects(Project::getList($filters, null, 0, $response->getLimit())) - ->setTotalProjects(Project::getList($filters, null, 0, 0, true)) - ->setStories(Stories::getList(['active' => true])) - ->setChannels(Node::getAll(['status' => 'active', 'type' => 'channel'])) - ->setBanners(Banner::getAll(true)) - ->setStats(Stats::create('home_stats')) - ->setFootprints($footprints) - ->setHomeItems(Home::getAll(Config::get('node'), 'index')) - ->setSponsors($this->getSponsors()) - ->setFootprintImpactData($footprintImpactData) - ->setProjectsByFootprint($projects_by_footprint) - ->setSdgByFootprint($sdg_by_footprint) - ; - - return $response; + return new HomeUseCaseResponse( + Project::getList($filters, null, 0, HomeUseCaseResponse::LIMIT), + Project::getList($filters, null, 0, 0, true), + Stories::getList(['active' => true]), + Node::getAll(['status' => 'active', 'type' => 'channel']), + Banner::getAll(true), + Stats::create('home_stats'), + $footprints, + Home::getAll(Config::get('node'), 'index'), + $this->getSponsors(), + $projects_by_footprint, + $sdg_by_footprint, + $footprintImpactData, + ); } /** diff --git a/src/Goteo/UseCase/Home/HomeUseCaseResponse.php b/src/Goteo/UseCase/Home/HomeUseCaseResponse.php index d5426ce07c..a271bac209 100644 --- a/src/Goteo/UseCase/Home/HomeUseCaseResponse.php +++ b/src/Goteo/UseCase/Home/HomeUseCaseResponse.php @@ -15,7 +15,7 @@ class HomeUseCaseResponse { public const LIMIT_ADD_JS = 12; - private const LIMIT = 24; + public const LIMIT = 24; private array $projects; private int $totalProjects; @@ -23,15 +23,53 @@ class HomeUseCaseResponse private array $channels; private array $banners; private Stats $stats; - private array $sponsors; private array $footprints; private array $homeItems; + private array $sponsors; private array $projectsByFootprint; private array $sdgByFootprint = []; private array $footprintImpactData = []; - public function __construct() - { + /** + * @param Project[] $projects + * @param int $totalProjects + * @param Stories[] $stories + * @param Node[] $channels + * @param Banner[] $banners + * @param Stats $stats + * @param Footprint[] $footprints + * @param Home[] $homeItems + * @param Sponsor[] $sponsors + * @param Project[] $projectsByFootprint + * @param Project[] $sdgByFootprint + * @param ImpactData[] $footprintImpactData + */ + public function __construct( + array $projects, + int $totalProjects, + array $stories, + array $channels, + array $banners, + Stats $stats, + array $footprints, + array $homeItems, + array $sponsors, + array $projectsByFootprint, + array $sdgByFootprint, + array $footprintImpactData + ) { + $this->projects = $projects; + $this->totalProjects = $totalProjects; + $this->stories = $stories; + $this->channels = $channels; + $this->banners = $banners; + $this->stats = $stats; + $this->footprints = $footprints; + $this->homeItems = $homeItems; + $this->sponsors = $sponsors; + $this->projectsByFootprint = $projectsByFootprint; + $this->sdgByFootprint = $sdgByFootprint; + $this->footprintImpactData = $footprintImpactData; } public function getLimit(): int @@ -47,28 +85,11 @@ public function getProjects(): array return $this->projects; } - /** - * @param Project[] - */ - public function setProjects(array $projects): self - { - $this->projects = $projects; - - return $this; - } - public function getTotalProjects(): int { return $this->totalProjects; } - public function setTotalProjects(int $totalProjects): self - { - $this->totalProjects = $totalProjects; - - return $this; - } - /** * @return Stories[] */ @@ -77,16 +98,6 @@ public function getStories(): array return $this->stories; } - /** - * @param Stories[] - */ - public function setStories(array $stories): self - { - $this->stories = $stories; - - return $this; - } - /** * @return Node[] */ @@ -95,16 +106,6 @@ public function getChannels(): array return $this->channels; } - /** - * @param Node[] - */ - public function setChannels(array $channels): self - { - $this->channels = $channels; - - return $this; - } - /** * @return Banner[] */ @@ -113,28 +114,11 @@ public function getBanners(): array return $this->banners; } - /** - * @param Node[] - */ - public function setBanners(array $banners): self - { - $this->banners = $banners; - - return $this; - } - public function getStats(): Stats { return $this->stats; } - public function setStats(Stats $stats): self - { - $this->stats = $stats; - - return $this; - } - /** * @return Footprint[] */ @@ -143,16 +127,6 @@ public function getFootprints(): array return $this->footprints; } - /** - * @param Footprint[] - */ - public function setFootprints(array $footprints): self - { - $this->footprints = $footprints; - - return $this; - } - /** * @return Home[] */ @@ -161,16 +135,6 @@ public function getHomeItems(): array return $this->homeItems; } - /** - * @param Home[] - */ - public function setHomeItems(array $homeItems): self - { - $this->homeItems = $homeItems; - - return $this; - } - /** * @return Sponsor[] */ @@ -179,16 +143,6 @@ public function getSponsors(): array return $this->sponsors; } - /** - * @param Sponsor[] - */ - public function setSponsors(array $sponsors): self - { - $this->sponsors = $sponsors; - - return $this; - } - /** * @return ImpactData[] */ @@ -197,16 +151,6 @@ public function getFootprintImpactData(): array return $this->footprintImpactData; } - /** - * @param ImpactData[] - */ - public function setFootprintImpactData(array $footprintImpactData): self - { - $this->footprintImpactData = $footprintImpactData; - - return $this; - } - /** * @return Project[] */ @@ -215,16 +159,6 @@ public function getProjectsByFootprint(): array return $this->projectsByFootprint; } - /** - * @param Project[] - */ - public function setProjectsByFootprint(array $projectsByFootprint): self - { - $this->projectsByFootprint = $projectsByFootprint; - - return $this; - } - /** * @return Project[] */ @@ -232,14 +166,4 @@ public function getSdgByFootprint(): array { return $this->sdgByFootprint; } - - /** - * @param Project[] - */ - public function setSdgByFootprint(array $sdgByFootprint): self - { - $this->sdgByFootprint = $sdgByFootprint; - - return $this; - } } From b8cb680f04c12ab4d1f4a54c0b2776c5da2bc5a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADnez=20Gadea?= Date: Wed, 27 Jul 2022 10:55:13 +0200 Subject: [PATCH 4/4] Refactor HomeUseCase to move some properties generation into a separate function --- src/Goteo/UseCase/Home/HomeUseCase.php | 41 +++++++++++++++----------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/Goteo/UseCase/Home/HomeUseCase.php b/src/Goteo/UseCase/Home/HomeUseCase.php index 26c2dd734d..6891355be8 100644 --- a/src/Goteo/UseCase/Home/HomeUseCase.php +++ b/src/Goteo/UseCase/Home/HomeUseCase.php @@ -16,6 +16,9 @@ class HomeUseCase { private Project\ProjectFilters $projectFilters; + private array $projects_by_footprint = []; + private array $sdg_by_footprint = []; + private array $footprintImpactData = []; public function __construct() { @@ -25,21 +28,8 @@ public function __construct() public function execute(): HomeUseCaseResponse { $footprints = Footprint::getList(); - + $this->generateFootprintRelatedData($footprints); $filters = $this->projectFilters->getFilters('promoted'); - $projects_by_footprint = []; - $sdg_by_footprint = []; - $footprintImpactData = []; - - foreach ($footprints as $footprint) { - $footprintImpactData[$footprint->id] = $footprint->getAllImpactData(); - $projects_by_footprint[$footprint->id] = Project::getByFootprint([ - 'footprints' => $footprint->id, - 'rand' => true, - 'amount_bigger_than' => 7000 - ]); - $sdg_by_footprint[$footprint->id] = Sdg::getList(['footprint' => $footprint->id]); - } return new HomeUseCaseResponse( Project::getList($filters, null, 0, HomeUseCaseResponse::LIMIT), @@ -51,12 +41,29 @@ public function execute(): HomeUseCaseResponse $footprints, Home::getAll(Config::get('node'), 'index'), $this->getSponsors(), - $projects_by_footprint, - $sdg_by_footprint, - $footprintImpactData, + $this->projects_by_footprint, + $this->sdg_by_footprint, + $this->footprintImpactData, ); } + /** + * @param Footprint[] $footprints + * @return void + */ + private function generateFootprintRelatedData(array $footprints): void + { + foreach ($footprints as $footprint) { + $this->footprintImpactData[$footprint->id] = $footprint->getAllImpactData(); + $this->projects_by_footprint[$footprint->id] = Project::getByFootprint([ + 'footprints' => $footprint->id, + 'rand' => true, + 'amount_bigger_than' => 7000 + ]); + $this->sdg_by_footprint[$footprint->id] = Sdg::getList(['footprint' => $footprint->id]); + } + } + /** * @return Sponsor[] */