Completed
Push — wip-platform ( 1b7118...2b724b )
by
unknown
03:32
created

Iterator::getQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Blast Project package.
5
 *
6
 * Copyright (C) 2015-2017 Libre Informatique
7
 *
8
 * This file is licenced under the GNU LGPL v3.
9
 * For the full copyright and license information, please view the LICENSE.md
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Blast\Bundle\CoreBundle\DataSource;
14
15
use Exporter\Source\DoctrineORMQuerySourceIterator;
16
use Doctrine\ORM\PersistentCollection;
17
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
18
19
class Iterator extends DoctrineORMQuerySourceIterator
20
{
21
    /**
22
     * {@inheritdoc}
23
     * Allows to get back embedded arrays, in 2 dimensions max.
24
     * e.g.: fields positions.name & positions.organism.name will be returned as :
25
     *       [
26
     *          'name' => 'myEntity',
27
     *          'positions' => [
28
     *              ['name' => 'XXX', 'organism.name' => 'YYY'],
29
     *              ['name' => 'AAA', 'organism.name' => 'BBB'],
30
     *          ]
31
     *       ];.
32
     */
33
    public function current()
34
    {
35
        $data = [];
36
        $propertyPaths = $this->propertyPaths;
37
38
        // then complete it for "fields" representing a collection of sub-entities
39
        foreach ($this->propertyPaths as $i => $propertyPath) {
40
            try {
41
                if (($property = $this->propertyAccessor->getValue($this->iterator->current()[0], $propertyPath)) instanceof PersistentCollection) {
0 ignored issues
show
Bug introduced by
The method getValue() does not seem to exist on object<Symfony\Component...yAccess\PropertyAccess>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42 View Code Duplication
                    if (!(isset($data[(string) $propertyPath]) && is_array($data[(string) $propertyPath]))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
43
                        $data[(string) $propertyPath] = [];
44
                    }
45
46
                    foreach ($property as $subEntity) {
47
                        $data[(string) $propertyPath][] = (string) $subEntity;
48
                    }
49
50
                    unset($this->propertyPaths[$i]);
51
                }
52
            } catch (NoSuchPropertyException $e) {
53
                $collection = preg_replace('/\..+$/', '', $propertyPath);
54
                $subProperty = preg_replace('/^.+\./U', '', $propertyPath);
55
                if ($collection != (string) $propertyPath && $subProperty) {
56
                    if (($property = $this->propertyAccessor->getValue($this->iterator->current()[0], $collection)) instanceof PersistentCollection) {
0 ignored issues
show
Bug introduced by
The method getValue() does not seem to exist on object<Symfony\Component...yAccess\PropertyAccess>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57 View Code Duplication
                        if (!(isset($data[$collection]) && is_array($data[$collection]))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
58
                            $data[$collection] = [];
59
                        }
60
61
                        foreach ($property as $subEntity) {
62
                            $data[$collection][spl_object_hash($subEntity)][$subProperty] = (string) $this->propertyAccessor->getValue($subEntity, $subProperty);
0 ignored issues
show
Bug introduced by
The method getValue() does not seem to exist on object<Symfony\Component...yAccess\PropertyAccess>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63
                        }
64
                    }
65
                }
66
                unset($this->propertyPaths[$i]);
67
            }
68
        }
69
70
        // first do the "normal" stuff
71
        $data = array_merge($data, parent::current());
72
73
        $this->propertyPaths = $propertyPaths;
74
75
        return $data;
76
    }
77
78
    /**
79
     * getQuery().
80
     *
81
     * @return Doctrine\ORM\Query
82
     **/
83
    public function getQuery()
84
    {
85
        return $this->query;
86
    }
87
}
88