MinistryOfTruth::convertIndex()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 15
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Veslo project <https://github.com/symfony-doge/veslo>.
5
 *
6
 * (C) 2019 Pavel Petrov <[email protected]>.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @license https://opensource.org/licenses/GPL-3.0 GPL-3.0
12
 */
13
14
declare(strict_types=1);
15
16
namespace Veslo\SanityBundle\Vacancy\Analyser\DataConverter;
17
18
use SymfonyDoge\MinistryOfTruthClient\Dto\Response\Index\ContentDto as IndexData;
19
use Veslo\SanityBundle\Dto\Vacancy\IndexDto;
20
use Veslo\SanityBundle\Dto\Vacancy\Tag\GroupDto;
21
use SymfonyDoge\MinistryOfTruthClient\Dto\Response\Index\TagDto as TagData;
22
use Veslo\SanityBundle\Dto\Vacancy\TagDto;
23
24
/**
25
 * Converts sanity index data from format of external microservice to local data transfer objects
26
 */
27
class MinistryOfTruth
28
{
29
    /**
30
     * Returns index dto created by external data source
31
     *
32
     * @param IndexData $index Sanity index content structure from external microservice
33
     *
34
     * @return IndexDto
35
     */
36
    public function convertIndex(IndexData $index): IndexDto
37
    {
38
        $indexDto = new IndexDto();
39
40
        $indexValue = $index->getValue();
41
        $indexDto->setValue($indexValue);
42
43
        $tags        = $index->getTags();
44
        $tagDtoArray = $this->convertTags($tags);
45
46
        foreach ($tagDtoArray as $tagDto) {
47
            $indexDto->addTag($tagDto);
48
        }
49
50
        return $indexDto;
51
    }
52
53
    /**
54
     * Returns tags dto array created by external data source
55
     *
56
     * @param array $tagData Tags array, indexed by group name
57
     *
58
     * @return array
59
     */
60
    private function convertTags(array $tagData): array
61
    {
62
        $tagDtoArray = [];
63
64
        /** @var TagData[] $tags */
65
        foreach ($tagData as $tagsGroupName => $tags) {
66
            $groupDto = new GroupDto();
67
            $groupDto->setName($tagsGroupName);
68
69
            foreach ($tags as $tag) {
70
                $tagDto = new TagDto();
71
                $tagDto->setGroup($groupDto);
72
73
                $tagName = $tag->getName();
74
                $tagDto->setName($tagName);
75
76
                $tagTitle = $tag->getTitle();
77
                $tagDto->setTitle($tagTitle);
78
79
                $tagDescription = $tag->getDescription();
80
                $tagDto->setDescription($tagDescription);
81
82
                $tagColor = $tag->getColor();
83
                $tagDto->setColor($tagColor);
84
85
                $tagImageUrl = $tag->getImageUrl();
86
                $tagDto->setImageUrl($tagImageUrl);
87
88
                $tagDtoArray[] = $tagDto;
89
            }
90
        }
91
92
        return $tagDtoArray;
93
    }
94
}
95