Completed
Push — master ( f6198e...300d99 )
by Paweł
53:20
created

ArticleHydrator::populateByline()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.5

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 3
cts 6
cp 0.5
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 1
crap 2.5
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\Model\ArticleInterface;
21
use SWP\Bundle\ContentBundle\Provider\RouteProviderInterface;
22
use SWP\Component\Bridge\Model\ItemInterface;
23
use SWP\Component\Bridge\Model\PackageInterface;
24
25
final class ArticleHydrator implements ArticleHydratorInterface
26
{
27
    /**
28
     * @var RouteProviderInterface
29
     */
30
    private $routeProvider;
31
32
    /**
33
     * @var array
34
     */
35
    private $allowedTypes = [
36
        ItemInterface::TYPE_PICTURE,
37
        ItemInterface::TYPE_FILE,
38
        ItemInterface::TYPE_TEXT,
39
        ItemInterface::TYPE_COMPOSITE,
40
    ];
41
42
    /**
43
     * ArticleHydrator constructor.
44
     *
45
     * @param RouteProviderInterface $routeProvider
46 26
     */
47
    public function __construct(RouteProviderInterface $routeProvider)
48 26
    {
49 26
        $this->routeProvider = $routeProvider;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54 11
     */
55
    public function hydrate(ArticleInterface $article, PackageInterface $package): ArticleInterface
56 11
    {
57 11
        if ($this->populateByline($package) !== $package->getByLine()) {
58 11
            $package->setByLine($this->populateByline($package));
59 11
        }
60
61
        $article->setBody($this->populateBody($package));
62 11
        $article->setTitle($package->getHeadline());
63 11
        if (null !== $package->getSlugline()) {
64 11
            $article->setSlug($package->getSlugline());
65 11
        }
66
67 11
        $article->setLocale($package->getLanguage());
68
        $article->setLead($this->populateLead($package));
69 11
        $article->setMetadata($package->getMetadata());
70
        $article->setKeywords($package->getKeywords());
71
        // assign default route
72
        $article->setRoute($this->routeProvider->getRouteForArticle($article));
73
74
        return $article;
75
    }
76
77 11
    /**
78
     * @param PackageInterface $package
79 11
     *
80
     * @return string
81 4
     */
82
    protected function populateLead(PackageInterface $package)
83 4
    {
84 4
        if (null === $package->getDescription() || '' === $package->getDescription()) {
85
            $items = $this->filterTextItems($package->getItems());
86
87 7
            $map = $items->map(
88
                function (ItemInterface $item) {
89
                    return ' '.$item->getDescription();
90
                }
91
            );
92
93
            return trim($package->getDescription().implode('', $map->toArray()));
94
        }
95
96
        return $package->getDescription();
97 11
    }
98 4
99
    /**
100 4
     * @param PackageInterface $package
101 11
     *
102
     * @return string
103
     */
104
    protected function populateByline(PackageInterface $package)
105
    {
106
        $items = $this->filterTextItems($package->getItems());
107
108
        $authors = array_filter(array_values(array_map(function (ItemInterface $item) {
109 4
            $metadata = $item->getMetadata();
110
111 4
            return $metadata['byline'];
112
        }, $items->toArray())));
113
114
        if (empty($authors)) {
115
            return $package->getByLine();
116
        }
117
118 4
        return implode(', ', $authors);
119
    }
120
121
    private function filterTextItems(Collection $items)
122
    {
123
        return $items->filter(
124
            function (ItemInterface $item) {
125
                $this->ensureTypeIsAllowed($item->getType());
126
127
                return ItemInterface::TYPE_TEXT === $item->getType();
128
            }
129
        );
130
    }
131
132
    /**
133
     * @param PackageInterface $package
134
     *
135
     * @return string
136
     */
137
    protected function populateBody(PackageInterface $package)
138
    {
139
        return $package->getBody().' '.implode('', array_map(function (ItemInterface $item) {
140
            $this->ensureTypeIsAllowed($item->getType());
141
142
            return $item->getBody();
143
        }, $package->getItems()->toArray()));
144
    }
145
146
    /**
147
     * @param string $type
148
     *
149
     * @throws \InvalidArgumentException
150
     */
151
    protected function ensureTypeIsAllowed(string $type)
152
    {
153
        if (!in_array($type, $this->allowedTypes)) {
154
            throw new \InvalidArgumentException(sprintf(
155
                'Item type "%s" is not supported. Supported types are: %s',
156
                $type,
157
                implode(', ', $this->allowedTypes)
158
            ));
159
        }
160
    }
161
}
162