GetViewModel   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 46
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A fromPostDtoList() 0 9 2
A addPostData() 0 11 1
A getPostList() 0 4 1
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\Admin\PostList;
16
17
use Acme\App\Core\Component\Blog\Application\Query\PostDto;
18
use Acme\App\Core\Component\Blog\Domain\Post\PostId;
19
use Acme\App\Core\Port\TemplateEngine\TemplateViewModelInterface;
20
use DateTimeInterface;
21
22
final class GetViewModel implements TemplateViewModelInterface
23
{
24
    /**
25
     * @var array [PostId, string, DateTimeInterface]
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()
35
    {
36
    }
37
38
    /**
39
     * We create named constructors for the cases where we need to extract the raw data from complex data structures.
40
     */
41
    public static function fromPostDtoList(PostDto ...$postDtoList): self
42
    {
43
        $viewModel = new self();
44
        foreach ($postDtoList as $postDto) {
45
            $viewModel->addPostData($postDto->getId(), $postDto->getTitle(), $postDto->getPublishedAt());
46
        }
47
48
        return $viewModel;
49
    }
50
51
    private function addPostData(
52
        PostId $id,
53
        string $title,
54
        DateTimeInterface $publishedAt
55
    ): void {
56
        $this->postList[] = [
57
            'id' => $id,
58
            'title' => $title,
59
            'publishedAt' => $publishedAt,
60
        ];
61
    }
62
63
    public function getPostList(): array
64
    {
65
        return $this->postList;
66
    }
67
}
68