Indexer::getUpdateQuery()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Mdiyakov\DoctrineSolrBundle\Indexer;
4
5
use Mdiyakov\DoctrineSolrBundle\Query\Update\UpdateQuery;
6
use Mdiyakov\DoctrineSolrBundle\Schema\Schema;
7
8
class Indexer
9
{
10
    /**
11
     * @var Schema
12
     */
13
    private $schema;
14
15
    /**
16
     * @var UpdateQuery
17
     */
18
    private $updateQuery;
19
20
    /**
21
     * @var string[]
22
     */
23
    private $entityConfig;
24
25
    /**
26
     * @param UpdateQuery $updateQuery
27
     * @param Schema $schema
28
     * @param string[] $entityConfig
29
     */
30
    public function __construct(UpdateQuery $updateQuery, Schema $schema, $entityConfig)
31
    {
32
        $this->updateQuery = $updateQuery;
33
        $this->schema = $schema;
34
        $this->entityConfig = $entityConfig;
35
    }
36
37
    /**
38
     * @param mixed $entity
39
     */
40
    public function indexAllFields($entity)
41
    {
42
        $updateQuery = $this->getUpdateQuery();
43
        $updateQuery->beginEntity();
44
        $fields = $this->schema->getFields();
45
        $uniqueField = $this->schema->getDocumentUniqueField();
46
        $configEntityFields = $this->schema->getConfigEntityFields();
47
48
        foreach ($fields as $field) {
49
            $updateQuery->addField(
50
                $field->getEntityFieldName(),
51
                $field->getDocumentFieldValue($entity)
52
            );
53
        }
54
55
        foreach ($configEntityFields as $configField) {
56
            $updateQuery->addConfigField(
57
                $configField->getConfigFieldName(),
58
                $configField->getValue($this->entityConfig)
59
            );
60
        }
61
62
        $updateQuery->addUniqueFieldValue(
63
            $uniqueField->getValue($entity, $this->entityConfig)
64
        );
65
66
67
        $updateQuery->endEntity();
68
        $updateQuery->update();
69
    }
70
71
    /**
72
     * @param mixed $entity
73
     */
74
    public function removeByPrimaryKey($entity)
75
    {
76
        $uniqueField = $this->schema->getDocumentUniqueField();
77
        $updateQuery = $this->getUpdateQuery();
78
        $updateQuery->addDeleteCriteriaByUniqueFieldValue(
79
            $uniqueField->getValue($entity, $this->entityConfig)
80
        );
81
82
        $updateQuery->update();
83
    }
84
85
    /**
86
     * @return UpdateQuery
87
     */
88
    private function getUpdateQuery()
89
    {
90
        return $this->updateQuery->reset();
91
    }
92
}