Completed
Pull Request — develop (#305)
by Wachter
15:11
created

PostArticleHandler::findRoutePathProperty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sulu\Bundle\ArticleBundle\Prooph\Model\Handler;
6
7
use Sulu\Bundle\ArticleBundle\Prooph\Model\ArticleRepositoryInterface;
8
use Sulu\Bundle\ArticleBundle\Prooph\Model\ArticleTranslation;
9
use Sulu\Bundle\ArticleBundle\Prooph\Model\Command\PostArticleCommand;
10
use Sulu\Bundle\RouteBundle\Generator\ChainRouteGeneratorInterface;
11
use Sulu\Bundle\RouteBundle\Manager\ConflictResolverInterface;
12
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;
13
use Sulu\Component\Content\Metadata\StructureMetadata;
14
15
class PostArticleHandler
16
{
17
    const ROUTE_PROPERTY = 'routePath';
18
19
    const TAG_NAME = 'sulu_article.article_route';
20
21
    /**
22
     * @var ArticleRepositoryInterface
23
     */
24
    private $repository;
25
26
    /**
27
     * @var StructureMetadataFactoryInterface
28
     */
29
    private $metadataFactory;
30
31
    /**
32
     * @var ChainRouteGeneratorInterface
33
     */
34
    private $chainRouteGenerator;
35
36
    /**
37
     * @var ConflictResolverInterface
38
     */
39
    private $conflictResolver;
40
41
    public function __construct(
42
        ArticleRepositoryInterface $repository,
43
        StructureMetadataFactoryInterface $metadataFactory,
44
        ChainRouteGeneratorInterface $chainRouteGenerator,
45
        ConflictResolverInterface $conflictResolver
46
    ) {
47
        $this->repository = $repository;
48
        $this->metadataFactory = $metadataFactory;
49
        $this->chainRouteGenerator = $chainRouteGenerator;
50
        $this->conflictResolver = $conflictResolver;
51
    }
52
53
    public function __invoke(PostArticleCommand $command): void
54
    {
55
        $structureType = $command->requestData()['template'];
56
        $metadata = $this->metadataFactory->getStructureMetadata('article', $structureType);
57
58
        $structureData = [];
59 View Code Duplication
        foreach ($metadata->getProperties() as $property) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
            if (array_key_exists($property->getName(), $command->requestData())) {
61
                $structureData[$property->getName()] = $command->requestData()[$property->getName()];
62
            }
63
        }
64
65
        $propertyName = $this->findRoutePathProperty($metadata)->getName();
0 ignored issues
show
Bug introduced by
It seems like $metadata defined by $this->metadataFactory->...ticle', $structureType) on line 56 can be null; however, Sulu\Bundle\ArticleBundl...findRoutePathProperty() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
66
        $structureData[$propertyName] = $this->generateRoute($command, $structureType, $structureData, $propertyName);
67
        $requestData = $command->requestData();
68
        $requestData[$propertyName] = $structureData[$propertyName];
69
70
        $article = $this->repository->create($command->id(), $command->userId());
71
        $article->modifyTranslationStructure(
72
            $command->locale(),
73
            $structureType,
74
            $structureData,
75
            $command->userId(),
76
            $requestData
77
        );
78
        $this->repository->save($article);
79
    }
80
81
    private function generateRoute(
82
        PostArticleCommand $command,
83
        string $structureType,
84
        array $structureData,
85
        string $propertyName
86
    ): string {
87
        $routePath = array_key_exists($propertyName, $structureData) ? $structureData[$propertyName] : null;
88
89
        $translation = new ArticleTranslation();
90
        $translation->id = $command->id();
91
        $translation->title = $structureData['title'];
92
        $translation->routePath = $routePath;
93
        $translation->locale = $command->locale();
94
        $translation->structureType = $structureType;
95
        $translation->structureData = $structureData;
96
        $translation->createdBy = $command->userId();
97
        $translation->createdAt = $command->createdAt();
98
        $translation->modifiedAt = $command->userId();
0 ignored issues
show
Documentation Bug introduced by
It seems like $command->userId() of type integer is incompatible with the declared type object<DateTimeImmutable> of property $modifiedAt.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
99
        $translation->modifiedBy = $command->createdAt();
0 ignored issues
show
Documentation Bug introduced by
It seems like $command->createdAt() of type object<DateTimeImmutable> is incompatible with the declared type integer of property $modifiedBy.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
100
101
        $route = $this->conflictResolver->resolve($this->chainRouteGenerator->generate($translation, $routePath));
102
103
        return $route->getPath();
104
    }
105
106 View Code Duplication
    private function findRoutePathProperty(StructureMetadata $metadata)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
    {
108
        if ($metadata->hasTag(self::TAG_NAME)) {
109
            return $metadata->getPropertyByTagName(self::TAG_NAME);
110
        }
111
112
        return $metadata->getProperty(self::ROUTE_PROPERTY);
113
    }
114
}
115