Completed
Pull Request — develop (#236)
by Wachter
21:30 queued 06:18
created

ArticleTeaserProvider::getConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 58
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 58
ccs 21
cts 22
cp 0.9545
rs 9.639
c 0
b 0
f 0
cc 1
eloc 31
nc 1
nop 0
crap 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
/*
4
 * This file is part of Sulu.
5
 *
6
 * (c) MASSIVE ART WebServices GmbH
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Sulu\Bundle\ArticleBundle\Teaser;
13
14
use ONGR\ElasticsearchBundle\Service\Manager;
15
use ONGR\ElasticsearchDSL\Query\TermLevel\IdsQuery;
16
use Sulu\Bundle\ArticleBundle\Document\ArticleViewDocument;
17
use Sulu\Bundle\ArticleBundle\Metadata\ArticleViewDocumentIdTrait;
18
use Sulu\Bundle\ContentBundle\Teaser\Configuration\TeaserConfiguration;
19
use Sulu\Bundle\ContentBundle\Teaser\Provider\TeaserProviderInterface;
20
use Sulu\Bundle\ContentBundle\Teaser\Teaser;
21
use Symfony\Component\Translation\TranslatorInterface;
22
23
/**
24
 * Enables selection of articles in teaser content-type.
25
 */
26
class ArticleTeaserProvider implements TeaserProviderInterface
27
{
28
    use ArticleViewDocumentIdTrait;
29
30
    /**
31
     * @var Manager
32
     */
33
    private $searchManager;
34
35
    /**
36
     * @var TranslatorInterface
37
     */
38
    private $translator;
39
40
    /**
41
     * @var string
42
     */
43 2
    private $articleDocumentClass;
44
45 2
    /**
46 2
     * @param Manager $searchManager
47 2
     * @param TranslatorInterface $translator
48
     * @param $articleDocumentClass
49
     */
50
    public function __construct(Manager $searchManager, TranslatorInterface $translator, $articleDocumentClass)
51
    {
52
        $this->searchManager = $searchManager;
53
        $this->translator = $translator;
54
        $this->articleDocumentClass = $articleDocumentClass;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function getConfiguration()
61
    {
62
        return new TeaserConfiguration(
63
            'sulu_article.teaser',
64
            'teaser-selection/list@suluarticle',
65
            [
66
                'url' => '/admin/api/articles?locale={locale}',
67
                'resultKey' => 'articles',
68
                'searchFields' => ['title', 'route_path', 'changer_full_name', 'creator_full_name', 'author_full_name'],
69
            ],
70
            [
71
                [
72
                    'title' => $this->translator->trans('sulu_article.authored', [], 'backend'),
73
                    'cssClass' => 'authored-slide',
74
                    'contentSpacing' => true,
75
                    'buttons' => [
76
                        [
77
                            'type' => 'ok',
78
                            'align' => 'right',
79 2
                        ],
80
                    ],
81 2
                ],
82
                [
83
                    'title' => $this->translator->trans('sulu_article.contact-selection-overlay.title', [], 'backend'),
84
                    'cssClass' => 'contact-slide',
85 2
                    'contentSpacing' => true,
86
                    'buttons' => [
87 2
                        [
88 2
                            'type' => 'ok',
89 2
                            'align' => 'right',
90
                        ],
91 2
                    ],
92 2
                ],
93 2
                [
94 2
                    'title' => $this->translator->trans('sulu_article.category-selection-overlay.title', [], 'backend'),
95 2
                    'cssClass' => 'category-slide',
96 2
                    'contentSpacing' => true,
97 2
                    'buttons' => [
98 2
                        [
99 2
                            'type' => 'ok',
100 2
                            'align' => 'right',
101 2
                        ],
102 2
                    ],
103 2
                ],
104
                [
105
                    'title' => $this->translator->trans('sulu_article.tag-selection-overlay.title', [], 'backend'),
106
                    'cssClass' => 'tag-slide',
107 2
                    'contentSpacing' => true,
108
                    'buttons' => [
109
                        [
110
                            'type' => 'ok',
111
                            'align' => 'right',
112
                        ],
113
                    ],
114
                ],
115
            ]
116
        );
117 2
    }
118
119
    /**
120 2
     * {@inheritdoc}
121 2
     */
122
    public function find(array $ids, $locale)
123
    {
124
        if (0 === count($ids)) {
125
            return [];
126
        }
127
128
        $articleIds = $this->getViewDocumentIds($ids, $locale);
129
130
        $repository = $this->searchManager->getRepository($this->articleDocumentClass);
131
        $search = $repository->createSearch();
132
        $search->addQuery(new IdsQuery($articleIds));
133
134
        $result = [];
135
        foreach ($repository->findDocuments($search) as $item) {
136
            $excerpt = $item->getExcerpt();
137
            $result[] = new Teaser(
138
                $item->getUuid(),
139
                'article',
140
                $item->getLocale(),
141
                ('' !== $excerpt->title ? $excerpt->title : $item->getTitle()),
142
                ('' !== $excerpt->description ? $excerpt->description : $item->getTeaserDescription()),
143
                $excerpt->more,
144
                $item->getRoutePath(),
145
                count($excerpt->images) ? $excerpt->images[0]->id : $item->getTeaserMediaId(),
146
                $this->getAttributes($item)
0 ignored issues
show
Documentation introduced by
$item is of type null|object|array, but the function expects a object<Sulu\Bundle\Artic...nt\ArticleViewDocument>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
147
            );
148
        }
149
150
        return $result;
151
    }
152
153
    /**
154
     * Returns attributes for teaser.
155
     *
156
     * @param ArticleViewDocument $viewDocument
157
     *
158
     * @return array
159
     */
160
    protected function getAttributes(ArticleViewDocument $viewDocument)
161
    {
162
        return [
163
            'structureType' => $viewDocument->getStructureType(),
164
            'type' => $viewDocument->getType(),
165
        ];
166
    }
167
}
168