|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Kreta package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Beñat Espiña <[email protected]> |
|
7
|
|
|
* (c) Gorka Laucirica <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
declare(strict_types=1); |
|
14
|
|
|
|
|
15
|
|
|
namespace Kreta\Notifier\Infrastructure\Application\Query\Elasticsearch\Inbox; |
|
16
|
|
|
|
|
17
|
|
|
use Kreta\Notifier\Application\Query\Inbox\GetNotificationsHandler; |
|
18
|
|
|
use Kreta\Notifier\Application\Query\Inbox\GetNotificationsQuery; |
|
19
|
|
|
use ONGR\ElasticsearchBundle\Service\Repository; |
|
20
|
|
|
use ONGR\ElasticsearchDSL\Query\TermLevel\IdsQuery; |
|
21
|
|
|
use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery; |
|
22
|
|
|
|
|
23
|
|
|
final class ElasticsearchGetNotificationsHandler implements GetNotificationsHandler |
|
24
|
|
|
{ |
|
25
|
|
|
private $repository; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(Repository $repository) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->repository = $repository; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function __invoke(GetNotificationsQuery $query) : array |
|
33
|
|
|
{ |
|
34
|
|
|
$userId = $query->userId(); |
|
35
|
|
|
$offset = $query->offset(); |
|
36
|
|
|
$limit = $query->limit(); |
|
37
|
|
|
$status = $query->status(); |
|
38
|
|
|
|
|
39
|
|
|
$search = $this->repository->createSearch(); |
|
40
|
|
|
$search->setSize($limit); |
|
41
|
|
|
$search->setFrom($offset); |
|
42
|
|
|
|
|
43
|
|
|
$search->addQuery(new IdsQuery([$userId])); |
|
44
|
|
|
if (null !== $status) { |
|
45
|
|
|
$search->addQuery(new TermQuery('notifications.status', $status)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$user = $this->repository->findDocuments($search)->current(); |
|
49
|
|
|
|
|
50
|
|
|
$result = []; |
|
51
|
|
|
foreach ($user->notifications as $notification) { |
|
52
|
|
|
$result['notifications'][] = $notification; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $result; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|