-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathEntityRepository.php
More file actions
115 lines (86 loc) · 2.7 KB
/
EntityRepository.php
File metadata and controls
115 lines (86 loc) · 2.7 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
107
108
109
110
111
112
113
114
115
<?php
declare(strict_types = 1);
namespace Spameri\Elastic;
readonly class EntityRepository
{
public function __construct(
private \Spameri\Elastic\Model\Insert $insert,
private \Spameri\Elastic\Model\GetAllBy $getAllBy,
private \Spameri\Elastic\Model\Delete $delete,
private \Spameri\Elastic\Model\EntitySettingsLocator $entitySettingsLocator,
private \Spameri\Elastic\Factory\EntityFactory $entityFactory,
)
{
}
public function find(
string $id,
string $class,
): \Spameri\Elastic\Entity\ElasticEntityInterface
{
$elasticQuery = new \Spameri\ElasticQuery\ElasticQuery();
$elasticQuery->addMustQuery(
new \Spameri\ElasticQuery\Query\Term(
'_id',
$id,
),
);
return $this->findOneBy($elasticQuery, $class);
}
public function findOneBy(
\Spameri\ElasticQuery\ElasticQuery $elasticQuery,
string $class,
): \Spameri\Elastic\Entity\ElasticEntityInterface
{
$elasticQuery->options()->changeSize(1);
$entities = $this->findBy($elasticQuery, $class);
if (\count($entities) === 0) {
throw new \Spameri\Elastic\Exception\DocumentNotFound('Entity ' . $class . ' not found.');
}
return \reset($entities);
}
public function findBy(
\Spameri\ElasticQuery\ElasticQuery $elasticQuery,
string $class,
): array
{
$indexConfig = $this->entitySettingsLocator->locateByEntityClass($class);
try {
$resultSearch = $this->getAllBy->execute($elasticQuery, $indexConfig->indexName());
} catch (\Spameri\Elastic\Exception\ElasticSearch $exception) {
\Tracy\Debugger::log($exception->getMessage(), \Tracy\ILogger::CRITICAL);
throw $exception;
}
$entities = [];
foreach ($resultSearch->hits() as $hit) {
try {
$entities[] = $this->entityFactory->create($hit, $class)->current();
} catch (\Spameri\Elastic\Exception\ElasticSearch $exception) {
\Tracy\Debugger::log($exception->getMessage(), \Tracy\ILogger::CRITICAL);
}
}
// TODO univerzální kolekce
return $entities;
}
public function findAll(string $class): array
{
$elasticQuery = new \Spameri\ElasticQuery\ElasticQuery();
$elasticQuery->options()->changeSize(10000);
return $this->findBy($elasticQuery, $class);
}
public function persist(
\Spameri\Elastic\Entity\AbstractElasticEntity $entity
): string
{
$indexConfig = $this->entitySettingsLocator->locateByEntityClass($entity::class);
return $this->insert->execute(
entity: $entity,
index: $indexConfig->indexName(),
hasSti: $indexConfig->hasSti(),
);
}
public function remove(\Spameri\Elastic\Entity\ElasticEntityInterface $entity): bool
{
$indexConfig = $this->entitySettingsLocator->locateByEntityClass($entity::class);
return $this->delete->execute($entity->id(), $indexConfig->indexName());
}
}