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

ArticleDocumentProjector   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 100
Duplicated Lines 16 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 12
dl 16
loc 100
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A onCreateTranslation() 9 9 1
A onPublishTranslation() 0 6 1
A onUnpublishTranslation() 0 6 1
A onModifyTranslationStructure() 7 7 1
A onRemoveArticle() 0 7 1
B persistDocument() 0 30 4
A createForm() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sulu\Bundle\ArticleBundle\Prooph\Projection;
6
7
use Sulu\Bundle\ArticleBundle\Controller\ArticleController;
8
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
9
use Sulu\Bundle\ArticleBundle\Prooph\Model\Event\CreateTranslation;
10
use Sulu\Bundle\ArticleBundle\Prooph\Model\Event\ModifyTranslationStructure;
11
use Sulu\Bundle\ArticleBundle\Prooph\Model\Event\PublishTranslation;
12
use Sulu\Bundle\ArticleBundle\Prooph\Model\Event\RemoveArticle;
13
use Sulu\Bundle\ArticleBundle\Prooph\Model\Event\UnpublishTranslation;
14
use Sulu\Component\Content\Form\Exception\InvalidFormException;
15
use Sulu\Component\DocumentManager\DocumentManagerInterface;
16
use Sulu\Component\DocumentManager\MetadataFactoryInterface;
17
use Symfony\Component\Form\FormFactory;
18
19
class ArticleDocumentProjector
20
{
21
    /**
22
     * @var DocumentManagerInterface
23
     */
24
    private $documentManager;
25
26
    /**
27
     * @var MetadataFactoryInterface
28
     */
29
    private $metadataFactory;
30
31
    /**
32
     * @var FormFactory
33
     */
34
    private $formFactory;
35
36
    public function __construct(DocumentManagerInterface $documentManager, MetadataFactoryInterface $metadataFactory, FormFactory $formFactory)
37
    {
38
        $this->documentManager = $documentManager;
39
        $this->metadataFactory = $metadataFactory;
40
        $this->formFactory = $formFactory;
41
    }
42
43 View Code Duplication
    public function onCreateTranslation(CreateTranslation $event): void
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...
44
    {
45
        /** @var ArticleDocument $document */
46
        $document = $this->documentManager->create(ArticleController::DOCUMENT_TYPE);
47
        $document->setUuid($event->aggregateId());
48
49
        $this->persistDocument($document, $event->requestData(), $event->locale(), $event->createdBy());
50
        $this->documentManager->flush();
51
    }
52
53
    public function onPublishTranslation(PublishTranslation $event): void
54
    {
55
        $document = $this->documentManager->find($event->aggregateId(), $event->locale());
56
        $this->documentManager->publish($document, $event->locale());
57
        $this->documentManager->flush();
58
    }
59
60
    public function onUnpublishTranslation(UnpublishTranslation $event): void
61
    {
62
        $document = $this->documentManager->find($event->aggregateId(), $event->locale());
63
        $this->documentManager->unpublish($document, $event->locale());
64
        $this->documentManager->flush();
65
    }
66
67 View Code Duplication
    public function onModifyTranslationStructure(ModifyTranslationStructure $event): void
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...
68
    {
69
        $document = $this->documentManager->find($event->aggregateId(), $event->locale());
70
71
        $this->persistDocument($document, $event->requestData(), $event->locale(), $event->createdBy());
72
        $this->documentManager->flush();
73
    }
74
75
    public function onRemoveArticle(RemoveArticle $event): void
76
    {
77
        $document = $this->documentManager->find($event->aggregateId());
78
79
        $this->documentManager->remove($document);
80
        $this->documentManager->flush();
81
    }
82
83
    private function persistDocument(ArticleDocument $document, array $data, string $locale, int $userId)
84
    {
85
        $formType = $this->metadataFactory->getMetadataForAlias('article')->getFormType();
86
        $form = $this->createForm(
87
            $formType,
88
            $document,
89
            [
90
                // disable csrf protection, since we can't produce a token, because the form is cached on the client
91
                'csrf_protection' => false,
92
            ]
93
        );
94
        $form->submit($data, false);
95
96
        if (!$form->isValid()) {
97
            throw new InvalidFormException($form);
98
        }
99
100
        if (array_key_exists('author', $data) && null === $data['author']) {
101
            $document->setAuthor(null);
102
        }
103
104
        $this->documentManager->persist(
105
            $document,
106
            $locale,
107
            [
108
                'user' => $userId,
109
                'clear_missing_content' => false,
110
            ]
111
        );
112
    }
113
114
    protected function createForm($type, $data = null, array $options = array())
115
    {
116
        return $this->formFactory->create($type, $data, $options);
117
    }
118
}
119