Completed
Pull Request — develop (#233)
by Wachter
45:40 queued 30:46
created

ArticleTeaserProvider::getConfiguration()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 5
cp 0
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 0
crap 2
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
22
/**
23
 * Enables selection of articles in teaser content-type.
24
 */
25
class ArticleTeaserProvider implements TeaserProviderInterface
26
{
27
    use ArticleViewDocumentIdTrait;
28
29
    /**
30
     * @var Manager
31
     */
32
    private $searchManager;
33
34
    /**
35
     * @var string
36
     */
37
    private $articleDocumentClass;
38
39
    /**
40
     * @param Manager $searchManager
41
     * @param $articleDocumentClass
42
     */
43 2
    public function __construct(Manager $searchManager, $articleDocumentClass)
44
    {
45 2
        $this->searchManager = $searchManager;
46 2
        $this->articleDocumentClass = $articleDocumentClass;
47 2
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getConfiguration()
53
    {
54
        return new TeaserConfiguration(
55
            'sulu_article.teaser',
56
            'teaser-selection/list@suluarticle',
57
            [
58
                'url' => '/admin/api/articles?locale={locale}',
59
                'resultKey' => 'articles',
60
                'searchFields' => ['title', 'route_path', 'changer_full_name', 'creator_full_name', 'pages.title'],
61
            ],
62
            [
0 ignored issues
show
Unused Code introduced by
The call to TeaserConfiguration::__construct() has too many arguments starting with array(array('title' => '... 'align' => 'right')))).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
63
                [
64
                    'title' => 'Test 1',
65
                    'cssClass' => 'authored-slide',
66
                    'contentSpacing' => true,
67
                    'buttons' => [
68
                        [
69
                            'type' => 'ok',
70
                            'align' => 'right',
71
                        ],
72
                    ],
73
                ],
74
            ]
75
        );
76
    }
77
78
    /**
79 2
     * {@inheritdoc}
80
     */
81 2
    public function find(array $ids, $locale)
82
    {
83
        if (0 === count($ids)) {
84
            return [];
85 2
        }
86
87 2
        $articleIds = $this->getViewDocumentIds($ids, $locale);
88 2
89 2
        $repository = $this->searchManager->getRepository($this->articleDocumentClass);
90
        $search = $repository->createSearch();
91 2
        $search->addQuery(new IdsQuery($articleIds));
92 2
93 2
        $result = [];
94 2
        foreach ($repository->findDocuments($search) as $item) {
95 2
            $excerpt = $item->getExcerpt();
96 2
            $result[] = new Teaser(
97 2
                $item->getUuid(),
98 2
                'article',
99 2
                $item->getLocale(),
100 2
                ('' !== $excerpt->title ? $excerpt->title : $item->getTitle()),
101 2
                ('' !== $excerpt->description ? $excerpt->description : $item->getTeaserDescription()),
102 2
                $excerpt->more,
103 2
                $item->getRoutePath(),
104
                count($excerpt->images) ? $excerpt->images[0]->id : $item->getTeaserMediaId(),
105
                $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...
106
            );
107 2
        }
108
109
        return $result;
110
    }
111
112
    /**
113
     * Returns attributes for teaser.
114
     *
115
     * @param ArticleViewDocument $viewDocument
116
     *
117 2
     * @return array
118
     */
119
    protected function getAttributes(ArticleViewDocument $viewDocument)
120 2
    {
121 2
        return [
122
            'structureType' => $viewDocument->getStructureType(),
123
            'type' => $viewDocument->getType(),
124
        ];
125
    }
126
}
127