UpdateQuery::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Solr Client Symfony package.
7
 *
8
 * (c) ingatlan.com Zrt. <[email protected]>
9
 *
10
 * This source file is subject to the MIT license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13
14
namespace iCom\SolrClient\Query;
15
16
use iCom\SolrClient\JsonQuery;
17
use iCom\SolrClient\Query\Command\Add;
18
use iCom\SolrClient\Query\Command\Commit;
19
use iCom\SolrClient\Query\Command\Delete;
20
use iCom\SolrClient\Query\Command\Optimize;
21
use iCom\SolrClient\Query\Command\Rollback;
22
23
/**
24
 * Creates a JSON formatted update query.
25
 *
26
 * Note that: Solr has his own JSON syntax which allows to have multiple keys (multiple commands).
27
 *
28
 * @see https://lucene.apache.org/solr/guide/8_3/uploading-data-with-index-handlers.html#sending-json-update-commands
29
 *
30
 * @psalm-immutable
31
 */
32
final class UpdateQuery implements JsonQuery
33
{
34
    /**
35
     * @var Command[]
36
     * @psaml-var list<Command>
37
     */
38
    private array $commands = [];
39
40
    /**
41
     * @psalm-param list<Command> $commands
42
     */
43
    public function __construct(iterable $commands = [])
44
    {
45
        $this->commands = (static function (Command ...$commands): array {
46
            return $commands;
47
        })(...$commands);
48
    }
49
50
    /**
51
     * @psalm-param list<Command> $commands
52
     */
53
    public static function create(iterable $commands = []): self
54
    {
55
        return new self($commands);
56
    }
57
58
    public function add(array $document, ?int $commitWithin = null, ?bool $overwrite = null): self
59
    {
60
        $add = Add::create($document);
61
62
        if (null !== $overwrite) {
63
            $add = $overwrite ? $add->enableOverWrite() : $add->disableOverWrite();
64
        }
65
66
        if (null !== $commitWithin) {
67
            $add = $add->commitWithin($commitWithin);
68
        }
69
70
        return $this->withCommand($add);
71
    }
72
73
    public function commit(?bool $waitSearcher = null, ?bool $expungeDeletes = null): self
74
    {
75
        $commit = Commit::create();
76
77
        if (null !== $waitSearcher) {
78
            $commit = $waitSearcher ? $commit->enableWaitSearcher() : $commit->disableWaitSearcher();
79
        }
80
81
        if (null !== $expungeDeletes) {
82
            $commit = $expungeDeletes ? $commit->enableExpungeDeletes() : $commit->disableExpungeDeletes();
83
        }
84
85
        return $this->withCommand($commit);
86
    }
87
88
    public function optimize(?bool $waitSearcher = null, ?int $maxSegments = null): self
89
    {
90
        $optimize = Optimize::create();
91
92
        if (null !== $waitSearcher) {
93
            $optimize = $waitSearcher ? $optimize->enableWaitSearcher() : $optimize->disableWaitSearcher();
94
        }
95
96
        if (null !== $maxSegments) {
97
            $optimize = $optimize->maxSegments($maxSegments);
98
        }
99
100
        return $this->withCommand($optimize);
101
    }
102
103
    public function rollback(): self
104
    {
105
        return $this->withCommand(Rollback::create());
106
    }
107
108
    public function deleteByIds(array $ids): self
109
    {
110
        return $this->withCommand(Delete::fromIds($ids));
111
    }
112
113
    public function deleteByQuery(SelectQuery $query): self
114
    {
115
        return $this->withCommand(Delete::fromQuery($query));
116
    }
117
118
    public function toJson(): string
119
    {
120
        $commands = [];
121
        foreach ($this->commands as $command) {
122
            $commands[] = sprintf('"%s":%s', $command->getName(), $command->toJson());
123
        }
124
125
        return sprintf('{%s}', implode(',', $commands));
126
    }
127
128
    private function withCommand(Command $command): self
129
    {
130
        $self = clone $this;
131
        $self->commands[] = $command;
132
133
        return $self;
134
    }
135
}
136