Creator::createByDto()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 23
rs 9.8333
cc 2
nc 2
nop 2
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\Index;
17
18
use Veslo\AppBundle\Entity\Repository\BaseEntityRepository;
19
use Veslo\SanityBundle\Dto\Vacancy\IndexDto;
20
use Veslo\SanityBundle\Dto\Vacancy\TagDto;
21
use Veslo\SanityBundle\Entity\Vacancy\Index;
22
use Veslo\SanityBundle\Entity\Vacancy\Tag;
23
use Veslo\SanityBundle\Vacancy\Tag\Creator as TagCreator;
24
25
/**
26
 * Creates and persists sanity index entities in the local storage
27
 */
28
class Creator
29
{
30
    /**
31
     * Creates and persists sanity tags in the local storage
32
     *
33
     * @var TagCreator
34
     */
35
    private $tagCreator;
36
37
    /**
38
     * Repository where sanity index entities are stored
39
     *
40
     * @var BaseEntityRepository
41
     */
42
    private $indexRepository;
43
44
    /**
45
     * Repository where sanity tag entities are stored
46
     *
47
     * @var BaseEntityRepository
48
     */
49
    private $tagRepository;
50
51
    /**
52
     * Creator constructor.
53
     *
54
     * @param TagCreator           $tagCreator      Creates and persists sanity tags in the local storage
55
     * @param BaseEntityRepository $indexRepository Repository where sanity index entities are stored
56
     * @param BaseEntityRepository $tagRepository   Repository where sanity tag entities are stored
57
     */
58
    public function __construct(
59
        TagCreator $tagCreator,
60
        BaseEntityRepository $indexRepository,
61
        BaseEntityRepository $tagRepository
62
    ) {
63
        $this->tagCreator      = $tagCreator;
64
        $this->indexRepository = $indexRepository;
65
        $this->tagRepository   = $tagRepository;
66
    }
67
68
    /**
69
     * Creates and returns a new sanity index entity by specified data
70
     *
71
     * @param IndexDto $indexData      Context of sanity index data
72
     * @param bool     $isCascadeChild Whenever entity creation is just a part of the entity-owner creation
73
     *                                 and entity manager should not be instantly flushed
74
     *
75
     * @return Index
76
     */
77
    public function createByDto(IndexDto $indexData, bool $isCascadeChild = false): Index
78
    {
79
        $index = new Index();
80
81
        $indexValue = $indexData->getValue();
82
        $index->setValue($indexValue);
83
84
        $vacancyId = $indexData->getVacancyId();
85
        $index->setVacancyId($vacancyId);
86
87
        $indexationDate = $indexData->getIndexationDate();
88
        $index->setIndexationDate($indexationDate);
89
90
        $tagDataArray = $indexData->getTags();
91
        $tags         = $this->resolveTags($tagDataArray);
92
93
        foreach ($tags as $tag) {
94
            $index->addTag($tag);
95
        }
96
97
        $this->indexRepository->save($index, !$isCascadeChild);
98
99
        return $index;
100
    }
101
102
    /**
103
     * Returns related tag entities
104
     *
105
     * @param TagDto[] $tagDataArray Tags to be assigned to the vacancy after indexation
106
     *
107
     * @return Tag[]
108
     */
109
    private function resolveTags(array $tagDataArray): array
110
    {
111
        $tags = [];
112
113
        foreach ($tagDataArray as $tagData) {
114
            $tags[] = $this->resolveTag($tagData);
115
        }
116
117
        return $tags;
118
    }
119
120
    /**
121
     * Returns related tag entity if exists or calls the tag creator to build a new set
122
     *
123
     * @param TagDto $tagData Tag to be assigned to the vacancy
124
     *
125
     * @return Tag
126
     */
127
    private function resolveTag(TagDto $tagData): Tag
128
    {
129
        $tagName = $tagData->getName();
130
        $tag     = $this->tagRepository->findOneByName($tagName);
0 ignored issues
show
Bug introduced by
The method findOneByName() does not exist on Veslo\AppBundle\Entity\R...ry\BaseEntityRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

130
        /** @scrutinizer ignore-call */ 
131
        $tag     = $this->tagRepository->findOneByName($tagName);
Loading history...
131
132
        if (!$tag instanceof Tag) {
133
            $tag = $this->tagCreator->createByDto($tagData, true);
134
        }
135
136
        return $tag;
137
    }
138
}
139