Completed
Pull Request — develop (#173)
by Wachter
12:32
created

ArticleTeaserProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
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 2
     */
43
    public function __construct(Manager $searchManager, $articleDocumentClass)
44 2
    {
45 2
        $this->searchManager = $searchManager;
46 2
        $this->articleDocumentClass = $articleDocumentClass;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getConfiguration()
53
    {
54
        return new TeaserConfiguration(
55
            'sulu_article.teaser',
56
            'teaser-selection/list@sulucontent',
57
            [
58
                'url' => '/admin/api/articles?locale={locale}',
59
                'resultKey' => 'articles',
60
                'searchFields' => ['title', 'type'],
61
                'matchings' => [
62
                    [
63
                        'content' => 'public.title',
64
                        'name' => 'title',
65
                    ],
66
                    [
67
                        'content' => 'public.type',
68
                        'name' => 'typeTranslation',
69
                        'type' => 'translation',
70
                    ],
71
                ],
72
            ]
73
        );
74
    }
75
76
    /**
77
     * {@inheritdoc}
78 2
     */
79
    public function find(array $ids, $locale)
80 2
    {
81
        if (0 === count($ids)) {
82
            return [];
83
        }
84 2
85
        $articleIds = $this->getViewDocumentIds($ids, $locale);
86 2
87 2
        $repository = $this->searchManager->getRepository($this->articleDocumentClass);
88 2
        $search = $repository->createSearch();
89
        $search->addQuery(new IdsQuery($articleIds));
90 2
91 2
        $result = [];
92 2
        foreach ($repository->findDocuments($search) as $item) {
93 2
            $excerpt = $item->getExcerpt();
94 2
            $result[] = new Teaser(
95 2
                $item->getUuid(),
96 2
                'article',
97 2
                $item->getLocale(),
98 2
                ('' !== $excerpt->title ? $excerpt->title : $item->getTitle()),
99 2
                ('' !== $excerpt->description ? $excerpt->description : $item->getTeaserDescription()),
100 2
                $excerpt->more,
101 2
                $item->getRoutePath(),
102
                count($excerpt->images) ? $excerpt->images[0]->id : $item->getTeaserMediaId(),
103 2
                $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...
104 2
            );
105
        }
106
107
        return $result;
108
    }
109 2
110
    /**
111
     * Returns attributes for teaser.
112
     *
113
     * @param ArticleViewDocument $viewDocument
114
     *
115
     * @return array
116
     */
117
    protected function getAttributes(ArticleViewDocument $viewDocument)
118
    {
119
        return [
120
            'structureType' => $viewDocument->getStructureType(),
121
            'type' => $viewDocument->getType(),
122
        ];
123
    }
124
}
125