-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchFacade.php
More file actions
106 lines (80 loc) · 3.46 KB
/
SearchFacade.php
File metadata and controls
106 lines (80 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);
namespace Ibexa\Behat\API\Facade;
use Ibexa\Contracts\Core\Repository\ContentService;
use Ibexa\Contracts\Core\Repository\LocationService;
use Ibexa\Contracts\Core\Repository\SearchService;
use Ibexa\Contracts\Core\Repository\URLAliasService;
use Ibexa\Contracts\Core\Repository\Values\Content\LocationQuery;
use Ibexa\Contracts\Core\Repository\Values\Content\Query;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\LocationId;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\LogicalAnd;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\LogicalNot;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Subtree;
use Ibexa\Contracts\Core\Repository\Values\Content\URLAlias;
use PHPUnit\Framework\Assert;
class SearchFacade
{
/** @var \Ibexa\Contracts\Core\Repository\LocationService */
private $locationService;
/** @var \Ibexa\Contracts\Core\Repository\URLAliasService */
private $urlAliasService;
/** @var \Ibexa\Contracts\Core\Repository\SearchService */
private $searchService;
/**
* @var \Ibexa\Contracts\Core\Repository\ContentService
*/
private $contentService;
private const ROOT_LOCATION_ID = 2;
public function __construct(URLAliasService $urlAliasService, LocationService $locationService, SearchService $searchService, ContentService $contentService)
{
$this->urlAliasService = $urlAliasService;
$this->locationService = $locationService;
$this->searchService = $searchService;
$this->contentService = $contentService;
}
public function getRandomChildFromPath(string $path): string
{
$urlAlias = $this->urlAliasService->lookup($path);
Assert::assertEquals(URLAlias::LOCATION, $urlAlias->type);
$location = $this->locationService->loadLocation((int) $urlAlias->destination);
$query = new LocationQuery();
$query->performCount = false;
$query->filter = new LogicalAnd([
new Subtree($location->pathString),
new LogicalNot(new LocationId($location->id)),
]);
$query->limit = 100;
$results = $this->searchService->findLocations($query)->searchHits;
$resultCount = count($results);
$randomInt = random_int(0, $resultCount - 1);
$location = $results[$randomInt]->valueObject;
return $this->urlAliasService->reverseLookup($location)->path;
}
public function getRandomContentIds(int $number)
{
$query = new Query();
$query->limit = 50;
$query->performCount = false;
$query->filter = new LogicalNot(new LocationId(self::ROOT_LOCATION_ID));
$results = $this->searchService->findContent($query)->searchHits;
$indices = array_rand($results, $number);
if ($number === 1) {
return $results[$indices]->valueObject->contentInfo->id;
}
$randomContentIDs = [];
foreach ($indices as $i) {
$randomContentIDs[] = $results[$i]->valueObject->contentInfo->id;
}
return $randomContentIDs;
}
public function getRandomLocationID(): int
{
$contentId = $this->getRandomContentIds(1);
return $this->contentService->loadContent($contentId)->contentInfo->mainLocationId;
}
}