|
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 2018 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 2018 Sourcefabric z.ú |
|
14
|
|
|
* @license http://www.superdesk.org/license |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace SWP\Bundle\CoreBundle\Adapter; |
|
18
|
|
|
|
|
19
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
20
|
|
|
use GuzzleHttp\ClientInterface; |
|
21
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
22
|
|
|
use GuzzleHttp\Psr7\Response as GuzzleResponse; |
|
23
|
|
|
use SWP\Bundle\ContentBundle\Manager\MediaManagerInterface; |
|
24
|
|
|
use SWP\Bundle\CoreBundle\Model\ArticleInterface; |
|
25
|
|
|
use SWP\Bundle\CoreBundle\Model\ExternalArticle; |
|
26
|
|
|
use SWP\Bundle\CoreBundle\Model\ExternalArticleInterface; |
|
27
|
|
|
use SWP\Bundle\CoreBundle\Model\OutputChannelInterface; |
|
28
|
|
|
use SWP\Bundle\CoreBundle\OutputChannel\External\Wordpress\Post; |
|
29
|
|
|
use SWP\Bundle\CoreBundle\OutputChannel\External\Wordpress\PostInterface; |
|
30
|
|
|
use SWP\Component\Storage\Repository\RepositoryInterface; |
|
31
|
|
|
use Symfony\Component\Serializer\Encoder\JsonEncoder; |
|
32
|
|
|
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter; |
|
33
|
|
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; |
|
34
|
|
|
use Symfony\Component\Serializer\Serializer; |
|
35
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
|
36
|
|
|
|
|
37
|
|
|
final class WordpressAdapter implements AdapterInterface |
|
38
|
|
|
{ |
|
39
|
|
|
public const STATUS_DRAFT = 'draft'; |
|
40
|
|
|
|
|
41
|
|
|
public const STATUS_PUBLISHED = 'publish'; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var ClientInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
private $client; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var RepositoryInterface |
|
50
|
|
|
*/ |
|
51
|
|
|
private $externalArticleRepository; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var EntityManagerInterface |
|
55
|
|
|
*/ |
|
56
|
|
|
private $externalArticleManager; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var MediaManagerInterface |
|
60
|
|
|
*/ |
|
61
|
|
|
private $mediaManager; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* WordpressAdapter constructor. |
|
65
|
|
|
* |
|
66
|
|
|
* @param ClientInterface $client |
|
67
|
|
|
* @param RepositoryInterface $externalArticleRepository |
|
68
|
|
|
* @param EntityManagerInterface $externalArticleManager |
|
69
|
|
|
* @param MediaManagerInterface $mediaManager |
|
70
|
|
|
*/ |
|
71
|
|
|
public function __construct( |
|
72
|
|
|
ClientInterface $client, |
|
73
|
|
|
RepositoryInterface $externalArticleRepository, |
|
74
|
|
|
EntityManagerInterface $externalArticleManager, |
|
75
|
|
|
MediaManagerInterface $mediaManager |
|
76
|
|
|
) { |
|
77
|
|
|
$this->client = $client; |
|
78
|
|
|
$this->externalArticleRepository = $externalArticleRepository; |
|
79
|
|
|
$this->externalArticleManager = $externalArticleManager; |
|
80
|
|
|
$this->mediaManager = $mediaManager; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* {@inheritdoc} |
|
85
|
|
|
*/ |
|
86
|
|
|
public function create(OutputChannelInterface $outputChannel, ArticleInterface $article): void |
|
87
|
|
|
{ |
|
88
|
|
|
$this->handleArticleCreate($outputChannel, $article); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritdoc} |
|
93
|
|
|
*/ |
|
94
|
|
|
public function update(OutputChannelInterface $outputChannel, ArticleInterface $article): void |
|
95
|
|
|
{ |
|
96
|
|
|
$externalArticle = $this->getExternalArticle($article, $outputChannel); |
|
97
|
|
|
$this->handleArticleUpdate($outputChannel, $article, $externalArticle->getStatus()); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
|
|
public function publish(OutputChannelInterface $outputChannel, ArticleInterface $article): void |
|
104
|
|
|
{ |
|
105
|
|
|
$this->handleArticleUpdate($outputChannel, $article, self::STATUS_PUBLISHED); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* {@inheritdoc} |
|
110
|
|
|
*/ |
|
111
|
|
|
public function unpublish(OutputChannelInterface $outputChannel, ArticleInterface $article): void |
|
112
|
|
|
{ |
|
113
|
|
|
$this->handleArticleUpdate($outputChannel, $article, self::STATUS_DRAFT); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* {@inheritdoc} |
|
118
|
|
|
*/ |
|
119
|
|
|
public function supports(OutputChannelInterface $outputChannel): bool |
|
120
|
|
|
{ |
|
121
|
|
|
return OutputChannelInterface::TYPE_WORDPRESS === $outputChannel->getType(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
private function handleArticleCreate(OutputChannelInterface $outputChannel, ArticleInterface $article): ExternalArticleInterface |
|
125
|
|
|
{ |
|
126
|
|
|
$post = $this->createPost($outputChannel, $article); |
|
127
|
|
|
$post->setStatus(self::STATUS_DRAFT); |
|
128
|
|
|
$response = $this->send($outputChannel, 'posts', $post); |
|
129
|
|
|
|
|
130
|
|
|
return $this->handleExternalArticleUpdateOrCreate($article, $response, $article->getExternalArticle()); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param OutputChannelInterface $outputChannel |
|
135
|
|
|
* @param ArticleInterface $article |
|
136
|
|
|
* @param string $status |
|
137
|
|
|
*/ |
|
138
|
|
|
private function handleArticleUpdate(OutputChannelInterface $outputChannel, ArticleInterface $article, string $status): void |
|
139
|
|
|
{ |
|
140
|
|
|
$post = $this->createPost($outputChannel, $article); |
|
141
|
|
|
$post->setStatus($status); |
|
142
|
|
|
$externalArticle = $this->getExternalArticle($article, $outputChannel); |
|
143
|
|
|
$response = $this->send($outputChannel, sprintf('posts/%s', $externalArticle->getExternalId()), $post); |
|
144
|
|
|
|
|
145
|
|
|
if (self::STATUS_PUBLISHED === $status && null === $externalArticle->getPublishedAt()) { |
|
146
|
|
|
$externalArticle->setPublishedAt(new \DateTime()); |
|
147
|
|
|
} elseif (self::STATUS_DRAFT === $status && $externalArticle->getPublishedAt() instanceof \DateTime) { |
|
148
|
|
|
$externalArticle->setUnpublishedAt(new \DateTime()); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$this->handleExternalArticleUpdateOrCreate($article, $response, $externalArticle); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
private function getExternalArticle(ArticleInterface $article, OutputChannelInterface $outputChannel): ExternalArticleInterface |
|
155
|
|
|
{ |
|
156
|
|
|
$externalArticle = $article->getExternalArticle(); |
|
157
|
|
|
if (null === $externalArticle) { |
|
158
|
|
|
return $this->handleArticleCreate($outputChannel, $article); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return $externalArticle; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
private function handleExternalArticleUpdateOrCreate(ArticleInterface $article, GuzzleResponse $response, ExternalArticleInterface $externalArticle = null): ExternalArticleInterface |
|
165
|
|
|
{ |
|
166
|
|
|
$responseData = \json_decode($response->getBody()->getContents(), true); |
|
167
|
|
|
if (null === $externalArticle) { |
|
168
|
|
|
$externalArticle = new ExternalArticle($article, (string) $responseData['id'], 'draft'); |
|
169
|
|
|
} |
|
170
|
|
|
$article->setExternalArticle($externalArticle); |
|
171
|
|
|
if (isset($responseData['link'])) { |
|
172
|
|
|
$externalArticle->setLiveUrl($responseData['link']); |
|
173
|
|
|
} |
|
174
|
|
|
if (null !== $responseData['featured_media']) { |
|
175
|
|
|
$externalArticle->setExtra(['featured_media' => $responseData['featured_media']]); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
if (200 === $response->getStatusCode()) { |
|
179
|
|
|
$externalArticle->setStatus($responseData['status']); |
|
180
|
|
|
$externalArticle->setUpdatedAt(new \DateTime()); |
|
181
|
|
|
$this->externalArticleManager->flush(); |
|
182
|
|
|
} elseif (201 === $response->getStatusCode()) { |
|
183
|
|
|
$this->externalArticleRepository->add($externalArticle); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
return $externalArticle; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @param OutputChannelInterface $outputChannel |
|
191
|
|
|
* @param ArticleInterface $article |
|
192
|
|
|
* |
|
193
|
|
|
* @return Post |
|
194
|
|
|
*/ |
|
195
|
|
|
private function createPost(OutputChannelInterface $outputChannel, ArticleInterface $article): Post |
|
196
|
|
|
{ |
|
197
|
|
|
$post = new Post(); |
|
198
|
|
|
$post->setTitle($article->getTitle()); |
|
199
|
|
|
$post->setContent($article->getBody()); |
|
200
|
|
|
$post->setSlug($article->getSlug()); |
|
201
|
|
|
$post->setType(PostInterface::TYPE_STANDARD); |
|
202
|
|
|
|
|
203
|
|
|
if (null !== $featureMedia = $article->getFeatureMedia()) { |
|
204
|
|
|
$image = $featureMedia->getImage(); |
|
205
|
|
|
$edge = 'media'; |
|
206
|
|
|
$externalArticle = $article->getExternalArticle(); |
|
207
|
|
|
if (null !== $externalArticle) { |
|
208
|
|
|
if (null !== $featuredMediaId = $externalArticle->getExtra()['featured_media']) { |
|
209
|
|
|
$edge .= '/'.$featuredMediaId; |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
try { |
|
214
|
|
|
$response = $this->send( |
|
215
|
|
|
$outputChannel, |
|
216
|
|
|
$edge, |
|
217
|
|
|
new Post(), |
|
218
|
|
|
[ |
|
219
|
|
|
'headers' => [ |
|
220
|
|
|
'Content-Type' => $featureMedia->getMimetype(), |
|
221
|
|
|
'Content-Disposition' => 'attachment; filename="'.$image->getAssetId().'.'.$image->getFileExtension().'"', |
|
222
|
|
|
], |
|
223
|
|
|
'body' => $this->mediaManager->getFile($article->getFeatureMedia()->getImage()), |
|
224
|
|
|
'timeout' => 5, |
|
225
|
|
|
] |
|
226
|
|
|
); |
|
227
|
|
|
$decodedBody = \json_decode($response->getBody()->getContents(), true); |
|
228
|
|
|
$post->setFeaturedMedia($decodedBody['id']); |
|
229
|
|
|
} catch (RequestException $e) { |
|
230
|
|
|
// ignore feature media |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
$post->setTags($article->getKeywords()); |
|
235
|
|
|
|
|
236
|
|
|
return $post; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @param OutputChannelInterface $outputChannel |
|
241
|
|
|
* @param string $endpoint |
|
242
|
|
|
* @param Post $post |
|
243
|
|
|
* @param array|null $requestOptions |
|
244
|
|
|
* |
|
245
|
|
|
* @return GuzzleResponse |
|
246
|
|
|
*/ |
|
247
|
|
|
private function send(OutputChannelInterface $outputChannel, string $endpoint, Post $post, array $requestOptions = null): GuzzleResponse |
|
248
|
|
|
{ |
|
249
|
|
|
$url = $outputChannel->getConfig()['url']; |
|
250
|
|
|
$authorizationKey = $outputChannel->getConfig()['authorization_key']; |
|
251
|
|
|
|
|
252
|
|
|
if (null === $requestOptions) { |
|
253
|
|
|
$requestOptions = [ |
|
254
|
|
|
'headers' => [ |
|
255
|
|
|
'Content-Type' => 'application/json', |
|
256
|
|
|
], |
|
257
|
|
|
'body' => $this->getSerializer()->serialize($post, 'json'), |
|
258
|
|
|
'timeout' => 5, |
|
259
|
|
|
]; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
if (isset($requestOptions['headers'])) { |
|
263
|
|
|
$requestOptions['headers']['Authorization'] = $authorizationKey; |
|
264
|
|
|
} |
|
265
|
|
|
/** @var \GuzzleHttp\Psr7\Response $response */ |
|
266
|
|
|
$response = $this->client->post($url.'/wp-json/wp/v2/'.$endpoint, $requestOptions); |
|
267
|
|
|
|
|
268
|
|
|
return $response; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* @return SerializerInterface |
|
273
|
|
|
*/ |
|
274
|
|
|
private function getSerializer(): SerializerInterface |
|
275
|
|
|
{ |
|
276
|
|
|
$encoders = [new JsonEncoder()]; |
|
277
|
|
|
$normalizer = new ObjectNormalizer(null, new CamelCaseToSnakeCaseNameConverter()); |
|
278
|
|
|
$normalizers = [$normalizer]; |
|
279
|
|
|
|
|
280
|
|
|
return new Serializer($normalizers, $encoders); |
|
281
|
|
|
} |
|
282
|
|
|
} |
|
283
|
|
|
|