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

PutArticleHandler::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 15

Duplication

Lines 5
Ratio 22.73 %

Importance

Changes 0
Metric Value
dl 5
loc 22
rs 9.2
c 0
b 0
f 0
cc 3
eloc 15
nc 3
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\Command\PutArticleCommand;
9
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;
10
11
class PutArticleHandler
12
{
13
    /**
14
     * @var ArticleRepositoryInterface
15
     */
16
    private $repository;
17
18
    /**
19
     * @var StructureMetadataFactoryInterface
20
     */
21
    private $metadataFactory;
22
23
    public function __construct(ArticleRepositoryInterface $repository, StructureMetadataFactoryInterface $metadataFactory)
24
    {
25
        $this->repository = $repository;
26
        $this->metadataFactory = $metadataFactory;
27
    }
28
29
    public function __invoke(PutArticleCommand $command): void
30
    {
31
        $structureType = $command->requestData()['template'];
32
        $metadata = $this->metadataFactory->getStructureMetadata('article', $structureType);
33
34
        $structureData = [];
35 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...
36
            if (array_key_exists($property->getName(), $command->requestData())) {
37
                $structureData[$property->getName()] = $command->requestData()[$property->getName()];
38
            }
39
        }
40
41
        $article = $this->repository->get($command->id());
42
        $article = $article->modifyTranslationStructure(
43
            $command->locale(),
44
            $structureType,
45
            $structureData,
46
            $command->userId(),
47
            $command->requestData()
48
        );
49
        $this->repository->save($article);
50
    }
51
}
52