Completed
Push — master ( 30fa97...3ef484 )
by Paweł
14s
created

removeInstantArticle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 3.6875

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
ccs 1
cts 4
cp 0.25
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
crap 3.6875
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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $facebookInstantArticlesArticleRepository of type object<SWP\Bundle\CoreBu...cleRepositoryInterface> is incompatible with the declared type object<SWP\Bundle\Storag...e\ORM\EntityRepository> of property $facebookInstantArticlesArticleRepository.

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..

Loading history...
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);
0 ignored issues
show
Documentation Bug introduced by
The method findInFeed does not exist on object<SWP\Bundle\Storag...e\ORM\EntityRepository>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
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);
0 ignored issues
show
Documentation Bug introduced by
The method findSubmission does not exist on object<SWP\Bundle\Storag...e\ORM\EntityRepository>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
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);
1 ignored issue
show
Documentation introduced by
$article->getRoute() is of type object<SWP\Bundle\Conten...e\Model\RouteInterface>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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