QueryResultCollection   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 81
Duplicated Lines 13.58 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 11
loc 81
wmc 4
lcom 1
cbo 3
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A current() 11 11 1
A initialize() 0 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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()
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...
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