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\CoreBundle\Model\FacebookInstantArticlesArticle; |
24
|
|
|
use SWP\Bundle\CoreBundle\Model\FacebookInstantArticlesFeedInterface; |
25
|
|
|
use SWP\Bundle\CoreBundle\Repository\FacebookInstantArticlesArticleRepositoryInterface; |
26
|
|
|
use SWP\Bundle\FacebookInstantArticlesBundle\Manager\FacebookInstantArticlesManagerInterface; |
27
|
|
|
use SWP\Bundle\StorageBundle\Doctrine\ORM\EntityRepository; |
28
|
|
|
use SWP\Component\Storage\Factory\FactoryInterface; |
29
|
|
|
|
30
|
|
|
class FacebookInstantArticlesService implements FacebookInstantArticlesServiceInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var FacebookInstantArticlesManagerInterface |
34
|
|
|
*/ |
35
|
|
|
protected $facebookInstantArticlesManager; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var FactoryInterface |
39
|
|
|
*/ |
40
|
|
|
protected $instantArticlesArticleFactory; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var EntityRepository |
44
|
|
|
*/ |
45
|
|
|
protected $facebookInstantArticlesArticleRepository; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* FacebookInstantArticlesService constructor. |
49
|
|
|
* |
50
|
|
|
* @param FacebookInstantArticlesManagerInterface $facebookInstantArticlesManager |
51
|
|
|
* @param FactoryInterface $instantArticlesArticleFactory |
52
|
|
|
* @param FacebookInstantArticlesArticleRepositoryInterface $facebookInstantArticlesArticleRepository |
53
|
|
|
*/ |
54
|
14 |
|
public function __construct( |
55
|
|
|
FacebookInstantArticlesManagerInterface $facebookInstantArticlesManager, |
56
|
|
|
FactoryInterface $instantArticlesArticleFactory, |
57
|
|
|
FacebookInstantArticlesArticleRepositoryInterface $facebookInstantArticlesArticleRepository |
58
|
|
|
) { |
59
|
14 |
|
$this->facebookInstantArticlesManager = $facebookInstantArticlesManager; |
60
|
14 |
|
$this->instantArticlesArticleFactory = $instantArticlesArticleFactory; |
61
|
14 |
|
$this->facebookInstantArticlesArticleRepository = $facebookInstantArticlesArticleRepository; |
|
|
|
|
62
|
14 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
1 |
|
public function pushInstantArticle( |
68
|
|
|
FacebookInstantArticlesFeedInterface $feed, |
69
|
|
|
InstantArticle $instantArticle, |
70
|
|
|
ArticleInterface $article |
71
|
|
|
) { |
72
|
1 |
|
$submissionId = $this->getClient($feed)->importArticle($instantArticle, true); |
73
|
|
|
|
74
|
|
|
/** @var FacebookInstantArticlesArticle $instantArticleEntity */ |
75
|
|
|
$instantArticleEntity = $this->facebookInstantArticlesArticleRepository->findInFeed($feed, $article); |
|
|
|
|
76
|
|
|
|
77
|
|
|
if (null === $instantArticleEntity) { |
78
|
|
|
$instantArticleEntity = $this->instantArticlesArticleFactory->create(); |
79
|
|
|
$instantArticleEntity->setArticle($article); |
80
|
|
|
$instantArticleEntity->setFeed($feed); |
81
|
|
|
$instantArticleEntity->setStatus('new'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$instantArticleEntity->setSubmissionId((string) $submissionId); |
85
|
|
|
$this->facebookInstantArticlesArticleRepository->add($instantArticleEntity); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
1 |
|
public function updateSubmissionStatus(string $submissionId) |
92
|
|
|
{ |
93
|
|
|
/** @var FacebookInstantArticlesArticle $instantArticleEntity */ |
94
|
1 |
|
$instantArticle = $this->facebookInstantArticlesArticleRepository->findSubmission($submissionId); |
|
|
|
|
95
|
|
|
|
96
|
1 |
|
if (null === $instantArticle) { |
97
|
|
|
throw new \Exception('Instant Article with provided submission ID does not exists.'); |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
$submissionStatus = $this->getClient($instantArticle->getFeed())->getSubmissionStatus($submissionId); |
101
|
1 |
|
$instantArticle->setStatus($submissionStatus->getStatus()); |
102
|
1 |
|
$instantArticle->setErrors($this->getSubmissionErrors($submissionStatus)); |
103
|
1 |
|
$this->facebookInstantArticlesArticleRepository->flush(); |
104
|
|
|
|
105
|
1 |
|
return $instantArticle; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param $feed |
110
|
|
|
* |
111
|
|
|
* @return Client |
112
|
|
|
* |
113
|
|
|
* @throws \Exception |
114
|
|
|
*/ |
115
|
1 |
|
protected function getClient($feed) |
116
|
|
|
{ |
117
|
1 |
|
$facebookPage = $feed->getFacebookPage(); |
118
|
|
|
|
119
|
1 |
|
if (null === $facebookPage->getApplication()) { |
120
|
1 |
|
throw new \Exception('Page is not authorized to publish Instant Articles', 403); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$facebook = $this->facebookInstantArticlesManager->getFacebookManager()->createForApp($facebookPage->getApplication()); |
124
|
|
|
$facebook->setDefaultAccessToken($facebookPage->getAccessToken()); |
125
|
|
|
|
126
|
|
|
return new Client($facebook, $facebookPage->getPageId(), $feed->isDevelopment()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param InstantArticleStatus $submissionStatus |
131
|
|
|
* |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
1 |
|
private function getSubmissionErrors(InstantArticleStatus $submissionStatus): array |
135
|
|
|
{ |
136
|
1 |
|
$errors = []; |
137
|
1 |
|
foreach ($submissionStatus->getMessages() as $serverMessage) { |
138
|
|
|
$errors[] = [$serverMessage->getLevel() => $serverMessage->getMessage()]; |
139
|
|
|
} |
140
|
|
|
|
141
|
1 |
|
return $errors; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
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..