1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Sulu. |
5
|
|
|
* |
6
|
|
|
* (c) MASSIVE ART WebServices GmbH |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sulu\Component\DocumentManager\Collection; |
13
|
|
|
|
14
|
|
|
use PHPCR\Query\QueryResultInterface; |
15
|
|
|
use Sulu\Component\DocumentManager\Event\HydrateEvent; |
16
|
|
|
use Sulu\Component\DocumentManager\Events; |
17
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Lazily hydrate query results. |
21
|
|
|
*/ |
22
|
|
|
class QueryResultCollection extends AbstractLazyCollection |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var EventDispatcherInterface |
26
|
|
|
*/ |
27
|
|
|
private $eventDispatcher; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var QueryResultInterface |
31
|
|
|
*/ |
32
|
|
|
private $result; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $locale; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
private $options; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var bool |
46
|
|
|
*/ |
47
|
|
|
private $initialized = false; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var null|string |
51
|
|
|
*/ |
52
|
|
|
private $primarySelector = null; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param QueryResultInterface $result |
56
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
57
|
|
|
* @param string $locale |
58
|
|
|
* @param array $options |
59
|
|
|
* @param null|string $primarySelector |
60
|
|
|
*/ |
61
|
|
|
public function __construct( |
62
|
|
|
QueryResultInterface $result, |
63
|
|
|
EventDispatcherInterface $eventDispatcher, |
64
|
|
|
$locale, |
65
|
|
|
$options = [], |
66
|
|
|
$primarySelector = null |
67
|
|
|
) { |
68
|
|
|
$this->result = $result; |
69
|
|
|
$this->eventDispatcher = $eventDispatcher; |
70
|
|
|
$this->locale = $locale; |
71
|
|
|
$this->options = $options; |
72
|
|
|
$this->primarySelector = $primarySelector; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
public function current() |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$this->initialize(); |
81
|
|
|
$row = $this->documents->current(); |
82
|
|
|
$node = $row->getNode($this->primarySelector); |
83
|
|
|
|
84
|
|
|
$hydrateEvent = new HydrateEvent($node, $this->locale, $this->options); |
85
|
|
|
$this->eventDispatcher->dispatch(Events::HYDRATE, $hydrateEvent); |
86
|
|
|
|
87
|
|
|
return $hydrateEvent->getDocument(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
protected function initialize() |
94
|
|
|
{ |
95
|
|
|
if (true === $this->initialized) { |
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->documents = $this->result->getRows(); |
100
|
|
|
$this->initialized = true; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
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.