Completed
Push — master ( 8d3dbf...7592af )
by Paweł
62:36
created

updateSubmissionStatus()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.0054

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 8
cts 9
cp 0.8889
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 1
crap 2.0054
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;
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...
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);
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...
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);
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...
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