GetXmlViewModel   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 49
loc 49
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A fromPostDtoList() 23 23 2
A getPostList() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

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.

Loading history...
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