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

Composer   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 12
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 12
rs 10
c 1
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A __construct() 0 3 1
1
<?php declare(strict_types=1);
2
require_once __DIR__ . '/../vendor/autoload.php';
3
4
use Igni\Storage\Driver\Pdo\Connection;
5
use Igni\Storage\Driver\Pdo\ConnectionOptions;
6
use Igni\Storage\Driver\Pdo\Repository;
7
use Igni\Storage\Entity;
8
use Igni\Storage\Id\AutoGenerateId;
9
use Igni\Storage\Id\GenericId;
10
use Igni\Storage\Mapping\Annotation\Entity as Storable;
11
use Igni\Storage\Mapping\Annotation\Property;
12
use Igni\Storage\Mapping\MappingStrategy;
13
use Igni\Storage\Mapping\Type;
14
use Igni\Storage\Storage;
15
16
17
class ComposerType extends Property
18
{
19
    public function getType(): string
20
    {
21
        return 'composer';
22
    }
23
}
24
25
class ComposerMapping implements MappingStrategy
26
{
27
    public static function hydrate(&$value)
28
    {
29
        $value = new Composer($value);
30
    }
31
32
    public static function extract(&$value)
33
    {
34
        $value = (string) $value;
35
    }
36
}
37
38
Type::register('composer', ComposerMapping::class);
39
40
41
class Composer
42
{
43
    private $name;
44
45
    public function __construct(string $name)
46
    {
47
        $this->name = $name;
48
    }
49
50
    public function __toString(): string
51
    {
52
        return $this->name;
53
    }
54
}
55
56
/**
57
 * @Storable(source="tracks")
58
 */
59
class Track implements Entity
60
{
61
    use AutoGenerateId;
62
63
    /**
64
     * @Property(type="id", name="TrackId", class=GenericId::class)
65
     */
66
    protected $id;
67
68
    /**
69
     * @Property(type="string", name="Name")
70
     */
71
    protected $name;
72
73
    /**
74
     * @Property(name="Composer", type="composer")
75
     */
76
    protected $composer;
77
78
    public function __construct(string $name, Composer $composer)
79
    {
80
        $this->name = $name;
81
        $this->composer = $composer;
82
    }
83
84
    public function getComposer(): Composer
85
    {
86
        return $this->composer;
87
    }
88
89
    public function getName(): string
90
    {
91
        return $this->name;
92
    }
93
}
94
95
$sqlLiteConnection = new Connection(__DIR__ . '/db.db', new ConnectionOptions('sqlite'));
96
$sqlLiteConnection->open();
97
$unitOfWork = new Storage();
98
$unitOfWork->addRepository(new class($sqlLiteConnection, $unitOfWork->getEntityManager()) extends Repository {
99
    public function getEntityClass(): string
100
    {
101
        return Track::class;
102
    }
103
});
104
105
$track = $unitOfWork->get(Track::class, 1);
106
107
$track->getComposer();// Instance of composer.
0 ignored issues
show
Bug introduced by
The method getComposer() 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 or Track. ( Ignorable by Annotation )

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

107
$track->/** @scrutinizer ignore-call */ 
108
        getComposer();// Instance of composer.
Loading history...
108