Wire::it()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\TableLoader\Loader;
5
6
use Stratadox\TableLoader\Loader\ContainsResultingObjects as Result;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Stratadox\TableLoader\Loader\Result. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
8
/**
9
 * Wires a relationship together.
10
 *
11
 * @author Stratadox
12
 */
13
final class Wire implements WiresObjects
14
{
15
    private $from;
16
    private $to;
17
    private $relation;
18
    private $setter;
19
20
    private function __construct(
21
        KnowsWhereToLookFrom $from,
22
        KnowsWhereToLook $to,
23
        MakesConnections $relation
24
    ) {
25
        $this->from = $from;
26
        $this->to = $to;
27
        $this->relation = $relation;
28
        // @todo use hydrator!
29
        $this->setter = function (string $property, $value): void {
30
            $this->$property = $value;
31
        };
32
    }
33
34
    /**
35
     * Makes a new object that wires a relationship together.
36
     *
37
     * @param KnowsWhereToLookFrom $from     Identification for the source entity.
38
     * @param KnowsWhereToLook     $to       Identification for the target entity.
39
     * @param MakesConnections     $relation The type of relationship.
40
     * @return WiresObjects                  The wiring object.
41
     */
42
    public static function it(
43
        KnowsWhereToLookFrom $from,
44
        KnowsWhereToLook $to,
45
        MakesConnections $relation
46
    ): WiresObjects {
47
        return new Wire($from, $to, $relation);
48
    }
49
50
    /** @inheritdoc */
51
    public function wire(Result $objects, array $data): void
52
    {
53
        $relationships = $this->relation->load(
54
            $this->from,
55
            $data,
56
            $this->to,
57
            $objects
58
        );
59
        foreach ($relationships as $property => $relations) {
60
            $this->writeTo($objects[$this->from->label()], $property, $relations);
0 ignored issues
show
Bug introduced by
$objects[$this->from->label()] of type iterable is incompatible with the type array expected by parameter $objects of Stratadox\TableLoader\Loader\Wire::writeTo(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
            $this->writeTo(/** @scrutinizer ignore-type */ $objects[$this->from->label()], $property, $relations);
Loading history...
61
        }
62
    }
63
64
    private function writeTo(
65
        array $objects,
66
        string $property,
67
        array $relations
68
    ): void {
69
        foreach ($relations as $id => $relation) {
70
            if ($this->from->hereToo($objects[$id])) {
71
                $this->setter->call($objects[$id], $property, $relation);
72
            }
73
        }
74
    }
75
}
76