Completed
Pull Request — 2.1 (#1090)
by Paweł
09:07
created

AppleNewsPublisher::publish()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 9.408
c 0
b 0
f 0
cc 2
nc 2
nop 2
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 2020 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 2020 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\AppleNews;
18
19
use SWP\Bundle\CoreBundle\AppleNews\Api\AppleNewsApi;
20
use SWP\Bundle\CoreBundle\AppleNews\Api\ClientFactory;
21
use SWP\Bundle\CoreBundle\AppleNews\Api\Response\AppleNewsArticle;
22
use SWP\Bundle\CoreBundle\AppleNews\Converter\ArticleToAppleNewsFormatConverter;
23
use SWP\Bundle\CoreBundle\Model\AppleNewsConfigInterface;
24
use SWP\Bundle\CoreBundle\Model\ArticleInterface;
25
use SWP\Bundle\CoreBundle\Model\TenantInterface;
26
use SWP\Bundle\CoreBundle\Model\AppleNewsArticle as PublisherAppleNewsArticle;
27
28
class AppleNewsPublisher
29
{
30
    private $appleNewsConverter;
31
32
    public function __construct(ArticleToAppleNewsFormatConverter $appleNewsConverter)
33
    {
34
        $this->appleNewsConverter = $appleNewsConverter;
35
    }
36
37
    public function publish(ArticleInterface $article, TenantInterface $tenant): AppleNewsArticle
38
    {
39
        $appleNewsConfig = $tenant->getAppleNewsConfig();
40
        $appleNewsClient = $this->getClient($appleNewsConfig);
41
        $appleNewsArticle = $article->getAppleNewsArticle();
42
43
        $json = $this->appleNewsConverter->convert($article);
44
45
        if (null === $appleNewsArticle) {
46
            $rawAppleNewsArticle = $appleNewsClient->createArticle($appleNewsConfig->getChannelId(), $json);
47
48
            $appleNewsArticle = new PublisherAppleNewsArticle();
49
            $appleNewsArticle->setShareUrl($rawAppleNewsArticle->getShareUrl());
50
            $appleNewsArticle->setArticleId($rawAppleNewsArticle->getArticleId());
51
            $appleNewsArticle->setRevisionId($rawAppleNewsArticle->getRevisionId());
52
            $article->setAppleNewsArticle($appleNewsArticle);
53
54
            return $rawAppleNewsArticle;
55
        }
56
57
        $rawAppleNewsArticle = $appleNewsClient->updateArticle(
58
            $appleNewsArticle->getArticleId(),
59
            $json,
60
            ['revision' => $appleNewsArticle->getRevisionId()]
61
        );
62
63
        $appleNewsArticle->setShareUrl($rawAppleNewsArticle->getShareUrl());
64
        $appleNewsArticle->setArticleId($rawAppleNewsArticle->getArticleId());
65
        $appleNewsArticle->setRevisionId($rawAppleNewsArticle->getRevisionId());
66
67
        return $rawAppleNewsArticle;
68
    }
69
70
    public function unpublish(ArticleInterface $article, TenantInterface $tenant): void
71
    {
72
        $appleNewsConfig = $tenant->getAppleNewsConfig();
73
        $appleNewsArticle = $article->getAppleNewsArticle();
74
        $appleNewsClient = $this->getClient($appleNewsConfig);
75
76
        $appleNewsClient->deleteArticle($appleNewsArticle->getArticleId());
77
    }
78
79
    private function getClient(AppleNewsConfigInterface $appleNewsConfig): AppleNewsApi
80
    {
81
        $clientFactory = new ClientFactory();
82
83
        return new AppleNewsApi($clientFactory->create(), $appleNewsConfig->getApiKeyId(), $appleNewsConfig->getApiKeySecret());
84
    }
85
}
86