Completed
Push — master ( 692aa1...25e5a7 )
by Paweł
60:22
created

ArticleHydrator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 5
dl 0
loc 138
rs 10
c 0
b 0
f 0
ccs 27
cts 30
cp 0.9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A populateLead() 0 16 3
A populateByline() 0 16 2
A filterTextItems() 0 10 1
A populateBody() 0 8 1
A ensureTypeIsAllowed() 0 10 2
A hydrate() 0 22 3
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->setCode($package->getGuid());
62 11
        $article->setBody($this->populateBody($package));
63 11
        $article->setTitle($package->getHeadline());
64 11
        if (null !== $package->getSlugline()) {
65 11
            $article->setSlug($package->getSlugline());
66
        }
67 11
68
        $article->setLocale($package->getLanguage());
69 11
        $article->setLead($this->populateLead($package));
70
        $article->setMetadata($package->getMetadata());
71
        $article->setKeywords($package->getKeywords());
72
        // assign default route
73
        $article->setRoute($this->routeProvider->getRouteForArticle($article));
74
75
        return $article;
76
    }
77 11
78
    /**
79 11
     * @param PackageInterface $package
80
     *
81 4
     * @return string
82
     */
83 4
    protected function populateLead(PackageInterface $package)
84 4
    {
85
        if (null === $package->getDescription() || '' === $package->getDescription()) {
86
            $items = $this->filterTextItems($package->getItems());
87 7
88
            $map = $items->map(
89
                function (ItemInterface $item) {
90
                    return ' '.$item->getDescription();
91
                }
92
            );
93
94
            return trim($package->getDescription().implode('', $map->toArray()));
95
        }
96
97 11
        return $package->getDescription();
98 4
    }
99
100 4
    /**
101 11
     * @param PackageInterface $package
102
     *
103
     * @return string
104
     */
105
    protected function populateByline(PackageInterface $package)
106
    {
107
        $items = $this->filterTextItems($package->getItems());
108
109 4
        $authors = array_filter(array_values(array_map(function (ItemInterface $item) {
110
            $metadata = $item->getMetadata();
111 4
112
            return $metadata['byline'];
113
        }, $items->toArray())));
114
115
        if (empty($authors)) {
116
            return $package->getByLine();
117
        }
118 4
119
        return implode(', ', $authors);
120
    }
121
122
    private function filterTextItems(Collection $items)
123
    {
124
        return $items->filter(
125
            function (ItemInterface $item) {
126
                $this->ensureTypeIsAllowed($item->getType());
127
128
                return ItemInterface::TYPE_TEXT === $item->getType();
129
            }
130
        );
131
    }
132
133
    /**
134
     * @param PackageInterface $package
135
     *
136
     * @return string
137
     */
138
    protected function populateBody(PackageInterface $package)
139
    {
140
        return $package->getBody().' '.implode('', array_map(function (ItemInterface $item) {
141
            $this->ensureTypeIsAllowed($item->getType());
142
143
            return $item->getBody();
144
        }, $package->getItems()->toArray()));
145
    }
146
147
    /**
148
     * @param string $type
149
     *
150
     * @throws \InvalidArgumentException
151
     */
152
    protected function ensureTypeIsAllowed(string $type)
153
    {
154
        if (!in_array($type, $this->allowedTypes)) {
155
            throw new \InvalidArgumentException(sprintf(
156
                'Item type "%s" is not supported. Supported types are: %s',
157
                $type,
158
                implode(', ', $this->allowedTypes)
159
            ));
160
        }
161
    }
162
}
163