Completed
Pull Request — develop (#151)
by
unknown
27:07
created

ArticleLinkProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 15.63%

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 8
dl 0
loc 80
ccs 5
cts 32
cp 0.1563
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getConfiguration() 0 19 2
B preload() 0 24 3
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\Markup;
13
14
use ONGR\ElasticsearchBundle\Service\Manager;
15
use ONGR\ElasticsearchDSL\Query\TermLevel\IdsQuery;
16
use ONGR\ElasticsearchDSL\Query\TermLevel\RangeQuery;
17
use ONGR\ElasticsearchDSL\Search;
18
use Sulu\Bundle\ArticleBundle\Document\ArticleViewDocumentInterface;
19
use Sulu\Bundle\ArticleBundle\Document\Index\DocumentFactory;
20
use Sulu\Bundle\ContentBundle\Markup\Link\LinkConfiguration;
21
use Sulu\Bundle\ContentBundle\Markup\Link\LinkItem;
22
use Sulu\Bundle\ContentBundle\Markup\Link\LinkProviderInterface;
23
24
/**
25
 * Integrates articles into link-system.
26
 */
27
class ArticleLinkProvider implements LinkProviderInterface
28
{
29
    /**
30
     * @var Manager
31
     */
32
    private $manager;
33
34
    /**
35
     * @var DocumentFactory
36
     */
37
    private $documentFactory;
38
39
    /**
40
     * @var array
41
     */
42
    private $types;
43
44
    /**
45
     * @param Manager         $manager
46
     * @param DocumentFactory $documentFactory
47
     * @param array           $types
48
     */
49 32
    public function __construct(Manager $manager, DocumentFactory $documentFactory, array $types)
50
    {
51 32
        $this->manager = $manager;
52 32
        $this->documentFactory = $documentFactory;
53 32
        $this->types = $types;
54 32
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getConfiguration()
60
    {
61
        $tabs = null;
62
        if (1 < count($this->types)) {
63
            $tabs = array_map(
64
                function ($type) {
65
                    return ['title' => $type['translation_key']];
66
                },
67
                $this->types
68
            );
69
        }
70
71
        return new LinkConfiguration(
72
            'sulu_article.ckeditor.link',
73
            'ckeditor/link/article@suluarticle',
74
            [],
75
            ['tabs' => $tabs]
76
        );
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function preload(array $hrefs, $locale, $published = true)
83
    {
84
        $search = new Search();
85
        $search->addQuery(new IdsQuery($hrefs));
86
        if ($published) {
87
            $search->addQuery(new RangeQuery('authored', ['lte' => 'now']));
88
        }
89
90
        $repository = $this->manager->getRepository($this->documentFactory->getClass('article'));
91
        $documents = $repository->findDocuments($search);
92
93
        $result = [];
94
        /** @var ArticleViewDocumentInterface $document */
95
        foreach ($documents as $document) {
96
            $result[] = new LinkItem(
97
                $document->getUuid(),
98
                $document->getTitle(),
99
                $document->getRoutePath(),
100
                $document->getAuthored() <= new \DateTime()
101
            );
102
        }
103
104
        return $result;
105
    }
106
}
107