Skip to content

Commit

Permalink
Re-factor TitleGenerators
Browse files Browse the repository at this point in the history
and co-locate them inside controllers
  • Loading branch information
eronisko committed Nov 2, 2023
1 parent 1ac3f98 commit 084b4e8
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 160 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
All notable changes to this project will be documented in this file[^1].

## [Unreleased]
### Changed
- title generators to turn them into controller methods

## [2.83.0] - 2023-10-26
### Fixed
Expand Down
8 changes: 0 additions & 8 deletions app/Filter/Contracts/TitleGenerator.php

This file was deleted.

70 changes: 0 additions & 70 deletions app/Filter/Generators/AbstractTitleGenerator.php

This file was deleted.

16 changes: 0 additions & 16 deletions app/Filter/Generators/AuthorityTitleGenerator.php

This file was deleted.

31 changes: 0 additions & 31 deletions app/Filter/Generators/ItemTitleGenerator.php

This file was deleted.

17 changes: 8 additions & 9 deletions app/Http/Controllers/AbstractSearchRequestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace App\Http\Controllers;

use App\Elasticsearch\Repositories\TranslatableRepository;
use App\Filter\Contracts\Filter;
use App\Filter\Contracts\SearchRequest;
use App\Filter\Contracts\TitleGenerator;
use Astrotomic\Translatable\Contracts\Translatable;
use Barryvdh\Form\CreatesForms;
use Illuminate\Pagination\Paginator;
Expand All @@ -20,22 +20,21 @@ abstract class AbstractSearchRequestController extends Controller

protected $repository;

protected $titleGenerator;

protected $searchRequestClass;

protected $searchRequestFormClass;

protected $indexView;

public function __construct(
TranslatableRepository $repository,
TitleGenerator $titleGenerator
) {
protected $titleAttributes;

public function __construct(TranslatableRepository $repository)
{
$this->repository = $repository;
$this->titleGenerator = $titleGenerator;
}

abstract protected function generateTitle(Filter $filter);

public function getIndex()
{
return view($this->indexView, $this->getIndexData());
Expand Down Expand Up @@ -78,7 +77,7 @@ protected function getIndexData()
'hasFilters' => (bool)$this->repository->buildQueryFromFilter($searchRequest),
'searchRequest' => $searchRequest,
'form' => $searchRequestForm->createView(),
'title' => $this->titleGenerator->generate($searchRequest),
'title' => $this->generateTitle($searchRequest),
'urlWithoutColor' => route(request()->route()->getName(), Arr::except(request()->query(), ['color'])),
'untranslated' => $collection->contains(function (Translatable $translatable) {
return !$translatable->hasTranslation();
Expand Down
32 changes: 25 additions & 7 deletions app/Http/Controllers/AuthorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use App\Elasticsearch\Repositories\AuthorityRepository;
use App\Elasticsearch\Repositories\ItemRepository;
use App\Filter\AuthoritySearchRequest;
use App\Filter\Contracts\Filter;
use App\Filter\Forms\Types\AuthoritySearchRequestType;
use App\Filter\Generators\AuthorityTitleGenerator;
use Illuminate\Pagination\AbstractPaginator;

class AuthorController extends AbstractSearchRequestController
Expand All @@ -20,12 +20,9 @@ class AuthorController extends AbstractSearchRequestController

protected $itemRepository;

public function __construct(
AuthorityRepository $repository,
AuthorityTitleGenerator $titleGenerator,
ItemRepository $itemRepository
) {
parent::__construct($repository, $titleGenerator);
public function __construct(AuthorityRepository $repository, ItemRepository $itemRepository)
{
parent::__construct($repository);
$this->itemRepository = $itemRepository;
}

Expand Down Expand Up @@ -74,4 +71,25 @@ protected function getIndexData()
});
return $data;
}

protected function generateTitle(Filter $filter)
{
$attributes = collect(['role', 'nationality', 'place', 'years', 'first_letter']);

return $attributes
->filter(fn($attribute) => $filter->get($attribute) !== null)
->map(function ($attribute) use ($filter) {
$value = $filter->get($attribute);

if ($attribute === 'years') {
return trans('authority.filter.title_generator.' . $attribute, [
'from' => $value->getFrom(),
'to' => $value->getTo(),
]);
}

return trans('authority.filter.title_generator.' . $attribute, ['value' => $value]);
})
->implode(" \u{2022} ");
}
}
54 changes: 45 additions & 9 deletions app/Http/Controllers/CatalogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace App\Http\Controllers;

use App\Elasticsearch\Repositories\ItemRepository;
use App\Filter\Contracts\Filter;
use App\Filter\Forms\Types\ItemSearchRequestType;
use App\Filter\Generators\ItemTitleGenerator;
use App\Filter\ItemFilter;
use App\Filter\ItemSearchRequest;
use App\Item;
Expand All @@ -17,11 +17,9 @@ class CatalogController extends AbstractSearchRequestController

protected $indexView = 'frontend.catalog.index';

public function __construct(
ItemRepository $repository,
ItemTitleGenerator $titleGenerator
) {
parent::__construct($repository, $titleGenerator);
public function __construct(ItemRepository $repository)
{
parent::__construct($repository);
}

public function getSuggestionsData()
Expand All @@ -34,14 +32,52 @@ public function getSuggestionsData()

public function getRandom()
{
$filter = (new ItemFilter)
->setHasImage(true)
->setHasIip(true);
$filter = (new ItemFilter())->setHasImage(true)->setHasIip(true);
$item = $this->repository
->getRandom(1, $filter)
->getCollection()
->first();

return response()->json($item);
}

protected function generateTitle(Filter $filter)
{
$attributes = collect([
'search',
'author',
'work_type',
'tag',
'gallery',
'credit',
'topic',
'medium',
'technique',
'has_image',
'has_iip',
'is_free',
'related_work',
'years',
]);

return $attributes
->filter(fn($attribute) => $filter->get($attribute) !== null)
->map(function ($attribute) use ($filter) {
$value = $filter->get($attribute);

if ($attribute === 'author') {
$value = formatName($value);
}

if ($attribute === 'years') {
return trans('item.filter.title_generator.' . $attribute, [
'from' => $value->getFrom(),
'to' => $value->getTo(),
]);
}

return trans('item.filter.title_generator.' . $attribute, ['value' => $value]);
})
->implode(" \u{2022} ");
}
}
10 changes: 0 additions & 10 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
use App\Elasticsearch\Repositories\ItemRepository;
use App\Filter\Forms\Types\AuthoritySearchRequestType;
use App\Filter\Forms\Types\ItemSearchRequestType;
use App\Filter\Generators\AuthorityTitleGenerator;
use App\Filter\Generators\ItemTitleGenerator;
use App\Harvest\Importers\ItemImporter;
use App\Harvest\Mappers\BaseAuthorityMapper;
use Illuminate\Filesystem\FilesystemAdapter;
Expand All @@ -16,8 +14,6 @@
use League\Flysystem\Filesystem;
use League\Flysystem\WebDAV\WebDAVAdapter;
use Sabre\DAV\Client;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;

class AppServiceProvider extends ServiceProvider
{
Expand All @@ -32,14 +28,8 @@ class AppServiceProvider extends ServiceProvider
*/
public function register()
{
$this->app->singleton(PropertyAccessor::class, function () {
return PropertyAccess::createPropertyAccessor();
});

$this->app->singleton(AuthorityRepository::class);
$this->app->singleton(ItemRepository::class);
$this->app->singleton(AuthorityTitleGenerator::class);
$this->app->singleton(ItemTitleGenerator::class);
$this->app->bind(AuthoritySearchRequestType::class);
$this->app->bind(ItemSearchRequestType::class);

Expand Down

0 comments on commit 084b4e8

Please sign in to comment.