Completed
Pull Request — master (#660)
by
unknown
02:31
created

IdSetter::addIds()   C

Complexity

Conditions 8
Paths 6

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 26
rs 5.3846
cc 8
eloc 15
nc 6
nop 1
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\ElasticsearchBundle\Service;
13
14
use ONGR\ElasticsearchBundle\Mapping\MetadataCollector;
15
16
/**
17
 * Sets ids to persisted documents
18
 */
19
class IdSetter
20
{
21
    /**
22
     * Documents that are added to bulk
23
     * @var object[]
24
     */
25
    private $persistedDocuments;
26
27
    /**
28
     * Mappings of the persisted documents types
29
     * @var array
30
     */
31
    private $mappings;
32
33
    /**
34
     * @var MetadataCollector
35
     */
36
    private $collector;
37
38
    public function __construct(MetadataCollector $collector)
39
    {
40
        $this->collector = $collector;
41
    }
42
43
    /**
44
     * @param object $document
45
     */
46
    public function persist($document)
47
    {
48
        $class = get_class($document);
49
50
        if (!isset($this->mappings[$class])) {
51
            $mapping = $this->collector->getMapping($class);
52
            if (isset($mapping['aliases']['_id'])) {
53
                $this->mappings[$class] = $mapping['aliases']['_id'];
54
            } else {
55
                return;
56
            }
57
        }
58
59
        $idMapping = $this->mappings[$class];
60
61
        if ($idMapping['propertyType'] == 'public') {
62
            $id = $document->{$idMapping['propertyName']};
63
        } else {
64
            $id = $document->{$idMapping['methods']['getter']}();
65
        }
66
67
        if (!$id) {
68
            $this->persistedDocuments[] = $document;
69
        }
70
    }
71
72
    /**
73
     * @param array $response
74
     */
75
    public function addIds(array $response)
76
    {
77
        if (empty($this->persistedDocuments) || $response['errors']) {
78
            return;
79
        }
80
81
        foreach ($response['items'] as $object) {
82
            if (isset($object['create'])) {
83
                $document = current($this->persistedDocuments);
84
85
                if (!$document ||
86
                    !isset($this->mappings[ $class = get_class($document)])) {
87
                    continue;
88
                }
89
90
                if ($this->mappings[$class]['propertyType'] == 'public') {
91
                    $document->{$this->mappings[$class]['propertyName']} = $object['create']['_id'];
92
                } else {
93
                    $document->{$this->mappings[$class]['methods']['setter']}($object['create']['_id']);
94
                }
95
96
                next($this->persistedDocuments);
97
            }
98
        }
99
        $this->persistedDocuments = [];
100
    }
101
}
102