|
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\CoreBundle\Model\ArticleInterface; |
|
24
|
|
|
use SWP\Bundle\CoreBundle\Model\CompositePublishActionInterface; |
|
25
|
|
|
use SWP\Bundle\CoreBundle\Model\PackageInterface; |
|
26
|
|
|
use SWP\Bundle\CoreBundle\Model\PublishDestinationInterface; |
|
27
|
|
|
use SWP\Bundle\CoreBundle\Model\TenantInterface; |
|
28
|
|
|
use SWP\Component\Bridge\Events; |
|
29
|
|
|
use SWP\Component\MultiTenancy\Context\TenantContextInterface; |
|
30
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
31
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent; |
|
32
|
|
|
|
|
33
|
|
|
final class ArticlePublisher implements ArticlePublisherInterface |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var ArticleRepositoryInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $articleRepository; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var EventDispatcherInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
private $eventDispatcher; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var ArticleFactoryInterface |
|
47
|
|
|
*/ |
|
48
|
|
|
private $articleFactory; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var TenantContextInterface |
|
52
|
|
|
*/ |
|
53
|
|
|
private $tenantContext; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* ArticlePublisher constructor. |
|
57
|
|
|
* |
|
58
|
|
|
* @param ArticleRepositoryInterface $articleRepository |
|
59
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
|
60
|
|
|
* @param ArticleFactoryInterface $articleFactory |
|
61
|
|
|
* @param TenantContextInterface $tenantContext |
|
62
|
|
|
*/ |
|
63
|
|
|
public function __construct( |
|
64
|
|
|
ArticleRepositoryInterface $articleRepository, |
|
65
|
|
|
EventDispatcherInterface $eventDispatcher, |
|
66
|
|
|
ArticleFactoryInterface $articleFactory, |
|
67
|
|
|
TenantContextInterface $tenantContext |
|
68
|
|
|
) { |
|
69
|
|
|
$this->articleRepository = $articleRepository; |
|
70
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
71
|
|
|
$this->articleFactory = $articleFactory; |
|
72
|
|
|
$this->tenantContext = $tenantContext; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* {@inheritdoc} |
|
77
|
|
|
*/ |
|
78
|
|
|
public function unpublish(PackageInterface $package, array $tenants = []) |
|
79
|
|
|
{ |
|
80
|
|
|
foreach ($package->getArticles() as $article) { |
|
81
|
|
|
foreach ($tenants as $tenant) { |
|
82
|
|
|
/* @var TenantInterface $tenant */ |
|
83
|
|
|
$this->tenantContext->setTenant($tenant); |
|
84
|
|
|
if ($article->getTenantCode() === $tenant->getCode()) { |
|
85
|
|
|
$this->eventDispatcher->dispatch(ArticleEvents::UNPUBLISH, new ArticleEvent($article)); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$this->articleRepository->flush(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* {@inheritdoc} |
|
95
|
|
|
*/ |
|
96
|
|
|
public function publish(PackageInterface $package, CompositePublishActionInterface $action) |
|
97
|
|
|
{ |
|
98
|
|
|
/** @var PublishDestinationInterface $destination */ |
|
99
|
|
|
foreach ($action->getDestinations() as $destination) { |
|
100
|
|
|
$this->tenantContext->setTenant($destination->getTenant()); |
|
101
|
|
|
/** @var ArticleInterface $article */ |
|
102
|
|
|
$article = $this->articleFactory->createFromPackage($package); |
|
103
|
|
|
$this->eventDispatcher->dispatch(Events::SWP_VALIDATION, new GenericEvent($article)); |
|
104
|
|
|
|
|
105
|
|
|
if (null !== ($existingArticle = $this->findArticleByTenantAndCode( |
|
106
|
|
|
$destination->getTenant()->getCode(), |
|
107
|
|
|
$article->getCode()) |
|
108
|
|
|
)) { |
|
109
|
|
|
$article->setRoute($destination->getRoute()); |
|
110
|
|
|
$article->setPublishedFBIA($destination->isFbia()); |
|
111
|
|
|
$this->dispatchEvents($existingArticle, $package); |
|
112
|
|
|
|
|
113
|
|
|
continue; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$article->setPackage($package); |
|
117
|
|
|
$article->setRoute($destination->getRoute()); |
|
118
|
|
|
$article->setPublishedFBIA($destination->isFbia()); |
|
119
|
|
|
$this->dispatchEvents($article, $package); |
|
120
|
|
|
$this->articleRepository->persist($article); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$this->articleRepository->flush(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
private function findArticleByTenantAndCode(string $tenantCode, string $code) |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->articleRepository->findOneBy([ |
|
129
|
|
|
'tenantCode' => $tenantCode, |
|
130
|
|
|
'code' => $code, |
|
131
|
|
|
]); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
private function dispatchEvents(ArticleInterface $article, PackageInterface $package) |
|
135
|
|
|
{ |
|
136
|
|
|
$this->eventDispatcher->dispatch(ArticleEvents::PUBLISH, new ArticleEvent($article)); |
|
137
|
|
|
$this->eventDispatcher->dispatch(ArticleEvents::PRE_CREATE, new ArticleEvent($article, $package)); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|