Completed
Push — master ( a682ba...4851b3 )
by Paweł
40:50
created

AbstractArticleFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 96
Duplicated Lines 16.67 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 89.29%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 16
loc 96
ccs 25
cts 28
cp 0.8929
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A hydrateArticle() 0 19 2
A populateLead() 8 8 1
A populateBody() 8 8 1
A ensureTypeIsAllowed() 0 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Factory;
18
19
use SWP\Bundle\ContentBundle\Model\ArticleInterface;
20
use SWP\Bundle\ContentBundle\Provider\RouteProviderInterface;
21
use SWP\Component\Bridge\Model\ItemInterface;
22
use SWP\Component\Bridge\Model\PackageInterface;
23
24
abstract class AbstractArticleFactory implements ArticleFactoryInterface
25
{
26
    /**
27
     * @var array
28
     */
29
    private $allowedTypes = [
30
        ItemInterface::TYPE_PICTURE,
31
        ItemInterface::TYPE_FILE,
32
        ItemInterface::TYPE_TEXT,
33
        ItemInterface::TYPE_COMPOSITE,
34
    ];
35
36
    /**
37
     * @var RouteProviderInterface
38
     */
39
    protected $routeProvider;
40
41
    /**
42
     * AbstractArticleFactory constructor.
43
     *
44
     * @param RouteProviderInterface $routeProvider
45
     */
46 16
    public function __construct(RouteProviderInterface $routeProvider)
47
    {
48 16
        $this->routeProvider = $routeProvider;
49 16
    }
50
51
    /**
52
     * @param PackageInterface $package
53
     *
54
     * @return ArticleInterface
55
     */
56 16
    protected function hydrateArticle(PackageInterface $package)
57
    {
58
        /** @var ArticleInterface $article */
59 16
        $article = $this->create();
60
61 16
        $article->setBody($this->populateBody($package));
62 16
        $article->setTitle($package->getHeadline());
63 16
        if (null !== $package->getSlugline()) {
64 16
            $article->setSlug($package->getSlugline());
65
        }
66
67 16
        $article->setLocale($package->getLanguage());
68 16
        $article->setLead($this->populateLead($package));
69 16
        $article->setMetadata($package->getMetadata());
70
        // assign default route
71 16
        $article->setRoute($this->routeProvider->getRouteForArticle($article));
72
73 16
        return $article;
74
    }
75
76
    /**
77
     * @param PackageInterface $package
78
     *
79
     * @return string
80
     */
81 16 View Code Duplication
    protected function populateLead(PackageInterface $package)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
    {
83
        return trim($package->getDescription().implode('', array_map(function (ItemInterface $item) {
84 11
            $this->ensureTypeIsAllowed($item->getType());
85
86 11
            return ' '.$item->getDescription();
87 16
        }, $package->getItems()->toArray())));
88
    }
89
90
    /**
91
     * @param PackageInterface $package
92
     *
93
     * @return string
94
     */
95 View Code Duplication
    protected function populateBody(PackageInterface $package)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
    {
97 16
        return $package->getBody().' '.implode('', array_map(function (ItemInterface $item) {
98 11
            $this->ensureTypeIsAllowed($item->getType());
99
100 11
            return $item->getBody();
101 16
        }, $package->getItems()->toArray()));
102
    }
103
104
    /**
105
     * @param string $type
106
     *
107
     * @throws \InvalidArgumentException
108
     */
109 11
    protected function ensureTypeIsAllowed(string $type)
110
    {
111 11
        if (!in_array($type, $this->allowedTypes)) {
112
            throw new \InvalidArgumentException(sprintf(
113
                'Item type "%s" is not supported. Supported types are: %s',
114
                $type,
115
                implode(', ', $this->allowedTypes)
116
            ));
117
        }
118 11
    }
119
}
120