GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( a11bd1...3fc1f5 )
by Anderson
02:01
created

QueryListener::insertParentsData()   C

Complexity

Conditions 7
Paths 3

Size

Total Lines 32
Code Lines 19

Duplication

Lines 21
Ratio 65.63 %

Importance

Changes 0
Metric Value
dl 21
loc 32
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 19
nc 3
nop 1
1
<?php
2
3
namespace DoctrineElastic\Listener;
4
5
use Doctrine\ORM\Mapping\JoinColumn;
6
use Doctrine\ORM\Mapping\JoinColumns;
7
use Doctrine\ORM\Mapping\ManyToOne;
8
use DoctrineElastic\Event\QueryEventArgs;
9
use DoctrineElastic\Hydrate\AnnotationEntityHydrator;
10
11
/**
12
 * Query events main handler for this extension
13
 *
14
 * @author Ands
15
 */
16
class QueryListener {
17
18
    /** @var AnnotationEntityHydrator */
19
    protected $hydrator;
20
21
    public function __construct() {
22
        $this->hydrator = new AnnotationEntityHydrator();
23
    }
24
25
    public function beforeQuery(QueryEventArgs $eventArgs) {
0 ignored issues
show
Unused Code introduced by
The parameter $eventArgs 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...
26
27
    }
28
29
    /**
30
     * @param QueryEventArgs $eventArgs
31
     */
32
    public function postQuery(QueryEventArgs $eventArgs) {
33
        $results = $eventArgs->getResults();
34
        $entityManager = $eventArgs->getEntityManager();
35
        $targetEntity = $eventArgs->getTargetEntity();
36
37
        if (!empty($results) && boolval($entityManager) && boolval($targetEntity)) {
38
            $this->executeRelationshipQueries($eventArgs);
39
        }
40
    }
41
42
    /**
43
     * Makes a heavy work, simulating a doctrine 1-n entity relationship
44
     * This recourse must be used cautiously. Check if your application really needs that.
45
     *
46
     * @param QueryEventArgs $eventArgs
47
     */
48
    private function executeRelationshipQueries(QueryEventArgs $eventArgs) {
49
        $targetClass = $eventArgs->getTargetEntity();
50
        $entityManager = $eventArgs->getEntityManager();
51
        $results = $eventArgs->getResults();
52
53
        /** @var ManyToOne[] $manyToOnes */
54
        $manyToOnes = $this->hydrator->extractSpecAnnotations($targetClass, ManyToOne::class);
55
        /** @var JoinColumns[] $joinsColumns */
56
        $joinsColumns = $this->hydrator->extractSpecAnnotations($targetClass, JoinColumns::class);
57
58 View Code Duplication
        foreach ($manyToOnes as $propName => $mto) {
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...
59
            if (!isset($joinsColumns[$propName])) {
60
                continue;
61
            }
62
63
            foreach ($joinsColumns as $joinsColumn) {
64
                /** @var JoinColumn[] $joinColumns */
65
                $joinColumns = $joinsColumn->value;
66
                foreach ($joinColumns as $joinColumn) {
67
                    $colunmName = AnnotationEntityHydrator::camelizeString($joinColumn->referencedColumnName);
68
                    foreach ($results as $key => $result) {
69
                        if (property_exists(get_class($result), $colunmName)) {
70
                            $value = $this->hydrator->extract($result, $colunmName);
71
                            $relObject = $entityManager->getRepository($mto->targetEntity)
72
                                ->findOneBy([$colunmName => $value]);
73
                            $this->hydrator->hydrate($results[$key], [$colunmName => $relObject]);
74
                        }
75
                    }
76
                }
77
            }
78
        }
79
80
        $eventArgs->setResults($results);
81
    }
82
83
    /**
84
     * Fetch and inject the parent object to the child ones from query
85
     *
86
     * @param QueryEventArgs $eventArgs
87
     */
88
    private function insertParentsData(QueryEventArgs $eventArgs) {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
89
        $targetClass = $eventArgs->getTargetEntity();
90
        $entityManager = $eventArgs->getEntityManager();
91
        $results = $eventArgs->getResults();
92
93
        /** @var ManyToOne[] $manyToOnes */
94
        $manyToOnes = $this->hydrator->extractSpecAnnotations($targetClass, ManyToOne::class);
95
96 View Code Duplication
        foreach ($manyToOnes as $propName => $mto) {
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...
97
            if (!isset($joinsColumns[$propName])) {
98
                continue;
99
            }
100
101
            foreach ($joinsColumns as $joinsColumn) {
102
                /** @var JoinColumn[] $joinColumns */
103
                $joinColumns = $joinsColumn->value;
104
                foreach ($joinColumns as $joinColumn) {
105
                    $colunmName = AnnotationEntityHydrator::camelizeString($joinColumn->referencedColumnName);
106
                    foreach ($results as $key => $result) {
107
                        if (property_exists(get_class($result), $colunmName)) {
108
                            $value = $this->hydrator->extract($result, $colunmName);
109
                            $relObject = $entityManager->getRepository($mto->targetEntity)
110
                                ->findOneBy([$colunmName => $value]);
111
                            $this->hydrator->hydrate($results[$key], [$colunmName => $relObject]);
112
                        }
113
                    }
114
                }
115
            }
116
        }
117
118
        $eventArgs->setResults($results);
119
    }
120
}