Passed
Push — master ( 2b47f2...d0c705 )
by Jesse
02:10
created

SimpleTable   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
dl 0
loc 60
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A from() 0 11 4
A converter() 0 6 1
A __construct() 0 8 1
A loadInto() 0 12 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\TableLoader;
5
6
use Stratadox\Hydrator\Hydrates;
7
use Stratadox\IdentityMap\IdentityMap;
8
use Stratadox\IdentityMap\MapsObjectsByIdentity as Map;
9
use Stratadox\TableLoader\ContainsResultingObjects as Objects;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Stratadox\TableLoader\Objects. 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...
10
use Throwable;
11
12
/**
13
 * Converts a table into objects.
14
 *
15
 * @author Stratadox
16
 */
17
final class SimpleTable implements LoadsTable
18
{
19
    private $label;
20
    private $make;
21
    private $identity;
22
23
    private function __construct(
24
        string $label,
25
        Hydrates $hydrator,
26
        IdentifiesEntities $identity
27
    ) {
28
        $this->label = $label;
29
        $this->make = $hydrator;
30
        $this->identity = $identity;
31
    }
32
33
    /**
34
     * Makes a new simple table converter.
35
     *
36
     * @param string             $label    The label to apply.
37
     * @param Hydrates           $hydrator The hydrator that produces the objects.
38
     * @param IdentifiesEntities $identity The row identification mechanism.
39
     *
40
     * @return SimpleTable                 The simple table converter.
41
     */
42
    public static function converter(
43
        string $label,
44
        Hydrates $hydrator,
45
        IdentifiesEntities $identity
46
    ): self {
47
        return new self($label, $hydrator, $identity);
48
    }
49
50
    /** @inheritdoc */
51
    public function from(array $input, Map $map = null): Objects
52
    {
53
        $result = Result::fromArray([], $map ?: IdentityMap::startEmpty());
54
        foreach ($input as $row) {
55
            try {
56
                $result = $this->loadInto($result, $row);
57
            } catch (Throwable $exception) {
58
                throw UnmappableRow::encountered($exception, $this->label, $row);
59
            }
60
        }
61
        return $result;
62
    }
63
64
    /** @throws Throwable */
65
    private function loadInto(Objects $result, array $row): Objects
66
    {
67
        $class = $this->make->classFor($row);
68
        $id = $this->identity->forIdentityMap($row);
69
        if ($result->has($class, $id)) {
70
            return $result->include($this->label, $id, $result->get($class, $id));
71
        }
72
        return $result->add(
73
            $this->label,
74
            $this->identity->forLoading($row),
75
            $id,
76
            $this->make->fromArray($row)
77
        );
78
    }
79
}
80