Completed
Push — master ( 98e719...571bb7 )
by Dawid
06:37
created

TrackEntity   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 14
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
1
<?php declare(strict_types=1);
2
require_once __DIR__ . '/../vendor/autoload.php';
3
4
use Igni\Storage\Mapping\Annotation\Entity;
5
use Igni\Storage\Mapping\Annotation\Property;
6
use Igni\Storage\Id\AutoGenerateId;
7
use Igni\Storage\Driver\Pdo\Repository;
8
use Igni\Storage\Driver\Pdo\Connection;
9
use Igni\Storage\Driver\Pdo\ConnectionOptions;
10
use Igni\Storage\Storage;
11
use Igni\Storage\Mapping\Collection\LazyCollection;
12
use Igni\Storage\Id\GenericId;
13
use Igni\Storage\Storable;
14
15
16
// Define connections:
17
$sqliteConnection = new Connection(__DIR__ . '/db.db', new ConnectionOptions(
18
    $type = 'sqlite'
19
));
20
$sqliteConnection->open();
21
22
23
// Define entities:
24
/** @Entity(source="tracks") */
25
class TrackEntity implements Storable
26
{
27
    use AutoGenerateId;
28
    /** @Property\Id(class=GenericId::class, name="TrackId") */
29
    public $id;
30
    /** @Property\Text(name="Name") */
31
    public $title;
32
    /** @Property\Reference(ArtistEntity::class, name="ArtistId", readonly=true) */
33
    public $artist;
34
35
    public function __construct(ArtistEntity $artist, string $title)
36
    {
37
        $this->title = $title;
38
        $this->artist = $artist;
39
    }
40
}
41
42
/** @Entity(source="artists") */
43
class ArtistEntity implements Storable
44
{
45
    use AutoGenerateId;
46
47
    /** @Property\Id(class=GenericId::class, name="ArtistId") */
48
    public $id;
49
50
    /** @Property\Text() */
51
    public $name;
52
53
    public function __construct(string $name)
54
    {
55
        $this->name = $name;
56
    }
57
}
58
59
// Define repositories:
60
61
class TrackRepository extends Repository
62
{
63
    public function findByArtist(ArtistEntity $artist): LazyCollection
64
    {
65
        $cursor = $this->query("SELECT tracks.* FROM tracks JOIN albums ON albums.AlbumId = tracks.AlbumId JOIN artists ON artists.ArtistId = albums.ArtistId  WHERE albums.ArtistId = :id", [
66
            'id' => $artist->getId()
67
        ]);
68
69
        return new LazyCollection($cursor);
70
    }
71
72
    public function getEntityClass(): string
73
    {
74
        return TrackEntity::class;
75
    }
76
}
77
78
// Work with UnitOfWork:
79
$storage = new Storage();
80
$storage->addRepository(
81
    // Dynamic Repository
82
    new class($sqliteConnection, $storage->getEntityManager()) extends Repository {
83
        public function getEntityClass(): string
84
        {
85
            return ArtistEntity::class;
86
        }
87
    },
88
89
    // Custom Repository class
90
    new TrackRepository($sqliteConnection, $storage->getEntityManager())
91
);
92
93
$artist = $storage->get(ArtistEntity::class, 1);
94
$track = $storage->get(TrackEntity::class, 1);
95
96
// Find Artist's tracks
97
foreach ($storage->getRepository(TrackEntity::class)->findByArtist($artist) as $track) {
0 ignored issues
show
Bug introduced by
The method findByArtist() does not exist on Igni\Storage\Repository. It seems like you code against a sub-type of Igni\Storage\Repository such as IgniTest\Fixtures\Album\AlbumRepository or TrackRepository. ( Ignorable by Annotation )

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

97
foreach ($storage->getRepository(TrackEntity::class)->/** @scrutinizer ignore-call */ findByArtist($artist) as $track) {
Loading history...
98
    echo $track->title;
99
}
100
101
102
// Override artist
103
$track->artist = $artist;
104
105
// Override artist name
106
$artist->name = 'John Lennon';
0 ignored issues
show
Bug introduced by
Accessing name on the interface Igni\Storage\Storable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
107
108
// Persist changes
109
$storage->persist($track, $artist);
110
$storage->commit();
111