|
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 |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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
|
|
|
|
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.