Completed
Push — 1.4 ( a3fc6f...89c66f )
by Paweł
14s
created

ArticleHydrator::populateKeywords()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Content Bundle.
7
 *
8
 * Copyright 2016 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 2016 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\ContentBundle\Hydrator;
18
19
use Doctrine\Common\Collections\Collection;
20
use SWP\Bundle\ContentBundle\Service\ArticleKeywordAdderInterface;
21
use SWP\Bundle\ContentBundle\Service\ArticleSourcesAdderInterface;
22
use SWP\Bundle\ContentBundle\Model\ArticleInterface;
23
use SWP\Component\Bridge\Model\ItemInterface;
24
use SWP\Component\Bridge\Model\PackageInterface;
25
26
final class ArticleHydrator implements ArticleHydratorInterface
27
{
28
    /**
29
     * @var ArticleSourcesAdderInterface
30
     */
31
    private $articleSourcesAdder;
32
33
    /**
34
     * @var ArticleKeywordAdderInterface
35
     */
36
    private $articleKeywordAdder;
37
38
    /**
39
     * @var array
40
     */
41
    private $allowedTypes = [
42
        ItemInterface::TYPE_PICTURE,
43
        ItemInterface::TYPE_FILE,
44
        ItemInterface::TYPE_TEXT,
45
        ItemInterface::TYPE_COMPOSITE,
46
        ItemInterface::TYPE_VIDEO,
47
        ItemInterface::TYPE_AUDIO,
48
    ];
49
50
    public function __construct(ArticleSourcesAdderInterface $articleSourcesAdder, ArticleKeywordAdderInterface $articleKeywordAdder)
51
    {
52
        $this->articleSourcesAdder = $articleSourcesAdder;
53
        $this->articleKeywordAdder = $articleKeywordAdder;
54
    }
55
56
    public function hydrate(ArticleInterface $article, PackageInterface $package): ArticleInterface
57
    {
58
        if ($this->populateByline($package) !== $package->getByLine()) {
59
            $package->setByLine($this->populateByline($package));
60
        }
61
62
        $article->setCode($package->getGuid());
63
        $article->setBody($this->populateBody($package));
64
65
        if (null !== $package->getSlugline() && null === $article->getSlug()) {
66
            $article->setSlug($package->getSlugline());
67
        }
68
69
        $article->setTitle($package->getHeadline());
70
        $article->setAuthors($package->getAuthors());
71
        $article->setExtra($package->getExtra());
72
73
        $this->populateSources($article, $package);
74
        $this->populateKeywords($article, $package);
75
76
        $article->setLocale($package->getLanguage());
77
        $article->setLead($this->populateLead($package));
78
        $article->setMetadata($package->getMetadata());
79
        $article->setRoute($article->getRoute());
80
81
        return $article;
82
    }
83
84
    /**
85
     * @param PackageInterface $package
86
     *
87
     * @return string
88
     */
89
    private function populateLead(PackageInterface $package)
90
    {
91
        if (null === $package->getDescription() || '' === $package->getDescription()) {
92
            $items = $this->filterTextItems($package->getItems());
93
94
            $map = $items->map(
95
                function (ItemInterface $item) {
96
                    return ' '.$item->getDescription();
97
                }
98
            );
99
100
            return trim($package->getDescription().implode('', $map->toArray()));
101
        }
102
103
        return $package->getDescription();
104
    }
105
106
    /**
107
     * @param PackageInterface $package
108
     *
109
     * @return string
110
     */
111
    private function populateByline(PackageInterface $package)
112
    {
113
        $items = $this->filterTextItems($package->getItems());
114
115
        $authors = array_filter(array_values(array_map(function (ItemInterface $item) {
116
            $metadata = $item->getMetadata();
117
118
            return $metadata['byline'];
119
        }, $items->toArray())));
120
121
        if (empty($authors)) {
122
            return $package->getByLine();
123
        }
124
125
        return implode(', ', $authors);
126
    }
127
128
    /**
129
     * @param Collection $items
130
     *
131
     * @return Collection
132
     */
133
    private function filterTextItems(Collection $items)
134
    {
135
        return $items->filter(
136
            function (ItemInterface $item) {
137
                $this->ensureTypeIsAllowed($item->getType());
138
139
                return ItemInterface::TYPE_TEXT === $item->getType();
140
            }
141
        );
142
    }
143
144
    /**
145
     * @param PackageInterface $package
146
     *
147
     * @return string
148
     */
149
    private function populateBody(PackageInterface $package)
150
    {
151
        return $package->getBody().' '.implode('', array_map(function (ItemInterface $item) {
152
            $this->ensureTypeIsAllowed($item->getType());
153
154
            return $item->getBody();
155
        }, $package->getItems()->toArray()));
156
    }
157
158
    /**
159
     * @param ArticleInterface $article
160
     * @param PackageInterface $package
161
     */
162
    private function populateSources(ArticleInterface $article, PackageInterface $package)
163
    {
164
        if (null === $package->getSource()) {
165
            return;
166
        }
167
168
        $this->articleSourcesAdder->add($article, $package->getSource());
169
170
        foreach ($package->getItems() as $item) {
171
            if (null === $item->getSource()) {
172
                continue;
173
            }
174
175
            $this->articleSourcesAdder->add($article, $item->getSource());
176
        }
177
    }
178
179
    private function populateKeywords(ArticleInterface $article, PackageInterface $package)
180
    {
181
        if (0 === \count($package->getKeywords())) {
182
            return;
183
        }
184
185
        foreach ($package->getKeywords() as $keyword) {
186
            $this->articleKeywordAdder->add($article, $keyword);
187
        }
188
    }
189
190
    /**
191
     * @param string $type
192
     *
193
     * @throws \InvalidArgumentException
194
     */
195
    private function ensureTypeIsAllowed(string $type)
196
    {
197
        if (!\in_array($type, $this->allowedTypes, true)) {
198
            throw new \InvalidArgumentException(sprintf(
199
                'Item type "%s" is not supported. Supported types are: %s',
200
                $type,
201
                implode(', ', $this->allowedTypes)
202
            ));
203
        }
204
    }
205
}
206