Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
namespace App\News\Service;
use App\News\DtoFactory\NewsCategoryDtoFactory;
use App\News\DtoFactory\NewsFilterVariantsDtoFactory;
use App\News\DtoFactory\NewsListDtoFactory;
use App\News\DtoFactory\NewsListingElementDtoFactory;
use App\Shared\DtoFactory\PaginationDtoFactory;
use App\News\Dto\NewsListDto;
use App\Shared\Abstraction\AbstractRequest;
use App\Shared\Abstraction\ServiceInterface;
use App\Shared\Repository\NewsCategoriesRepository;
use App\Shared\Repository\NewsRepository;
class NewsListingService implements ServiceInterface
{
public function __construct(
private readonly NewsRepository $news,
private readonly NewsCategoriesRepository $newsCategories,
private readonly PaginationDtoFactory $paginationFactory,
private readonly NewsListingElementDtoFactory $listFactory,
private readonly NewsFilterVariantsDtoFactory $filterFactory,
private readonly NewsListDtoFactory $listingFactory,
private readonly NewsCategoryDtoFactory $categoryFactory,
) {}
public function serve(AbstractRequest $request): NewsListDto
{
$countOfNews = $this->news->getCountWithFilters($request->news_category);
$pagination = $this->paginationFactory->create(
$request->page,
$request->limit,
$countOfNews
);
$offset = min($pagination->pageSize, $countOfNews) * ($pagination->currentPage - 1);
$list = $this->listFactory->createCollection(
$this->news->getWithFilters($request->news_category, $request->limit, $offset)
);
$categoriesDto = $this->categoryFactory->createCollection(
$this->newsCategories->getAll()
);
$filters = $this->filterFactory->create($categoriesDto);
return $this->listingFactory->create(
$pagination,
$list,
$filters
);
}
}