GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( d5b8d4...f20003 )
by Ross
21s queued 13s
created

TemplateEntityUpserter::getUpsertDtoByProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TemplateNamespace\Entity\Savers;
4
5
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\DataTransferObjectInterface;
6
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaver;
7
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\NewUpsertDtoDataModifierInterface;
8
use TemplateNamespace\Entity\DataTransferObjects\TemplateEntityDto;
9
use TemplateNamespace\Entity\Factories\TemplateEntityDtoFactory;
10
use TemplateNamespace\Entity\Factories\TemplateEntityFactory;
11
use TemplateNamespace\Entity\Interfaces\TemplateEntityInterface;
12
use TemplateNamespace\Entity\Repositories\TemplateEntityRepository;
13
14
class TemplateEntityUpserter
15
{
16
    /**
17
     * @var TemplateEntityDtoFactory
18
     */
19
    private $dtoFactory;
20
    /**
21
     * @var TemplateEntityFactory
22
     */
23
    private $entityFactory;
24
    /**
25
     * @var TemplateEntityRepository
26
     */
27
    private $repository;
28
    /**
29
     * @var EntitySaver
30
     */
31
    private $saver;
32
    /**
33
     * @var TemplateEntityUnitOfWorkHelper
34
     */
35
    private $unitOfWorkHelper;
36
37
    public function __construct(
38
        TemplateEntityRepository $repository,
39
        TemplateEntityDtoFactory $dtoFactory,
40
        TemplateEntityFactory $entityFactory,
41
        EntitySaver $saver,
42
        TemplateEntityUnitOfWorkHelper $unitOfWorkHelper
43
    ) {
44
        $this->repository       = $repository;
45
        $this->dtoFactory       = $dtoFactory;
46
        $this->entityFactory    = $entityFactory;
47
        $this->saver            = $saver;
48
        $this->unitOfWorkHelper = $unitOfWorkHelper;
49
    }
50
51
    public function getUpsertDtoByProperties(array $propertiesToValues): TemplateEntityDto
52
    {
53
        $modifier = $this->getModifierClass($propertiesToValues);
54
55
        return $this->getUpsertDtoByCriteria($propertiesToValues, $modifier);
56
    }
57
58
    private function getModifierClass(array $propertiesToValues): NewUpsertDtoDataModifierInterface
59
    {
60
        return new class($propertiesToValues) implements NewUpsertDtoDataModifierInterface
61
        {
62
            private $propertiesToValues;
63
64
            public function __construct(array $propertiesToValues)
65
            {
66
                $this->propertiesToValues = $propertiesToValues;
67
            }
68
69
            public function addDataToNewlyCreatedDto(DataTransferObjectInterface $dto): void
70
            {
71
                foreach ($this->propertiesToValues as $property => $value) {
72
                    $setter = 'set' . ucfirst($property);
73
                    $dto->$setter($value);
74
                }
75
            }
76
        };
77
    }
78
79
    /**
80
     * This method is used to get a DTO using search criteria, when you are not certain if the entity exists or not.
81
     * The criteria is passed through to the repository findOneBy method, if an entity is found then a DTO will be
82
     * created from it and returned.
83
     *
84
     * If an entity is not found then a new empty DTO will be created and returned instead.
85
     *
86
     * @param array                             $criteria
87
     * @param NewUpsertDtoDataModifierInterface $modifier
88
     *
89
     * @return TemplateEntityDto
90
     * @see \Doctrine\ORM\EntityRepository::findOneBy for how to use the crietia
91
     */
92
    public function getUpsertDtoByCriteria(
93
        array $criteria,
94
        NewUpsertDtoDataModifierInterface $modifier
95
    ): TemplateEntityDto {
96
        $entity = $this->repository->findOneBy($criteria);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $entity is correct as $this->repository->findOneBy($criteria) targeting TemplateNamespace\Entity...Repository::findOneBy() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
97
        if ($entity === null) {
0 ignored issues
show
introduced by
The condition $entity === null is always true.
Loading history...
98
            $dto = $this->dtoFactory->create();
99
            $modifier->addDataToNewlyCreatedDto($dto);
100
101
            return $dto;
102
        }
103
104
        return $this->dtoFactory->createDtoFromTemplateEntity($entity);
105
    }
106
107
    public function getUpsertDtoByProperty(string $propertyName, $value): TemplateEntityDto
108
    {
109
        $modifier = $this->getModifierClass([$propertyName => $value]);
110
111
        return $this->getUpsertDtoByCriteria([$propertyName => $value], $modifier);
112
    }
113
114
    /**
115
     * This is used to persist the DTO to the database. If the DTO is for a new entity then it will be created, if it
116
     * is for an existing Entity then it will be updated.
117
     *
118
     * Be aware that this method should __only__ be used with DTOs that have been created using the
119
     * self::getUpsertDtoByCriteria method, as if they come from elsewhere we will not not if the entity needs to be
120
     * created or updated
121
     *
122
     * @param TemplateEntityDto $dto
123
     *
124
     * @return TemplateEntityInterface
125
     * @throws \Doctrine\DBAL\DBALException
126
     */
127
    public function persistUpsertDto(TemplateEntityDto $dto): TemplateEntityInterface
128
    {
129
        $entity = $this->convertUpsertDtoToEntity($dto);
130
        $this->saver->save($entity);
131
132
        return $entity;
133
    }
134
135
    /**
136
     * This method will convert the DTO into an entity, but will not save it. This is useful if you want to bulk create
137
     * or update entities
138
     *
139
     * @param TemplateEntityDto $dto
140
     *
141
     * @return TemplateEntityInterface
142
     */
143
    public function convertUpsertDtoToEntity(TemplateEntityDto $dto): TemplateEntityInterface
144
    {
145
        if ($this->unitOfWorkHelper->hasRecordOfDto($dto) === false) {
146
            $entity = $this->entityFactory->create($dto);
147
148
            return $entity;
149
        }
150
        $entity = $this->unitOfWorkHelper->getEntityFromUnitOfWorkUsingDto($dto);
151
        $entity->update($dto);
152
153
        return $entity;
154
    }
155
}