Code Duplication    Length = 44-53 lines in 2 locations

Moss/Storage/Query/Relation/ManyTroughRelation.php 1 location

@@ 29-81 (lines=53) @@
26
     *
27
     * @return array
28
     */
29
    public function read(&$result)
30
    {
31
        $relations = [];
32
        $conditions = [];
33
34
        foreach ($result as $i => $entity) {
35
            foreach ($this->definition->localKeys() as $local => $refer) {
36
                $conditions[$refer][] = $this->accessor->getPropertyValue($entity, $local);
37
            }
38
39
            $relations[$this->buildLocalKey($entity, $this->definition->localKeys())][] = &$result[$i];
40
        }
41
42
        $collection = $this->fetch($this->definition->mediator(), $conditions, false);
43
44
// --- MEDIATOR START
45
46
        $mediator = [];
47
        $conditions = [];
48
        foreach ($collection as $entity) {
49
            foreach ($this->definition->foreignKeys() as $local => $refer) {
50
                $conditions[$refer][] = $this->accessor->getPropertyValue($entity, $local);
51
            }
52
53
            $in = $this->buildForeignKey($entity, $this->definition->localKeys());
54
            $out = $this->buildLocalKey($entity, $this->definition->foreignKeys());
55
            $mediator[$out][] = $in;
56
        }
57
58
        $collection = $this->fetch($this->definition->entity(), $conditions, true);
59
60
// --- MEDIATOR END
61
62
        foreach ($collection as $relEntity) {
63
            $key = $this->buildForeignKey($relEntity, $this->definition->foreignKeys());
64
65
            if (!isset($mediator[$key])) {
66
                continue;
67
            }
68
            foreach ($mediator[$key] as $mkey) {
69
                if (!isset($relations[$mkey])) {
70
                    continue;
71
                }
72
73
                foreach ($relations[$mkey] as &$entity) {
74
                    $this->accessor->addPropertyValue($entity, $this->definition->container(), $relEntity);
75
                    unset($entity);
76
                }
77
            }
78
        }
79
80
        return $result;
81
    }
82
83
    /**
84
     * Executes write for one-to-many relation

Moss/Storage/Query/Relation/OneTroughRelation.php 1 location

@@ 29-72 (lines=44) @@
26
     *
27
     * @return array
28
     */
29
    public function read(&$result)
30
    {
31
        $relations = [];
32
        $conditions = [];
33
34
        foreach ($result as $i => $entity) {
35
            foreach ($this->definition->localKeys() as $local => $refer) {
36
                $conditions[$refer][] = $this->accessor->getPropertyValue($entity, $local);
37
            }
38
39
            $relations[$this->buildLocalKey($entity, $this->definition->localKeys())][] = & $result[$i];
40
        }
41
42
        $collection = $this->fetch($this->definition->mediator(), $conditions, false);
43
44
        $mediator = [];
45
        $conditions = [];
46
        foreach ($collection as $entity) {
47
            foreach ($this->definition->foreignKeys() as $local => $refer) {
48
                $conditions[$refer][] = $this->accessor->getPropertyValue($entity, $local);
49
            }
50
51
            $in = $this->buildForeignKey($entity, $this->definition->localKeys());
52
            $out = $this->buildLocalKey($entity, $this->definition->foreignKeys());
53
            $mediator[$out] = $in;
54
        }
55
56
        $collection = $this->fetch($this->definition->entity(), $conditions, true);
57
58
        foreach ($collection as $relEntity) {
59
            $key = $this->buildForeignKey($relEntity, $this->definition->foreignKeys());
60
61
            if (!isset($mediator[$key]) || !isset($relations[$mediator[$key]])) {
62
                continue;
63
            }
64
65
            foreach ($relations[$mediator[$key]] as &$entity) {
66
                $this->accessor->setPropertyValue($entity, $this->definition->container(), $relEntity);
67
                unset($entity);
68
            }
69
        }
70
71
        return $result;
72
    }
73
74
    /**
75
     * Executes write fro one-to-one relation