Completed
Pull Request — master (#1)
by
unknown
13:02
created

ResultIterator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 57
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 16 5
A current() 0 17 4
1
<?php
2
3
namespace Sulu\Bundle\ElasticsearchActivityLogBundle\Storage;
4
5
use ONGR\ElasticsearchBundle\Result\DocumentIterator;
6
use Sulu\Bundle\ElasticsearchActivityLogBundle\Document\ActivityLoggerViewDocument;
7
use Sulu\Component\Security\Authentication\UserInterface;
8
9
/**
10
 * Take result iterator, parents and creator - and combines them.
11
 */
12
class ResultIterator extends \IteratorIterator
13
{
14
    /**
15
     * @var ActivityLoggerViewDocument[]
16
     */
17
    private $parents = [];
18
19
    /**
20
     * @var UserInterface[]
21
     */
22
    private $creators = [];
23
24
    /**
25
     * @param DocumentIterator $iterator
26
     * @param DocumentIterator $parents
27
     * @param array $creators
28
     */
29
    public function __construct(DocumentIterator $iterator, DocumentIterator $parents = null, array $creators = null)
30
    {
31
        parent::__construct($iterator);
32
33
        if (null !== $parents) {
34
            foreach ($parents as $parent) {
35
                $this->parents[$parent->getUuid()] = $parent;
36
            }
37
        }
38
39
        if (null !== $creators) {
40
            foreach ($creators as $creator) {
41
                $this->creators[$creator->getId()] = $creator;
42
            }
43
        }
44
    }
45
46
    /**
47
     * Set creator and parent to document.
48
     *
49
     * @return ActivityLoggerViewDocument
50
     */
51
    public function current()
52
    {
53
        $result = parent::current();
54
        if (!$result) {
55
            return $result;
56
        }
57
58
        $result->setParent(
59
            array_key_exists($result->getParentUuid(), $this->parents) ? $this->parents[$result->getParentUuid()] : null
60
        );
61
62
        $result->setCreator(
63
            array_key_exists($result->getCreatorId(), $this->creators) ? $this->creators[$result->getCreatorId()] : null
64
        );
65
66
        return $result;
67
    }
68
}
69