|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Superdesk Web Publisher Core Bundle. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright 2017 Sourcefabric z.ú. and contributors. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please see the |
|
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
|
12
|
|
|
* |
|
13
|
|
|
* @copyright 2017 Sourcefabric z.ú |
|
14
|
|
|
* @license http://www.superdesk.org/license |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace SWP\Bundle\CoreBundle\Service; |
|
18
|
|
|
|
|
19
|
|
|
use SWP\Bundle\ContentBundle\ArticleEvents; |
|
20
|
|
|
use SWP\Bundle\ContentBundle\Doctrine\ArticleRepositoryInterface; |
|
21
|
|
|
use SWP\Bundle\ContentBundle\Event\ArticleEvent; |
|
22
|
|
|
use SWP\Bundle\ContentBundle\Factory\ArticleFactoryInterface; |
|
23
|
|
|
use SWP\Bundle\ContentListBundle\Services\ContentListServiceInterface; |
|
24
|
|
|
use SWP\Bundle\CoreBundle\Model\ArticleInterface; |
|
25
|
|
|
use SWP\Bundle\CoreBundle\Model\ArticleStatisticsInterface; |
|
26
|
|
|
use SWP\Bundle\CoreBundle\Model\CompositePublishActionInterface; |
|
27
|
|
|
use SWP\Bundle\CoreBundle\Model\PackageInterface; |
|
28
|
|
|
use SWP\Bundle\CoreBundle\Model\PublishDestinationInterface; |
|
29
|
|
|
use SWP\Bundle\CoreBundle\Model\TenantInterface; |
|
30
|
|
|
use SWP\Bundle\CoreBundle\Repository\ContentListItemRepository; |
|
31
|
|
|
use SWP\Component\Bridge\Events; |
|
32
|
|
|
use SWP\Component\ContentList\Model\ContentListInterface; |
|
33
|
|
|
use SWP\Component\ContentList\Repository\ContentListRepositoryInterface; |
|
34
|
|
|
use SWP\Component\MultiTenancy\Context\TenantContextInterface; |
|
35
|
|
|
use SWP\Component\Storage\Factory\FactoryInterface; |
|
36
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
37
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent; |
|
38
|
|
|
|
|
39
|
|
|
final class ArticlePublisher implements ArticlePublisherInterface |
|
40
|
|
|
{ |
|
41
|
|
|
/** |
|
42
|
|
|
* @var ArticleRepositoryInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
private $articleRepository; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var EventDispatcherInterface |
|
48
|
|
|
*/ |
|
49
|
|
|
private $eventDispatcher; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var ArticleFactoryInterface |
|
53
|
|
|
*/ |
|
54
|
|
|
private $articleFactory; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var FactoryInterface |
|
58
|
|
|
*/ |
|
59
|
|
|
private $articleStatisticsFactory; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var TenantContextInterface |
|
63
|
|
|
*/ |
|
64
|
|
|
private $tenantContext; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var ContentListRepositoryInterface |
|
68
|
|
|
*/ |
|
69
|
|
|
private $contentListRepository; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @var ContentListItemRepository |
|
73
|
|
|
*/ |
|
74
|
|
|
private $contentListItemRepository; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @var ContentListServiceInterface |
|
78
|
|
|
*/ |
|
79
|
|
|
private $contentListService; |
|
80
|
|
|
|
|
81
|
|
|
public function __construct( |
|
82
|
|
|
ArticleRepositoryInterface $articleRepository, |
|
83
|
|
|
EventDispatcherInterface $eventDispatcher, |
|
84
|
|
|
ArticleFactoryInterface $articleFactory, |
|
85
|
|
|
FactoryInterface $articleStatisticsFactory, |
|
86
|
|
|
TenantContextInterface $tenantContext, |
|
87
|
|
|
ContentListRepositoryInterface $contentListRepository, |
|
88
|
|
|
ContentListItemRepository $contentListItemRepository, |
|
89
|
|
|
ContentListServiceInterface $contentListService |
|
90
|
|
|
) { |
|
91
|
|
|
$this->articleRepository = $articleRepository; |
|
92
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
93
|
|
|
$this->articleFactory = $articleFactory; |
|
94
|
|
|
$this->articleStatisticsFactory = $articleStatisticsFactory; |
|
95
|
|
|
$this->tenantContext = $tenantContext; |
|
96
|
|
|
$this->contentListRepository = $contentListRepository; |
|
97
|
|
|
$this->contentListItemRepository = $contentListItemRepository; |
|
98
|
|
|
$this->contentListService = $contentListService; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritdoc} |
|
103
|
|
|
*/ |
|
104
|
|
|
public function unpublish(PackageInterface $package, array $tenants = []) |
|
105
|
|
|
{ |
|
106
|
|
|
foreach ($package->getArticles() as $article) { |
|
107
|
|
|
foreach ($tenants as $tenant) { |
|
108
|
|
|
/* @var TenantInterface $tenant */ |
|
109
|
|
|
$this->tenantContext->setTenant($tenant); |
|
110
|
|
View Code Duplication |
if ($article->getTenantCode() === $tenant->getCode()) { |
|
|
|
|
|
|
111
|
|
|
$this->eventDispatcher->dispatch(ArticleEvents::UNPUBLISH, new ArticleEvent($article, null, ArticleEvents::UNPUBLISH)); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$this->articleRepository->flush(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function publish(PackageInterface $package, CompositePublishActionInterface $action) |
|
120
|
|
|
{ |
|
121
|
|
|
$originalRequestTenant = $this->tenantContext->getTenant(); |
|
122
|
|
|
/** @var PublishDestinationInterface $destination */ |
|
123
|
|
|
foreach ($action->getDestinations() as $destination) { |
|
124
|
|
|
$this->tenantContext->setTenant($destination->getTenant()); |
|
125
|
|
|
|
|
126
|
|
|
/* @var ArticleInterface $article */ |
|
127
|
|
|
if (null !== ($article = $this->findArticleByTenantAndCode($destination->getTenant()->getCode(), $package->getGuid()))) { |
|
128
|
|
|
$article->setRoute($destination->getRoute()); |
|
129
|
|
|
$article->setPublishedFBIA($destination->isPublishedFbia()); |
|
130
|
|
|
$article->setPaywallSecured($destination->isPaywallSecured()); |
|
131
|
|
|
$this->addToContentLists($destination->getContentLists(), $article); |
|
132
|
|
|
$this->eventDispatcher->dispatch(Events::SWP_VALIDATION, new GenericEvent($article)); |
|
133
|
|
|
$this->eventDispatcher->dispatch(ArticleEvents::PRE_UPDATE, new ArticleEvent($article, $package, ArticleEvents::PRE_UPDATE)); |
|
134
|
|
|
$this->articleRepository->flush(); |
|
135
|
|
|
$this->eventDispatcher->dispatch(ArticleEvents::POST_UPDATE, new ArticleEvent($article, $package, ArticleEvents::POST_UPDATE)); |
|
136
|
|
|
|
|
137
|
|
|
if ($destination->isPublished()) { |
|
138
|
|
|
$this->eventDispatcher->dispatch(ArticleEvents::PUBLISH, new ArticleEvent($article, $package, ArticleEvents::PUBLISH)); |
|
139
|
|
|
} |
|
140
|
|
|
$this->articleRepository->flush(); |
|
141
|
|
|
|
|
142
|
|
|
continue; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** @var ArticleInterface $article */ |
|
146
|
|
|
$article = $this->articleFactory->createFromPackage($package); |
|
147
|
|
|
/** @var ArticleStatisticsInterface $articleStatistics */ |
|
148
|
|
|
$articleStatistics = $this->articleStatisticsFactory->create(); |
|
149
|
|
|
$articleStatistics->setArticle($article); |
|
150
|
|
|
$this->articleRepository->persist($articleStatistics); |
|
151
|
|
|
$this->eventDispatcher->dispatch(Events::SWP_VALIDATION, new GenericEvent($article)); |
|
152
|
|
|
$article->setPackage($package); |
|
153
|
|
|
$route = $destination->getRoute(); |
|
154
|
|
|
if (null !== $route) { |
|
155
|
|
|
$route->setArticlesUpdatedAt(new \DateTime()); |
|
156
|
|
|
$article->setRoute($route); |
|
157
|
|
|
} |
|
158
|
|
|
$article->setPublishedFBIA($destination->isPublishedFbia()); |
|
159
|
|
|
$article->setPaywallSecured($destination->isPaywallSecured()); |
|
160
|
|
|
$article->setArticleStatistics($articleStatistics); |
|
161
|
|
|
$this->addToContentLists($destination->getContentLists(), $article); |
|
162
|
|
|
$this->articleRepository->persist($article); |
|
163
|
|
|
$this->eventDispatcher->dispatch(ArticleEvents::PRE_CREATE, new ArticleEvent($article, $package, ArticleEvents::PRE_CREATE)); |
|
164
|
|
|
$this->articleRepository->flush(); |
|
165
|
|
|
$this->eventDispatcher->dispatch(ArticleEvents::POST_CREATE, new ArticleEvent($article, $package, ArticleEvents::POST_CREATE)); |
|
166
|
|
|
|
|
167
|
|
|
if ($destination->isPublished()) { |
|
168
|
|
|
$this->eventDispatcher->dispatch(ArticleEvents::PUBLISH, new ArticleEvent($article, $package, ArticleEvents::PUBLISH)); |
|
169
|
|
|
} |
|
170
|
|
|
$this->articleRepository->flush(); |
|
171
|
|
|
} |
|
172
|
|
|
$this->tenantContext->setTenant($originalRequestTenant); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
private function addToContentLists(array $contentListsPositions, ArticleInterface $article): void |
|
176
|
|
|
{ |
|
177
|
|
|
foreach ($contentListsPositions as $contentListsPosition) { |
|
178
|
|
|
$contentList = $this->contentListRepository->findListById($contentListsPosition['id']); |
|
179
|
|
|
if (null === $contentList) { |
|
180
|
|
|
continue; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
$existingItemOnList = $this->contentListItemRepository->findItemByArticleAndList($article, $contentList, ContentListInterface::TYPE_MANUAL); |
|
|
|
|
|
|
184
|
|
|
if (null === $existingItemOnList) { |
|
185
|
|
|
$this->contentListService->addArticleToContentList( |
|
186
|
|
|
$contentList, |
|
187
|
|
|
$article, |
|
188
|
|
|
$contentListsPosition['position'] |
|
189
|
|
|
); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @param string $tenantCode |
|
196
|
|
|
* @param string $code |
|
197
|
|
|
* |
|
198
|
|
|
* @return object |
|
199
|
|
|
*/ |
|
200
|
|
|
private function findArticleByTenantAndCode(string $tenantCode, string $code) |
|
201
|
|
|
{ |
|
202
|
|
|
return $this->articleRepository->findOneBy([ |
|
203
|
|
|
'tenantCode' => $tenantCode, |
|
204
|
|
|
'code' => $code, |
|
205
|
|
|
]); |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
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.