ManyRelation::delete()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 3
eloc 10
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Storage package
5
 *
6
 * (c) Michal Wachowski <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Moss\Storage\Query\Relation;
13
14
/**
15
 * One to many relation handler
16
 *
17
 * @author  Michal Wachowski <[email protected]>
18
 * @package Moss\Storage
19
 */
20
class ManyRelation extends AbstractRelation implements RelationInterface
21
{
22
    /**
23
     * Executes read for one-to-many relation
24
     *
25
     * @param array $result
26
     *
27
     * @return array
28
     */
29
    public function read(&$result)
30
    {
31
        $relations = [];
32
        $conditions = [];
33
34
        foreach ($result as $i => $entity) {
35
            $container = $this->accessor->getPropertyValue($entity, $this->definition->container());
36
            if (empty($container)) {
37
                $this->accessor->setPropertyValue($entity, $this->definition->container(), []);
38
            }
39
40
            foreach ($this->definition->keys() as $local => $refer) {
41
                $conditions[$refer][] = $this->accessor->getPropertyValue($entity, $local);
42
            }
43
44
            $relations[$this->buildLocalKey($entity, $this->definition->keys())][] = &$result[$i];
45
        }
46
47
        $collection = $this->fetch($this->definition->entity(), $conditions, true);
48
49 View Code Duplication
        foreach ($collection as $relEntity) {
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...
50
            $key = $this->buildForeignKey($relEntity, $this->definition->keys());
51
52
            if (!isset($relations[$key])) {
53
                continue;
54
            }
55
56
            foreach ($relations[$key] as &$entity) {
57
                $this->accessor->addPropertyValue($entity, $this->definition->container(), $relEntity);
58
                unset($entity);
59
            }
60
        }
61
62
        return $result;
63
    }
64
65
    /**
66
     * Executes write for one-to-many relation
67
     *
68
     * @param array|\ArrayAccess $result
69
     *
70
     * @return array|\ArrayAccess
71
     * @throws RelationException
72
     */
73
    public function write(&$result)
74
    {
75
        $container = $this->accessor->getPropertyValue($result, $this->definition->container());
76 View Code Duplication
        if (empty($container)) {
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...
77
            $conditions = [];
78
            foreach ($this->definition->keys() as $local => $foreign) {
79
                $conditions[$foreign][] = $this->accessor->getPropertyValue($result, $local);
80
            }
81
82
            $this->cleanup($this->definition->entity(), [], $conditions);
83
            return $result;
84
        }
85
86
        $this->assertArrayAccess($container);
87
88
        foreach ($container as $relEntity) {
89
            foreach ($this->definition->keys() as $local => $foreign) {
90
                $this->accessor->setPropertyValue($relEntity, $foreign, $this->accessor->getPropertyValue($result, $local));
91
            }
92
93
            $this->storage->write($relEntity, $this->definition->entity())->execute();
94
        }
95
96
        $this->accessor->setPropertyValue($result, $this->definition->container(), $container);
97
98
        $conditions = [];
99
        foreach ($this->definition->keys() as $local => $foreign) {
100
            $conditions[$foreign] = $this->accessor->getPropertyValue($result, $local);
101
        }
102
103
        $this->cleanup($this->definition->entity(), $container, $conditions);
104
105
        return $result;
106
    }
107
108
    /**
109
     * Executes delete for one-to-many relation
110
     *
111
     * @param array|\ArrayAccess $result
112
     *
113
     * @return array|\ArrayAccess
114
     * @throws RelationException
115
     */
116
    public function delete(&$result)
117
    {
118
        $container = $this->accessor->getPropertyValue($result, $this->definition->container());
119
        if (empty($container)) {
120
            return $result;
121
        }
122
123
        $this->assertArrayAccess($container);
124
125
        foreach ($container as $relEntity) {
126
            $this->storage->delete($this->definition->entity(), $relEntity)
0 ignored issues
show
Documentation introduced by
$this->definition->entity() is of type string, but the function expects a array|object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
127
                ->execute();
128
        }
129
130
        $this->accessor->setPropertyValue($result, $this->definition->container(), $container);
131
132
        return $result;
133
    }
134
}
135