ComposerMapping   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 10
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 3
c 2
b 0
f 0
dl 0
loc 10
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hydrate() 0 3 1
A extract() 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\Storable;
8
use Igni\Storage\Id\AutoGenerateId;
9
use Igni\Storage\Id\GenericId;
10
use Igni\Storage\Mapping\Annotation\Entity;
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
use Igni\Storage\Driver\ConnectionManager;
16
17
class ComposerMapping implements MappingStrategy
18
{
19
    public static function hydrate(&$value)
20
    {
21
        $value = new Composer($value);
22
    }
23
24
    public static function extract(&$value)
25
    {
26
        $value = (string) $value;
27
    }
28
}
29
30
Type::register('composer', ComposerMapping::class);
31
32
33
class Composer
34
{
35
    private $name;
36
37
    public function __construct(string $name)
38
    {
39
        $this->name = $name;
40
    }
41
42
    public function __toString(): string
43
    {
44
        return $this->name;
45
    }
46
}
47
48
/**
49
 * @Entity(source="tracks")
50
 */
51
class Track implements Storable
52
{
53
    use AutoGenerateId;
54
55
    /**
56
     * @Property(type="id", name="TrackId", class=GenericId::class)
57
     */
58
    protected $id;
59
60
    /**
61
     * @Property(type="string", name="Name")
62
     */
63
    protected $name;
64
65
    /**
66
     * @Property(name="Composer", type="composer")
67
     */
68
    protected $composer;
69
70
    public function __construct(string $name, Composer $composer)
71
    {
72
        $this->name = $name;
73
        $this->composer = $composer;
74
    }
75
76
    public function getComposer(): Composer
77
    {
78
        return $this->composer;
0 ignored issues
show
Bug Best Practice introduced by
The property composer does not exist on Track. Did you maybe forget to declare it?
Loading history...
79
    }
80
81
    public function getName(): string
82
    {
83
        return $this->name;
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Track. Did you maybe forget to declare it?
Loading history...
84
    }
85
}
86
87
ConnectionManager::registerDefault(new Connection('sqlite:/' . __DIR__ . '/db.db'));
88
$unitOfWork = new Storage();
89
$unitOfWork->addRepository(new class($unitOfWork->getEntityManager()) extends Repository {
90
    public static function getEntityClass(): string
91
    {
92
        return Track::class;
93
    }
94
});
95
96
$track = $unitOfWork->get(Track::class, 1);
97
98
print_r($track->getComposer());// Instance of composer.
0 ignored issues
show
Bug introduced by
The method getComposer() 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

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