|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Explicit Architecture POC, |
|
7
|
|
|
* which is created on top of the Symfony Demo application. |
|
8
|
|
|
* |
|
9
|
|
|
* (c) Herberto Graça <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
12
|
|
|
* file that was distributed with this source code. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Acme\App\Presentation\Web\Core\Component\Blog\Anonymous\PostList; |
|
16
|
|
|
|
|
17
|
|
|
use Acme\App\Core\Component\Blog\Application\Query\LatestPostWithAuthorAndTagsDto; |
|
18
|
|
|
use Acme\App\Core\Port\TemplateEngine\TemplateViewModelInterface; |
|
19
|
|
|
use Acme\App\Presentation\Web\Core\Port\Paginator\PaginatorFactoryInterface; |
|
20
|
|
|
use Acme\App\Presentation\Web\Core\Port\Paginator\PaginatorInterface; |
|
21
|
|
|
|
|
22
|
|
View Code Duplication |
final class GetXmlViewModel implements TemplateViewModelInterface |
|
|
|
|
|
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var PaginatorInterface [string, string, string, DateTimeInterface, string, string[]] |
|
26
|
|
|
*/ |
|
27
|
|
|
private $postList; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The view model constructor depends on the most raw elements possible. |
|
31
|
|
|
* In this case the view model constructor is private, so that we force the usage of the named constructor. |
|
32
|
|
|
* We want this because it is not possible to have the a constructor with the raw data, since it is a list of data. |
|
33
|
|
|
*/ |
|
34
|
|
|
private function __construct(PaginatorInterface $postList) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->postList = $postList; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* We create named constructors for the cases where we need to extract the raw data from complex data structures. |
|
41
|
|
|
*/ |
|
42
|
|
|
public static function fromPostDtoList( |
|
43
|
|
|
PaginatorFactoryInterface $paginatorFactory, |
|
44
|
|
|
int $page, |
|
45
|
|
|
LatestPostWithAuthorAndTagsDto ...$postList |
|
46
|
|
|
): self { |
|
47
|
|
|
$postDataList = []; |
|
48
|
|
|
foreach ($postList as $post) { |
|
49
|
|
|
$postDataList[] = [ |
|
50
|
|
|
'title' => $post->getTitle(), |
|
51
|
|
|
'summary' => $post->getSummary(), |
|
52
|
|
|
'slug' => $post->getSlug(), |
|
53
|
|
|
'publishedAt' => $post->getPublishedAt(), |
|
54
|
|
|
'authorFullName' => $post->getAuthorFullName(), |
|
55
|
|
|
'authorEmail' => $post->getAuthorEmail(), |
|
56
|
|
|
'tagList' => $post->getTagList(), |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$paginator = $paginatorFactory->createPaginator($postDataList); |
|
61
|
|
|
$paginator->setCurrentPage($page); |
|
62
|
|
|
|
|
63
|
|
|
return new self($paginator); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function getPostList(): PaginatorInterface |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->postList; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.