Completed
Push — master ( 7fe5eb...9bad48 )
by Beñat
08:54 queued 04:27
created

buildSearch()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 15
nc 2
nop 2
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\Domain\ReadModel\Inbox\Notification;
16
17
use Kreta\SharedKernel\Infrastructure\Domain\ReadModel\ElasticsearchSearchSpecification;
18
19
class ElasticsearchNotificationsOfUserSpecification implements ElasticsearchSearchSpecification
20
{
21
    private $userId;
22
    private $from;
23
    private $size;
24
    private $status;
25
26
    public function __construct(string $userId, int $from, int $size, ?string $status)
27
    {
28
        $this->userId = $userId;
29
        $this->from = $from;
30
        $this->size = $size;
31
        $this->status = $status;
32
    }
33
34
    public function buildSearch(string $index, string $type) : array
35
    {
36
        $search = [
37
            'index' => $index,
38
            'type'  => $type,
39
            'body'  => [
40
                'from'  => $this->from,
41
                'size'  => $this->size,
42
                'query' => [
43
                    'bool' => [
44
                        'must' => [
45
                            [
46
                                'match' => [
47
                                    'user_id' => $this->userId,
48
                                ],
49
                            ],
50
                        ],
51
                    ],
52
                ],
53
            ],
54
        ];
55
56
        if (!empty($this->status)) {
57
            $search['body']['query']['bool']['must'][]['match']['status'] = $this->status;
58
        }
59
60
        return $search;
61
    }
62
}
63