|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Superdesk Web Publisher ElasticSearch Bundle. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright 2017 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 2017 Sourcefabric z.ú |
|
14
|
|
|
* @license http://www.superdesk.org/license |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace SWP\Bundle\ElasticSearchBundle\Loader; |
|
18
|
|
|
|
|
19
|
|
|
use FOS\ElasticaBundle\Manager\RepositoryManagerInterface; |
|
20
|
|
|
use SWP\Bundle\ElasticSearchBundle\Criteria\Criteria; |
|
21
|
|
|
use SWP\Bundle\ElasticSearchBundle\Repository\ArticleRepository; |
|
22
|
|
|
use SWP\Component\MultiTenancy\Context\TenantContextInterface; |
|
23
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Factory\MetaFactoryInterface; |
|
24
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Loader\LoaderInterface; |
|
25
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Meta\MetaCollection; |
|
26
|
|
|
|
|
27
|
|
|
final class SearchResultLoader implements LoaderInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var RepositoryManagerInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $repositoryManager; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var MetaFactoryInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $metaFactory; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var TenantContextInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
private $tenantContext; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
private $modelClass; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var array |
|
51
|
|
|
*/ |
|
52
|
|
|
private $extraFields; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* SearchResultLoader constructor. |
|
56
|
|
|
* |
|
57
|
|
|
* @param RepositoryManagerInterface $repositoryManager |
|
58
|
|
|
* @param MetaFactoryInterface $metaFactory |
|
59
|
|
|
* @param TenantContextInterface $tenantContext |
|
60
|
|
|
* @param string $modelClass |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __construct( |
|
63
|
|
|
RepositoryManagerInterface $repositoryManager, |
|
64
|
|
|
MetaFactoryInterface $metaFactory, |
|
65
|
|
|
TenantContextInterface $tenantContext, |
|
66
|
|
|
string $modelClass, |
|
67
|
|
|
array $extraFields |
|
68
|
|
|
) { |
|
69
|
|
|
$this->repositoryManager = $repositoryManager; |
|
70
|
|
|
$this->metaFactory = $metaFactory; |
|
71
|
|
|
$this->tenantContext = $tenantContext; |
|
72
|
|
|
$this->modelClass = $modelClass; |
|
73
|
|
|
$this->extraFields = $extraFields; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritdoc} |
|
78
|
|
|
*/ |
|
79
|
|
|
public function load($metaType, $withParameters = [], $withoutParameters = [], $responseType = self::COLLECTION) |
|
80
|
|
|
{ |
|
81
|
|
|
if (isset($withParameters['order']) && 2 === count($withParameters['order'])) { |
|
82
|
|
|
$withParameters['sort'] = [$withParameters['order'][0] => $withParameters['order'][1]]; |
|
83
|
|
|
unset($withParameters['order']); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$criteria = Criteria::fromQueryParameters($withParameters['term'] ?? '', $withParameters); |
|
87
|
|
|
|
|
88
|
|
|
/** @var ArticleRepository $repository */ |
|
89
|
|
|
$repository = $this->repositoryManager->getRepository($this->modelClass); |
|
90
|
|
|
$query = $repository->findByCriteria($criteria, $this->extraFields); |
|
91
|
|
|
$partialResult = $query->getResults( |
|
92
|
|
|
$criteria->getPagination()->getOffset(), |
|
93
|
|
|
$criteria->getPagination()->getItemsPerPage() |
|
94
|
|
|
); |
|
95
|
|
|
|
|
96
|
|
|
$metaCollection = new MetaCollection(); |
|
97
|
|
|
$metaCollection->setTotalItemsCount($partialResult->getTotalHits()); |
|
98
|
|
|
foreach ($partialResult->toArray() as $article) { |
|
99
|
|
|
if (null !== ($articleMeta = $this->metaFactory->create($article))) { |
|
100
|
|
|
$metaCollection->add($articleMeta); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $metaCollection; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* {@inheritdoc} |
|
109
|
|
|
*/ |
|
110
|
|
|
public function isSupported(string $type): bool |
|
111
|
|
|
{ |
|
112
|
|
|
return in_array($type, ['searchResults']); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|