anonymous//examples/basic_usage_example.php$0   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 4
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A basic_usage_example.php$0 ➔ getEntityClass() 0 3 1
1
<?php declare(strict_types=1);
2
/**
3
 * Simple tutorial step by step introducing user to world of storage framework.
4
 * Each section contains comment explaining what is happening behind the scenes.
5
 */
6
require_once __DIR__ . '/../vendor/autoload.php';
7
8
use Igni\Storage\Mapping\Annotation\Entity;
9
use Igni\Storage\Mapping\Annotation\Property;
10
use Igni\Storage\Id\AutoGenerateId;
11
use Igni\Storage\Driver\Pdo\Repository;
12
use Igni\Storage\Driver\Pdo\Connection;
13
use Igni\Storage\Storage;
14
use Igni\Storage\Mapping\Collection\Collection;
15
use Igni\Storage\Id\GenericId;
16
use Igni\Storage\Storable;
17
use Igni\Storage\Driver\ConnectionManager;
18
19
// Below there are entities used in the example:
20
21
/**
22
 * Records are taken from `tracks` table and hydrated to `Track` instance
23
 * @Entity(source="tracks")
24
 */
25
class Track 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(Artist $artist, string $title)
36
    {
37
        $this->title = $title;
38
        $this->artist = $artist;
39
    }
40
}
41
42
/**
43
 * Records are taken from `artists` table and hydrated to `Artist` instance
44
 * @Entity(source="artists")
45
 */
46
class Artist implements Storable
47
{
48
    use AutoGenerateId;
49
50
    /** @Property\Id(class=GenericId::class, name="ArtistId") */
51
    public $id;
52
53
    /** @Property\Text() */
54
    public $name;
55
56
    public function __construct(string $name)
57
    {
58
        $this->name = $name;
59
    }
60
}
61
62
// The following lines contain repositories
63
64
/**
65
 * Repositories can be used to define complex queries and aggregations by using native SQL queries.
66
 */
67
class TrackRepository extends Repository
68
{
69
    /**
70
     * Finds all tracks that belongs to given artist and return results as collection.
71
     */
72
    public function findByArtist(Artist $artist): Collection
73
    {
74
        $cursor = $this->query("
75
            SELECT tracks.* 
76
            FROM tracks 
77
            JOIN albums ON albums.AlbumId = tracks.AlbumId 
78
            JOIN artists ON artists.ArtistId = albums.ArtistId  
79
            WHERE albums.ArtistId = :id
80
        ", [
81
            'id' => $artist->getId()
82
        ]);
83
84
        return new Collection($cursor);
85
    }
86
87
    public static function getEntityClass(): string
88
    {
89
        return Track::class;
90
    }
91
}
92
// Work with unit of work
93
// Define connections:
94
ConnectionManager::registerDefault(new Connection('sqlite:/' . __DIR__ . '/db.db'));
95
96
$storage = new Storage();
97
98
// Attach repositories
99
$storage->addRepository(
100
    // Dynamic Repository
101
    new class($storage->getEntityManager()) extends Repository {
102
        public static function getEntityClass(): string
103
        {
104
            return Artist::class;
105
        }
106
    },
107
108
    // Custom Repository class
109
    new TrackRepository($storage->getEntityManager())
110
111
);
112
113
// Fetch items from database
114
115
$artist = $storage->get(Artist::class, 1); // This is equivalent to: SELECT *FROM artists WHERE ArtistId = 1
116
$track = $storage->get(Track::class, 1); // This is equivalent to: SELECT *FROM tracks WHERE TrackId = 1
117
118
// Iterate through all tracks that belong to given artist
119
foreach ($storage->getRepository(Track::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 Igni\Tests\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

119
foreach ($storage->getRepository(Track::class)->/** @scrutinizer ignore-call */ findByArtist($artist) as $track) {
Loading history...
120
    echo $track->title;
121
}
122
123
// Create new artist.
124
$jimmy = new Artist('Moaning Jimmy');
125
$storage->persist($jimmy);
126
127
// Update track's artist.
128
$track->artist = $jimmy;
129
130
$storage->remove($artist); // This will remove existing artist with id 1 once commit is executed.
131
132
$storage->persist($track); // Save changes that will be flushed to database once commit is executed.
133
134
$storage->commit(); // All update queries will happen from this point on
135