Passed
Push — dev_2x ( 1e248d...fedccd )
by Adrian
01:40
created

AbstractHydrator   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
eloc 26
c 1
b 0
f 0
dl 0
loc 88
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getPk() 0 3 1
A getMapperConfig() 0 3 1
A extract() 0 12 2
B compileRelations() 0 21 9
A setPk() 0 3 1
A setMapper() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Sirius\Orm\Entity;
5
6
use Sirius\Orm\CastingManager;
7
use Sirius\Orm\Contract\EntityInterface;
8
use Sirius\Orm\Contract\HydratorInterface;
9
use Sirius\Orm\Contract\Relation\ToManyInterface;
10
use Sirius\Orm\Contract\Relation\ToOneInterface;
11
use Sirius\Orm\Helpers\Arr;
12
use Sirius\Orm\Mapper;
13
use Sirius\Orm\MapperConfig;
14
15
abstract class AbstractHydrator implements HydratorInterface
16
{
17
    /**
18
     * @var CastingManager
19
     */
20
    protected $castingManager;
21
22
    /**
23
     * @var Mapper
24
     */
25
    protected $mapper;
26
27
    public function __construct(CastingManager $castingManager)
28
    {
29
        $this->castingManager = $castingManager;
30
    }
31
32
    public function setMapper(Mapper $mapper)
33
    {
34
        $this->mapper = $mapper;
35
    }
36
37
    /**
38
     * @param array $attributes
39
     *
40
     * @return mixed|GenericEntity
41
     */
42
    abstract public function hydrate(array $attributes = []);
43
44
    /**
45
     * @param EntityInterface $entity
46
     *
47
     * @return array
48
     */
49
    public function extract(EntityInterface $entity)
50
    {
51
        $data = Arr::renameKeys(
52
            $entity->toArray(),
53
            array_flip($this->getMapperConfig()->getColumnAttributeMap())
54
        );
55
        if ($this->castingManager) {
56
            $data = $this->castingManager
57
                ->castArrayForDb($data, $this->getMapperConfig()->getCasts());
58
        }
59
60
        return Arr::only($data, $this->getMapperConfig()->getColumns());
61
    }
62
63
    abstract public function get(EntityInterface $entity, string $attribute);
64
65
    abstract public function set(EntityInterface $entity, string $attribute, $value);
66
67
    public function getPk($entity)
68
    {
69
        return $this->get($entity, $this->getMapperConfig()->getPrimaryKey());
0 ignored issues
show
Bug introduced by
It seems like $this->getMapperConfig()->getPrimaryKey() can also be of type array; however, parameter $attribute of Sirius\Orm\Entity\AbstractHydrator::get() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

69
        return $this->get($entity, /** @scrutinizer ignore-type */ $this->getMapperConfig()->getPrimaryKey());
Loading history...
70
    }
71
72
    public function setPk($entity, $value)
73
    {
74
        return $this->set($entity, $this->getMapperConfig()->getPrimaryKey(), $value);
0 ignored issues
show
Bug introduced by
It seems like $this->getMapperConfig()->getPrimaryKey() can also be of type array; however, parameter $attribute of Sirius\Orm\Entity\AbstractHydrator::set() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

74
        return $this->set($entity, /** @scrutinizer ignore-type */ $this->getMapperConfig()->getPrimaryKey(), $value);
Loading history...
75
    }
76
77
    protected function compileRelations(array $attributes)
78
    {
79
        foreach ($this->mapper->getRelations() as $name) {
80
            $relation = $this->mapper->getRelation($name);
81
            if ($relation instanceof ToOneInterface &&
82
                isset($attributes[$name]) &&
83
                ! is_object($attributes[$name])) {
84
                $attributes[$name] = $relation->getForeignMapper()->newEntity($attributes[$name]);
85
            } elseif ($relation instanceof ToManyInterface &&
86
                      ! $relation instanceof ToOneInterface
87
                      && ( ! isset($attributes[$name]) || is_array($attributes[$name]))) {
88
                /**
89
                 * we also need to check against ToOneInterface because OneToOne relation extends
90
                 * OneToMany which implements ToOneInterface
91
                 * @todo remove this quirk
92
                 */
93
                $attributes[$name] = $relation->getForeignMapper()->newCollection($attributes[$name] ?? []);
94
            }
95
        }
96
97
        return $attributes;
98
    }
99
100
    protected function getMapperConfig(): MapperConfig
101
    {
102
        return $this->mapper->getConfig();
103
    }
104
}
105