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

ArticleToAppleNewsFormatConverter::convert()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 8.9599
c 0
b 0
f 0
cc 3
nc 4
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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