Passed
Pull Request — develop (#155)
by Laurent
02:14
created

EditFamilyLogHandler::updateFamilyLog()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 7
eloc 12
c 1
b 0
f 1
nc 8
nop 2
dl 0
loc 19
rs 8.8333
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the G.L.S.R. Apps package.
7
 *
8
 * (c) Dev-Int Création <[email protected]>.
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Administration\Domain\FamilyLog\Handler;
15
16
use Administration\Domain\FamilyLog\Command\EditFamilyLog;
17
use Administration\Domain\FamilyLog\Model\FamilyLog;
18
use Administration\Domain\Protocol\Repository\FamilyLogRepositoryProtocol;
19
use Core\Domain\Protocol\Common\Command\CommandHandlerProtocol;
20
21
class EditFamilyLogHandler implements CommandHandlerProtocol
22
{
23
    private FamilyLogRepositoryProtocol $repository;
24
25
    public function __construct(FamilyLogRepositoryProtocol $repository)
26
    {
27
        $this->repository = $repository;
28
    }
29
30
    public function __invoke(EditFamilyLog $command): void
31
    {
32
        if ($this->repository->existWithLabel($command->label()->getValue(), $command->parent())) {
33
            throw new \DomainException("FamilyLog with name: {$command->label()->getValue()} already exist!");
34
        }
35
36
        $familyLogToUpdate = $this->repository->findParent($command->uuid()->toString());
37
38
        $familyLog = $this->updateFamilyLog($familyLogToUpdate, $command);
39
40
        $this->repository->update($familyLog);
41
    }
42
43
    private function updateFamilyLog(FamilyLog $familyLog, EditFamilyLog $command): FamilyLog
44
    {
45
        $parent = null;
46
        if ($familyLog->label() !== $command->label()->getValue()) {
47
            $familyLog->rename($command->label());
48
        }
49
50
        if (null !== $command->parent()) {
51
            $parent = $this->repository->findParent($command->parent());
52
            if ((null !== $familyLog->parent()) && ($familyLog->parent()->uuid() !== $parent->uuid())) {
53
                $familyLog->attributeParent($parent);
54
            } else {
55
                $familyLog->attributeParent($parent);
56
            }
57
        } elseif (null === $command->parent() && null !== $familyLog->parent()) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $command->parent() targeting Administration\Domain\Fa...EditFamilyLog::parent() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
58
            $familyLog->attributeParent($parent);
0 ignored issues
show
Bug introduced by
$parent of type null is incompatible with the type Administration\Domain\FamilyLog\Model\FamilyLog expected by parameter $parent of Administration\Domain\Fa...yLog::attributeParent(). ( Ignorable by Annotation )

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

58
            $familyLog->attributeParent(/** @scrutinizer ignore-type */ $parent);
Loading history...
59
        }
60
61
        return $familyLog;
62
    }
63
}
64