Creator::createByDto()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 27
rs 9.7333
cc 1
nc 1
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\Tag;
17
18
use Veslo\AppBundle\Entity\Repository\BaseEntityRepository;
19
use Veslo\SanityBundle\Dto\Vacancy\Tag\GroupDto;
20
use Veslo\SanityBundle\Dto\Vacancy\TagDto;
21
use Veslo\SanityBundle\Entity\Vacancy\Tag;
22
use Veslo\SanityBundle\Entity\Vacancy\Tag\Group;
23
use Veslo\SanityBundle\Vacancy\Tag\Group\CreatorInterface as GroupCreatorInterface;
24
25
/**
26
 * Creates and persists a new sanity tag in the local storage
27
 */
28
class Creator
29
{
30
    /**
31
     * Creates and persists sanity tag groups in the local storage
32
     *
33
     * @var GroupCreatorInterface
34
     */
35
    private $groupCreator;
36
37
    /**
38
     * Repository where sanity tag entities are stored
39
     *
40
     * @var BaseEntityRepository
41
     */
42
    private $tagRepository;
43
44
    /**
45
     * Repository where sanity tag groups are stored
46
     *
47
     * @var BaseEntityRepository
48
     */
49
    private $groupRepository;
50
51
    /**
52
     * Creator constructor.
53
     *
54
     * @param GroupCreatorInterface $groupCreator    Creates and persists sanity tag groups in the local storage
55
     * @param BaseEntityRepository  $tagRepository   Repository where sanity tag entities are stored
56
     * @param BaseEntityRepository  $groupRepository Repository where sanity tag groups are stored
57
     */
58
    public function __construct(
59
        GroupCreatorInterface $groupCreator,
60
        BaseEntityRepository $tagRepository,
61
        BaseEntityRepository $groupRepository
62
    ) {
63
        $this->groupCreator    = $groupCreator;
64
        $this->tagRepository   = $tagRepository;
65
        $this->groupRepository = $groupRepository;
66
    }
67
68
    /**
69
     * Creates and returns a new sanity tag entity by specified data
70
     *
71
     * @param TagDto $tagData        Context of sanity tag data
72
     * @param bool   $isCascadeChild Whenever entity creation is just a part of the entity-owner creation and entity
73
     *                               manager should not be instantly flushed
74
     *
75
     * @return Tag
76
     */
77
    public function createByDto(TagDto $tagData, bool $isCascadeChild = false): Tag
78
    {
79
        $tag = new Tag();
80
81
        $tagName = $tagData->getName();
82
        $tag->setName($tagName);
83
84
        $tagTitle = $tagData->getTitle();
85
        $tag->setTitle($tagTitle);
86
87
        $tagDescription = $tagData->getDescription();
88
        $tag->setDescription($tagDescription);
89
90
        $tagColor = $tagData->getColor();
91
        $tag->setColor($tagColor);
92
93
        $tagImageUrl = $tagData->getImageUrl();
94
        $tag->setImageUrl($tagImageUrl);
95
96
        $groupData = $tagData->getGroup();
97
        $group     = $this->resolveGroup($groupData);
98
99
        $tag->setGroup($group);
100
101
        $this->tagRepository->save($tag, !$isCascadeChild);
102
103
        return $tag;
104
    }
105
106
    /**
107
     * Returns related group entity if exists or calls the group creator to build a new one
108
     *
109
     * @param GroupDto $groupData Context of vacancy tags group data
110
     *
111
     * @return Group
112
     */
113
    private function resolveGroup(GroupDto $groupData): Group
114
    {
115
        $groupName = $groupData->getName();
116
        $group     = $this->groupRepository->findOneByName($groupName);
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

116
        /** @scrutinizer ignore-call */ 
117
        $group     = $this->groupRepository->findOneByName($groupName);
Loading history...
117
118
        if (!$group instanceof Group) {
119
            $group = $this->groupCreator->createByDto($groupData, true);
120
        }
121
122
        return $group;
123
    }
124
}
125