Completed
Push — 2.1 ( 63ded9...b14dac )
by Paweł
23s queued 11s
created

configureComponentLayouts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\Converter;
18
19
use SWP\Bundle\ContentBundle\Model\SlideshowItemInterface;
20
use SWP\Bundle\CoreBundle\AppleNews\Component\Caption;
21
use SWP\Bundle\CoreBundle\AppleNews\Component\Gallery;
22
use SWP\Bundle\CoreBundle\AppleNews\Component\GalleryItem;
23
use SWP\Bundle\CoreBundle\AppleNews\Component\Intro;
24
use SWP\Bundle\CoreBundle\AppleNews\Component\Photo;
25
use SWP\Bundle\CoreBundle\AppleNews\Component\Title;
26
use SWP\Bundle\CoreBundle\AppleNews\Document\ArticleDocument;
27
use SWP\Bundle\CoreBundle\AppleNews\Document\ComponentLayout;
28
use SWP\Bundle\CoreBundle\AppleNews\Document\ComponentLayouts;
29
use SWP\Bundle\CoreBundle\AppleNews\Document\ComponentTextStyle;
30
use SWP\Bundle\CoreBundle\AppleNews\Document\ComponentTextStyles;
31
use SWP\Bundle\CoreBundle\AppleNews\Document\Layout;
32
use SWP\Bundle\CoreBundle\AppleNews\Document\LinkedArticle;
33
use SWP\Bundle\CoreBundle\AppleNews\Document\Margin;
34
use SWP\Bundle\CoreBundle\AppleNews\Document\Metadata;
35
use SWP\Bundle\CoreBundle\AppleNews\Document\TextStyle;
36
use SWP\Bundle\CoreBundle\AppleNews\Serializer\AppleNewsFormatSerializer;
37
use SWP\Bundle\CoreBundle\Factory\VersionFactory;
38
use SWP\Bundle\CoreBundle\Model\ArticleInterface;
39
use Symfony\Component\Routing\RouterInterface;
40
41
final class ArticleToAppleNewsFormatConverter
42
{
43
    private $serializer;
44
45
    private $router;
46
47
    private $versionFactory;
48
49
    private $articleBodyConverter;
50
51
    public function __construct(
52
        VersionFactory $versionFactory,
53
        AppleNewsFormatSerializer $serializer,
54
        RouterInterface $router,
55
        ArticleBodyToComponentsConverter $articleBodyConverter
56
    ) {
57
        $this->versionFactory = $versionFactory;
58
        $this->serializer = $serializer;
59
        $this->router = $router;
60
        $this->articleBodyConverter = $articleBodyConverter;
61
    }
62
63
    public function convert(ArticleInterface $article): string
64
    {
65
        $version = $this->versionFactory->create();
66
67
        $metadata = new Metadata();
68
        $articleDocument = new ArticleDocument();
69
        $articleDocument->setTitle($article->getTitle());
70
        $articleDocument->setIdentifier((string) $article->getId());
71
        $articleDocument->setLanguage($article->getLocale());
72
73
        $articleDocument->addComponent(new Title($article->getTitle(), 'halfMarginBelowLayout'));
74
        $articleDocument->addComponent(new Intro($article->getLead(), 'halfMarginBelowLayout'));
75
76
        $featureMedia = $article->getFeatureMedia();
77
78
        if (null !== $featureMedia) {
79
            $featureMediaUrl = $this->router->generate('swp_media_get', [
80
                'mediaId' => $featureMedia->getImage()->getAssetId(),
81
                'extension' => $featureMedia->getImage()->getFileExtension(),
82
            ], RouterInterface::ABSOLUTE_URL);
83
84
            $articleDocument->addComponent(new Photo($featureMediaUrl, (string) $featureMedia->getDescription()));
85
            $articleDocument->addComponent(new Caption($featureMedia->getDescription(), 'marginBetweenComponents'));
86
            $metadata->setThumbnailURL($featureMediaUrl);
87
        }
88
89
        $components = $this->articleBodyConverter->convert($article->getBody());
90
        $components = $this->processGalleries($components, $article);
91
        $links = $this->processRelatedArticles($article);
92
93
        foreach ($components as $component) {
94
            $articleDocument->addComponent($component);
95
        }
96
97
        $articleDocument->setLayout(new Layout(20, 1024, 20, 60));
98
99
        $componentTextStyles = $this->configureComponentTextStyles();
100
        $articleDocument->setComponentTextStyles($componentTextStyles);
101
102
        $componentLayouts = $this->configureComponentLayouts();
103
        $articleDocument->setComponentLayouts($componentLayouts);
104
105
        $metadata->setAuthors($article->getAuthorsNames());
106
107
        $canonicalUrl = $this->router->generate($article->getRoute()->getRouteName(), [
108
            'slug' => $article->getSlug(),
109
        ], RouterInterface::ABSOLUTE_URL);
110
111
        $metadata->setCanonicalUrl($canonicalUrl);
112
        $metadata->setDateCreated($article->getCreatedAt());
113
        $metadata->setDatePublished($article->getPublishedAt());
114
115
        $metadata->setExcerpt($article->getLead() ?? '');
116
117
        $metadata->setGeneratorIdentifier('publisher');
118
        $metadata->setGeneratorName('Publisher');
119
        $metadata->setGeneratorVersion($version->getVersion());
120
121
        $metadata->setKeywords($article->getKeywordsNames());
122
        $metadata->setLinks($links);
123
124
        $articleDocument->setMetadata($metadata);
125
126
        return $this->serializer->serialize($articleDocument);
127
    }
128
129
    private function processGalleries(array $components, ArticleInterface $article): array
130
    {
131
        if ($article->getSlideshows()->count() > 0) {
132
            foreach ($article->getSlideshows() as $slideshow) {
133
                $galleryComponent = new Gallery();
134
                /** @var SlideshowItemInterface $slideshowItem */
135
                foreach ($slideshow->getItems() as $slideshowItem) {
136
                    $media = $slideshowItem->getArticleMedia();
137
                    $caption = $media->getDescription();
138
                    $url = $this->router->generate('swp_media_get', [
139
                        'mediaId' => $media->getImage()->getAssetId(),
140
                        'extension' => $media->getImage()->getFileExtension(),
141
                    ], RouterInterface::ABSOLUTE_URL);
142
143
                    $galleryItem = new GalleryItem($url, $caption);
144
                    $galleryComponent->addItem($galleryItem);
145
                }
146
147
                $components[] = $galleryComponent;
148
            }
149
        }
150
151
        return $components;
152
    }
153
154
    private function processRelatedArticles(ArticleInterface $article): array
155
    {
156
        $links = [];
157
        if ($article->getRelatedArticles()->count() > 0) {
158
            foreach ($article->getRelatedArticles() as $relatedArticle) {
159
                $relatedArticleRoute = $relatedArticle->getArticle()->getRoute();
160
                $url = $this->router->generate($relatedArticleRoute->getRouteName(), [
161
                    'slug' => $relatedArticle->getArticle()->getSlug(),
162
                ], RouterInterface::ABSOLUTE_URL);
163
                $linkedArticle = new LinkedArticle($url);
0 ignored issues
show
Bug introduced by
The call to LinkedArticle::__construct() misses a required argument $relationship.

This check looks for function calls that miss required arguments.

Loading history...
164
                $links[] = $linkedArticle;
165
            }
166
        }
167
168
        return $links;
169
    }
170
171
    private function configureComponentTextStyles(): ComponentTextStyles
172
    {
173
        $linkStyle = new TextStyle('#8a0b1f');
174
        $componentTextStyles = new ComponentTextStyles();
175
        $componentTextStylesBody = new ComponentTextStyle();
176
        $componentTextStylesBody->setBackgroundColor('#fff');
177
        $componentTextStylesBody->setFontName('IowanOldStyle-Roman');
178
        $componentTextStylesBody->setFontColor('#222222');
179
        $componentTextStylesBody->setFontSize(16);
180
        $componentTextStylesBody->setLineHeight(22);
181
        $componentTextStylesBody->setLinkStyle($linkStyle);
182
        $componentTextStyles->setDefault($componentTextStylesBody);
183
184
        $componentTextStylesBody = new ComponentTextStyle();
185
        $componentTextStylesBody->setFontName('IowanOldStyle-Roman');
186
        $componentTextStyles->setDefaultBody($componentTextStylesBody);
187
188
        $componentTextStylesTitle = new ComponentTextStyle();
189
        $componentTextStylesTitle->setFontName('DINAlternate-Bold');
190
        $componentTextStylesTitle->setFontSize(42);
191
        $componentTextStylesTitle->setLineHeight(44);
192
        $componentTextStylesTitle->setTextColor('#53585F');
193
        $componentTextStyles->setDefaultTitle($componentTextStylesTitle);
194
195
        $componentTextStylesIntro = new ComponentTextStyle();
196
        $componentTextStylesIntro->setFontName('DINAlternate-Bold');
197
        $componentTextStylesIntro->setFontSize(18);
198
        $componentTextStylesIntro->setLineHeight(22);
199
        $componentTextStylesIntro->setTextColor('#A6AAA9');
200
        $componentTextStyles->setDefaultIntro($componentTextStylesIntro);
201
202
        return $componentTextStyles;
203
    }
204
205
    private function configureComponentLayouts(): ComponentLayouts
206
    {
207
        $componentLayouts = new ComponentLayouts();
208
        $componentLayout = new ComponentLayout();
209
        $componentLayout->setColumnSpan(14);
210
        $componentLayout->setColumnStart(0);
211
        $componentLayout->setMargin(new Margin(12));
212
        $componentLayouts->setHalfMarginBelowLayout($componentLayout);
213
214
        $componentLayout = new ComponentLayout();
215
        $componentLayout->setColumnSpan(14);
216
        $componentLayout->setColumnStart(0);
217
        $componentLayout->setMargin(new Margin(12, 12));
218
        $componentLayouts->setMarginBetweenComponents($componentLayout);
219
220
        return $componentLayouts;
221
    }
222
}
223