|
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 2015 Sourcefabric z.u. 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 2015 Sourcefabric z.ú |
|
14
|
|
|
* @license http://www.superdesk.org/license |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace SWP\Bundle\CoreBundle\Service; |
|
18
|
|
|
|
|
19
|
|
|
use Facebook\InstantArticles\Client\Client; |
|
20
|
|
|
use Facebook\InstantArticles\Client\InstantArticleStatus; |
|
21
|
|
|
use Facebook\InstantArticles\Elements\InstantArticle; |
|
22
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleInterface; |
|
23
|
|
|
use SWP\Bundle\ContentBundle\Model\RouteInterface; |
|
24
|
|
|
use SWP\Bundle\CoreBundle\Model\FacebookInstantArticlesArticle; |
|
25
|
|
|
use SWP\Bundle\CoreBundle\Model\FacebookInstantArticlesFeedInterface; |
|
26
|
|
|
use SWP\Bundle\CoreBundle\Repository\FacebookInstantArticlesArticleRepositoryInterface; |
|
27
|
|
|
use SWP\Bundle\FacebookInstantArticlesBundle\Manager\FacebookInstantArticlesManagerInterface; |
|
28
|
|
|
use SWP\Bundle\StorageBundle\Doctrine\ORM\EntityRepository; |
|
29
|
|
|
use SWP\Component\Storage\Factory\FactoryInterface; |
|
30
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
31
|
|
|
|
|
32
|
|
|
class FacebookInstantArticlesService implements FacebookInstantArticlesServiceInterface |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var FacebookInstantArticlesManagerInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $facebookInstantArticlesManager; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var FactoryInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $instantArticlesArticleFactory; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var EntityRepository |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $facebookInstantArticlesArticleRepository; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var UrlGeneratorInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $urlGenerator; |
|
53
|
|
|
|
|
54
|
15 |
|
/** |
|
55
|
|
|
* FacebookInstantArticlesService constructor. |
|
56
|
|
|
* |
|
57
|
|
|
* @param FacebookInstantArticlesManagerInterface $facebookInstantArticlesManager |
|
58
|
|
|
* @param FactoryInterface $instantArticlesArticleFactory |
|
59
|
15 |
|
* @param FacebookInstantArticlesArticleRepositoryInterface $facebookInstantArticlesArticleRepository |
|
60
|
15 |
|
*/ |
|
61
|
15 |
|
public function __construct( |
|
62
|
15 |
|
FacebookInstantArticlesManagerInterface $facebookInstantArticlesManager, |
|
63
|
|
|
FactoryInterface $instantArticlesArticleFactory, |
|
64
|
|
|
FacebookInstantArticlesArticleRepositoryInterface $facebookInstantArticlesArticleRepository, |
|
65
|
|
|
UrlGeneratorInterface $urlGenerator |
|
66
|
|
|
) { |
|
67
|
1 |
|
$this->facebookInstantArticlesManager = $facebookInstantArticlesManager; |
|
68
|
|
|
$this->instantArticlesArticleFactory = $instantArticlesArticleFactory; |
|
69
|
|
|
$this->facebookInstantArticlesArticleRepository = $facebookInstantArticlesArticleRepository; |
|
|
|
|
|
|
70
|
|
|
$this->urlGenerator = $urlGenerator; |
|
71
|
|
|
} |
|
72
|
1 |
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
|
|
public function pushInstantArticle( |
|
77
|
|
|
FacebookInstantArticlesFeedInterface $feed, |
|
78
|
|
|
InstantArticle $instantArticle, |
|
79
|
|
|
ArticleInterface $article |
|
80
|
|
|
) { |
|
81
|
|
|
$submissionId = $this->getClient($feed)->importArticle($instantArticle, true); |
|
82
|
|
|
|
|
83
|
|
|
/** @var FacebookInstantArticlesArticle $instantArticleEntity */ |
|
84
|
|
|
$instantArticleEntity = $this->facebookInstantArticlesArticleRepository->findInFeed($feed, $article); |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
if (null === $instantArticleEntity) { |
|
87
|
|
|
$instantArticleEntity = $this->instantArticlesArticleFactory->create(); |
|
88
|
|
|
$instantArticleEntity->setArticle($article); |
|
89
|
|
|
$instantArticleEntity->setFeed($feed); |
|
90
|
|
|
$instantArticleEntity->setStatus('new'); |
|
91
|
1 |
|
} |
|
92
|
|
|
|
|
93
|
|
|
$instantArticleEntity->setSubmissionId((string) $submissionId); |
|
94
|
1 |
|
$this->facebookInstantArticlesArticleRepository->add($instantArticleEntity); |
|
95
|
|
|
} |
|
96
|
1 |
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* {@inheritdoc} |
|
99
|
|
|
*/ |
|
100
|
1 |
|
public function updateSubmissionStatus(string $submissionId) |
|
101
|
1 |
|
{ |
|
102
|
1 |
|
/** @var FacebookInstantArticlesArticle $instantArticleEntity */ |
|
103
|
1 |
|
$instantArticle = $this->facebookInstantArticlesArticleRepository->findSubmission($submissionId); |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
1 |
|
if (null === $instantArticle) { |
|
106
|
|
|
throw new \Exception('Instant Article with provided submission ID does not exists.'); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$submissionStatus = $this->getClient($instantArticle->getFeed())->getSubmissionStatus($submissionId); |
|
110
|
|
|
$instantArticle->setStatus($submissionStatus->getStatus()); |
|
111
|
|
|
$instantArticle->setErrors($this->getSubmissionErrors($submissionStatus)); |
|
112
|
|
|
$this->facebookInstantArticlesArticleRepository->flush(); |
|
113
|
|
|
|
|
114
|
|
|
return $instantArticle; |
|
115
|
1 |
|
} |
|
116
|
|
|
|
|
117
|
1 |
|
/** |
|
118
|
|
|
* {@inheritdoc} |
|
119
|
1 |
|
*/ |
|
120
|
1 |
|
public function removeInstantArticle(FacebookInstantArticlesFeedInterface $feed, ArticleInterface $article) |
|
121
|
|
|
{ |
|
122
|
|
|
if ($article->getRoute() instanceof RouteInterface) { |
|
123
|
|
|
$url = $this->urlGenerator->generate($article->getRoute(), ['slug' => $article->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL); |
|
|
|
|
|
|
124
|
|
|
$status = $this->getClient($feed)->removeArticle($url); |
|
125
|
|
|
|
|
126
|
|
|
return $status; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param $feed |
|
134
|
1 |
|
* |
|
135
|
|
|
* @return Client |
|
136
|
1 |
|
* |
|
137
|
1 |
|
* @throws \Exception |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function getClient($feed) |
|
140
|
|
|
{ |
|
141
|
1 |
|
$facebookPage = $feed->getFacebookPage(); |
|
142
|
|
|
|
|
143
|
|
|
if (null === $facebookPage->getApplication()) { |
|
144
|
|
|
throw new \Exception('Page is not authorized to publish Instant Articles', 403); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$facebook = $this->facebookInstantArticlesManager->getFacebookManager()->createForApp($facebookPage->getApplication()); |
|
148
|
|
|
$facebook->setDefaultAccessToken($facebookPage->getAccessToken()); |
|
149
|
|
|
|
|
150
|
|
|
return new Client($facebook, $facebookPage->getPageId(), $feed->isDevelopment()); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param InstantArticleStatus $submissionStatus |
|
155
|
|
|
* |
|
156
|
|
|
* @return array |
|
157
|
|
|
*/ |
|
158
|
|
|
private function getSubmissionErrors(InstantArticleStatus $submissionStatus): array |
|
159
|
|
|
{ |
|
160
|
|
|
$errors = []; |
|
161
|
|
|
foreach ($submissionStatus->getMessages() as $serverMessage) { |
|
162
|
|
|
$errors[] = [$serverMessage->getLevel() => $serverMessage->getMessage()]; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return $errors; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..