UpdateQuery::update()   B
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8977
c 0
b 0
f 0
cc 6
nc 7
nop 0
1
<?php
2
3
namespace Mdiyakov\DoctrineSolrBundle\Query\Update;
4
5
use Mdiyakov\DoctrineSolrBundle\Exception\SchemaConfigException;
6
use Mdiyakov\DoctrineSolrBundle\Exception\UpdateQueryException;
7
use Mdiyakov\DoctrineSolrBundle\Schema\Schema;
8
use Solarium\Client;
9
10
class UpdateQuery
11
{
12
    /**
13
     * @var Client
14
     */
15
    private $client;
16
17
    /**
18
     * @var \Solarium\QueryType\Update\Query\Query
19
     */
20
    private $solrQuery;
21
22
    /**
23
     * @var Schema
24
     */
25
    private $schema;
26
27
    /**
28
     * @var array
29
     */
30
    private $addFieldsConditions = [];
31
32
    /**
33
     * @var array
34
     */
35
    private $deleteConditions = [];
36
37
    /**
38
     * @var bool
39
     */
40
    private $entityStarted = false;
41
42
    /**
43
     * @var bool
44
     */
45
    private $entityEnded = true;
46
47
    /**
48
     * @var array
49
     */
50
    private $entityConditions = [];
51
52
    /**
53
     * @var array
54
     */
55
    private $uniqueFieldCondition = [];
56
57
    /**
58
     * @param Client $client
59
     * @param Schema $schema
60
     */
61
    public function __construct(Client $client, Schema $schema)
62
    {
63
        $this->client = $client;
64
        $this->solrQuery = $client->createUpdate();
65
        $this->schema = $schema;
66
    }
67
68
    /**
69
     * @return $this
70
     */
71
    public function reset()
72
    {
73
        $this->entityConditions = [];
74
        $this->addFieldsConditions = [];
75
        $this->deleteConditions = [];
76
        $this->solrQuery = $this->client->createUpdate();
77
        $this->entityStarted = false;
78
        $this->entityEnded = true;
79
        $this->uniqueFieldCondition = [];
80
81
        return $this;
82
    }
83
84
    public function beginEntity()
85
    {
86
        if (!$this->entityEnded) {
87
            throw new UpdateQueryException('Entity is not ended. Call "endEntity" method before starting another one');
88
        }
89
90
        $this->addFieldsConditions = [];
91
        $this->uniqueFieldCondition = [];
92
        $this->entityStarted = true;
93
        $this->entityEnded = false;
94
    }
95
96
97
    public function endEntity()
98
    {
99
        if (!$this->entityStarted) {
100
            throw new UpdateQueryException('Entity is not started. Call "beginEntity" method before ending');
101
        }
102
103
        if (!$this->uniqueFieldCondition) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->uniqueFieldCondition of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
104
            throw new UpdateQueryException('You have to specify unique field value');
105
        }
106
107
        $this->entityConditions[] = array_merge($this->addFieldsConditions, $this->uniqueFieldCondition);
108
        $this->entityStarted = false;
109
        $this->entityEnded = true;
110
    }
111
112
    /**
113
     * @param $configFieldName
114
     * @param $value
115
     */
116
    public function addConfigField($configFieldName, $value)
117
    {
118
        if (!$this->entityStarted) {
119
            throw new UpdateQueryException('Entity is not started. Call "beginEntity" method before add field value');
120
        }
121
122
        $configEntityField = $this->schema->getConfigFieldByName($configFieldName);
123
        $this->addFieldsConditions[$configEntityField->getDocumentFieldName()] = $value;
124
    }
125
126
    /**
127
     * @param string $entityFieldName
128
     * @param $value
129
     */
130
    public function addField($entityFieldName, $value)
131
    {
132
        if (!$this->entityStarted) {
133
            throw new UpdateQueryException('Entity is not started. Call "beginEntity" method before add field value');
134
        }
135
136
        $field = $this->schema->getFieldByEntityFieldName($entityFieldName);
137
        $this->addFieldsConditions[$field->getDocumentFieldName()] = $value;
138
    }
139
140
    /**
141
     * @param $value
142
     */
143
    public function addUniqueFieldValue($value)
144
    {
145
        if (!$this->entityStarted) {
146
            throw new UpdateQueryException('Entity is not started. Call "beginEntity" method before add field value');
147
        }
148
149
        $documentUniqueField = $this->schema->getDocumentUniqueField();
150
        $this->uniqueFieldCondition[$documentUniqueField->getName()] = $value;
151
    }
152
153
    /**
154
     * @param string $entityFieldName
155
     * @param string $value
156
     * @throws SchemaConfigException
157
     */
158
    public function addDeleteCriteriaByField($entityFieldName, $value)
159
    {
160
        $field = $this->schema->getFieldByEntityFieldName($entityFieldName);
161
        $this->deleteConditions[$field->getDocumentFieldName()] = $value;
162
    }
163
164
    /**
165
     * @param string $entityFieldName
166
     * @param mixed $value
167
     */
168
    public function addDeleteCriteriaByConfigField($entityFieldName, $value)
169
    {
170
        $field = $this->schema->getConfigFieldByName($entityFieldName);
171
        $this->deleteConditions[$field->getDocumentFieldName()] = $value;
172
    }
173
174
175
    /**
176
     * @param $value
177
     */
178
    public function addDeleteCriteriaByUniqueFieldValue($value)
179
    {
180
        $field = $this->schema->getDocumentUniqueField();
181
        $this->deleteConditions[$field->getName()] = $value;
182
    }
183
184
    public function update()
185
    {
186
        if ($this->entityStarted || !$this->entityEnded) {
187
            throw new UpdateQueryException('It seems you did not end the entity');
188
        }
189
190
        foreach($this->deleteConditions as $documentFieldName => $value) {
191
            $this->solrQuery->addDeleteQuery(
192
                sprintf('%s:"%s"', $documentFieldName, $value)
193
            );
194
        }
195
196
        foreach ($this->entityConditions as $fieldsConditions) {
197
            /** @var \Solarium\QueryType\Update\Query\Document $document */
198
            $document = $this->solrQuery->createDocument();
199
            foreach ($fieldsConditions as $documentFieldName => $value) {
200
                $document->addField($documentFieldName, $value);
201
            }
202
            $this->solrQuery->addDocument($document);
203
        }
204
205
        $this->solrQuery->addCommit();
206
        $this->client->execute($this->solrQuery);
207
208
        $this->reset();
209
    }
210
}