Completed
Push — master ( 8e37af...dfcc9a )
by Simonas
02:29
created

ObjectIterator::offsetGet()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 13
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 13
loc 13
rs 9.4285
cc 3
eloc 7
nc 2
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\Result;
13
14
use ONGR\ElasticsearchBundle\Collection\Collection;
15
16
/**
17
 * ObjectIterator class.
18
 */
19
class ObjectIterator extends Collection
20
{
21
    /**
22
     * @var Converter
23
     */
24
    private $converter;
25
26
    /**
27
     * @var array Aliases information.
28
     */
29
    private $alias;
30
31
    /**
32
     * @var array
33
     */
34
    private $rawObjects;
35
36
    /**
37
     * Using part of abstract iterator functionality only.
38
     *
39
     * @param Converter $converter
40
     * @param array     $objects
41
     * @param array     $alias
42
     */
43
    public function __construct($converter, $objects, $alias)
44
    {
45
        $this->converter = $converter;
46
        $this->rawObjects = $objects;
47
        $this->alias = $alias;
48
49
        $callback = function ($v) {
0 ignored issues
show
Unused Code introduced by
The parameter $v is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
50
            return null;
51
        };
52
53
        // Pass array with available keys and no values
54
        parent::__construct(array_map($callback, $objects));
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    protected function convertDocument(array $document)
61
    {
62
        return $this->converter->assignArrayToObject(
63
            $document,
64
            new $this->alias['namespace'](),
65
            $this->alias['aliases']
66
        );
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 View Code Duplication
    public function current()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    {
74
        $value = parent::current();
75
76
        // Generate objects on demand
77
        if ($value === null && $this->valid()) {
78
            $key = $this->key();
79
            $value = $this->convertDocument($this->rawObjects[$key]);
80
            $this->rawObjects[$key] = null;
81
            $this->offsetSet($key, $value);
82
        }
83
84
        return $value;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 View Code Duplication
    public function offsetGet($offset)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92
        $value = parent::offsetGet($offset);
93
94
        // Generate objects on demand
95
        if ($value === null && $this->valid()) {
96
            $value = $this->convertDocument($this->rawObjects[$offset]);
97
            $this->rawObjects[$offset] = null;
98
            $this->offsetSet($offset, $value);
99
        }
100
101
        return $value;
102
    }
103
}
104