TrackHydrator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
require_once __DIR__ . '/../vendor/autoload.php';
3
4
use Igni\Storage\Driver\ConnectionManager;
5
use Igni\Storage\Driver\Pdo\Connection;
6
use Igni\Storage\Driver\Pdo\Repository;
7
use Igni\Storage\Hydration\GenericHydrator;
8
use Igni\Storage\Hydration\ObjectHydrator;
9
use Igni\Storage\Id;
10
use Igni\Storage\Id\GenericId;
11
use Igni\Storage\Mapping\Annotation\Entity;
12
use Igni\Storage\Mapping\Annotation\Property;
13
use Igni\Storage\Storable;
14
use Igni\Storage\Storage;
15
16
/**
17
 * This is your custom hydrator - it implements decorator pattern.
18
 * $baseHydrator is a default hydrator generated by the framework,
19
 * it contains base hydration logic which can be extended in this class.
20
 */
21
final class TrackHydrator implements ObjectHydrator
22
{
23
    private $baseHydrator;
24
25
    public function __construct(GenericHydrator $baseHydrator)
26
    {
27
        $this->baseHydrator = $baseHydrator;
28
    }
29
30
    public function hydrate(array $data)
31
    {
32
        /** @var Track $entity */
33
        $entity = $this->baseHydrator->hydrate($data);
34
        $entity->setAlbum('Unknown album');
0 ignored issues
show
Bug introduced by
The method setAlbum() does not exist on Track. ( Ignorable by Annotation )

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

34
        $entity->/** @scrutinizer ignore-call */ 
35
                 setAlbum('Unknown album');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
35
        // Here do custom hydration
36
        return $entity;
37
    }
38
39
    public function extract($entity): array
40
    {
41
        $data = $this->baseHydrator->extract($entity);
42
        // Here extract additional properties
43
        return $data;
44
    }
45
}
46
47
/**
48
 * This entity will be hydrated with our custom hydrator class.
49
 * Please take a careful look at the annotation below- it is using hydrator
50
 * property so framework knows which class should be used during the hydration.
51
 *
52
 * @Entity(source="tracks", hydrator=TrackHydrator::class)
53
 */
54
class Track implements Storable
55
{
56
    /**
57
     * @Property(type="id", name="TrackId", class=GenericId::class)
58
     */
59
    protected $id;
60
61
    /**
62
     * @Property(type="string", name="Name")
63
     */
64
    protected $name;
65
66
    /**
67
     * Please note: that this property is not hydrated by generic hydrator.
68
     * It gets hydrated by TrackHydrator.
69
     */
70
    protected $album;
71
72
    public function __construct(string $name)
73
    {
74
        $this->name = $name;
75
    }
76
77
    public function setAlbum(string $album)
78
    {
79
        $this->album = $album;
80
    }
81
82
    public function getAlbum()
83
    {
84
        return $this->album;
0 ignored issues
show
Bug Best Practice introduced by
The property album does not exist on Track. Did you maybe forget to declare it?
Loading history...
85
    }
86
87
    public function getId(): Id
88
    {
89
        return $this->id;
90
    }
91
}
92
93
// Below we setup bootstrap; connection and unit of work instance (Storage instance)
94
95
ConnectionManager::registerDefault(new Connection('sqlite:/' . __DIR__ . '/db.db'));
96
97
$unitOfWork = new Storage();
98
99
// Repository has to be registered so framework knows which class is responsible for obtaining given entity from database.
100
$unitOfWork->addRepository(new class($unitOfWork->getEntityManager()) extends Repository {
101
    public static function getEntityClass(): string
102
    {
103
        return Track::class;
104
    }
105
});
106
107
// Now database is queried and data gets hydrated with custom hydrator to instance of Track class.
108
$track = $unitOfWork->get(Track::class, 1);
109
110
echo $track->getAlbum();// Unknown album.
0 ignored issues
show
Bug introduced by
The method getAlbum() does not exist on Igni\Storage\Storable. It seems like you code against a sub-type of Igni\Storage\Storable such as Igni\Tests\Fixtures\Track\TrackEntity. ( Ignorable by Annotation )

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

110
echo $track->/** @scrutinizer ignore-call */ getAlbum();// Unknown album.
Loading history...
111