Passed
Push — master ( b97c16...98e719 )
by Dawid
04:21
created

TrackHydrator::extract()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
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\Entity;
5
use Igni\Storage\Storage;
6
use Igni\Storage\Id\GenericId;
7
use Igni\Storage\Mapping\Annotation\Entity as Storable;
8
use Igni\Storage\Mapping\Annotation\Property;
9
use Igni\Storage\Id\AutoGenerateId;
10
use Igni\Storage\Hydration\ObjectHydrator;
11
use Igni\Storage\Hydration\GenericHydrator;
12
use Igni\Storage\Driver\Pdo\Connection;
13
use Igni\Storage\Driver\Pdo\ConnectionOptions;
14
use Igni\Storage\Driver\Pdo\Repository;
15
16
17
final class TrackHydrator implements ObjectHydrator
18
{
19
    private $baseHydrator;
20
21
    public function __construct(GenericHydrator $baseHydrator)
22
    {
23
        $this->baseHydrator = $baseHydrator;
24
    }
25
26
    public function hydrate(array $data)
27
    {
28
        /** @var Track $entity */
29
        $entity = $this->baseHydrator->hydrate($data);
30
        $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

30
        $entity->/** @scrutinizer ignore-call */ 
31
                 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...
31
        // Here do custom hydration
32
        return $entity;
33
    }
34
35
    public function extract($entity): array
36
    {
37
        $data = $this->baseHydrator->extract($entity);
38
        // Here extract additional properties
39
        return $data;
40
    }
41
}
42
43
/**
44
 * @Storable(source="tracks", hydrator=TrackHydrator::class)
45
 */
46
class Track implements Entity
47
{
48
    use AutoGenerateId;
49
50
    /**
51
     * @Property(type="id", name="TrackId", class=GenericId::class)
52
     */
53
    protected $id;
54
55
    /**
56
     * @Property(type="string", name="Name")
57
     */
58
    protected $name;
59
60
    /**
61
     * @Property(name="Composer")
62
     */
63
    protected $composer;
64
65
    /**
66
     * @Property(type="int", name="Milliseconds")
67
     */
68
    protected $length;
69
70
    /**
71
     * @Property(type="int", name="Bytes")
72
     */
73
    protected $size;
74
75
    protected $album;
76
77
    public function __construct(string $name, string $composer)
78
    {
79
        $this->name = $name;
80
        $this->composer = $composer;
81
        $this->length = 0;
82
        $this->size = 0;
83
    }
84
85
    public function getComposer(): string
86
    {
87
        return $this->composer;
88
    }
89
90
    public function getName(): string
91
    {
92
        return $this->name;
93
    }
94
95
    public function getLength(): int
96
    {
97
        return $this->length;
0 ignored issues
show
Bug Best Practice introduced by
The property length does not exist on Track. Did you maybe forget to declare it?
Loading history...
98
    }
99
100
    public function getSize(): int
101
    {
102
        return $this->size;
0 ignored issues
show
Bug Best Practice introduced by
The property size does not exist on Track. Did you maybe forget to declare it?
Loading history...
103
    }
104
105
    public function setAlbum(string $album)
106
    {
107
        $this->album = $album;
108
    }
109
110
    public function getAlbum()
111
    {
112
        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...
113
    }
114
}
115
116
$sqlLiteConnection = new Connection(__DIR__ . '/db.db', new ConnectionOptions('sqlite'));
117
$sqlLiteConnection->open();
118
$unitOfWork = new Storage();
119
$unitOfWork->addRepository(new class($sqlLiteConnection, $unitOfWork->getEntityManager()) extends Repository {
120
    public function getEntityClass(): string
121
    {
122
        return Track::class;
123
    }
124
});
125
126
$track = $unitOfWork->get(Track::class, 1);
127
128
$track->getAlbum();// Unknown album.
0 ignored issues
show
Bug introduced by
The method getAlbum() does not exist on Igni\Storage\Entity. It seems like you code against a sub-type of Igni\Storage\Entity such as IgniTest\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

128
$track->/** @scrutinizer ignore-call */ 
129
        getAlbum();// Unknown album.
Loading history...
129