Completed
Push — master ( 408932...ce7eb5 )
by Dawid
06:58
created

TrackEntity   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 14
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 14
rs 10
c 1
b 0
f 0
wmc 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\Driver\Pdo\ConnectionOptions;
14
use Igni\Storage\Storage;
15
use Igni\Storage\Mapping\Collection\Collection;
16
use Igni\Storage\Id\GenericId;
17
use Igni\Storage\Storable;
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
$sqliteConnection = new Connection(__DIR__ . '/db.db');
95
$storage = new Storage();
96
97
// Attach repositories
98
$storage->addRepository(
99
// Dynamic Repository
100
    new class($sqliteConnection, $storage->getEntityManager()) extends Repository {
0 ignored issues
show
Bug introduced by
$sqliteConnection of type Igni\Storage\Driver\Pdo\Connection is incompatible with the type Igni\Storage\EntityManager expected by parameter $entityManager of anonymous//examples/basi...le.php$0::__construct(). ( Ignorable by Annotation )

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

100
    new class(/** @scrutinizer ignore-type */ $sqliteConnection, $storage->getEntityManager()) extends Repository {
Loading history...
Unused Code introduced by
The call to anonymous//examples/basi...le.php$0::__construct() has too many arguments starting with $storage->getEntityManager(). ( Ignorable by Annotation )

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

100
    /** @scrutinizer ignore-call */ 
101
    new class($sqliteConnection, $storage->getEntityManager()) extends Repository {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
101
        public static function getEntityClass(): string
102
        {
103
            return Artist::class;
104
        }
105
    },
106
107
    // Custom Repository class
108
    new TrackRepository($sqliteConnection, $storage->getEntityManager())
0 ignored issues
show
Unused Code introduced by
The call to TrackRepository::__construct() has too many arguments starting with $storage->getEntityManager(). ( Ignorable by Annotation )

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

108
    /** @scrutinizer ignore-call */ 
109
    new TrackRepository($sqliteConnection, $storage->getEntityManager())

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
$sqliteConnection of type Igni\Storage\Driver\Pdo\Connection is incompatible with the type Igni\Storage\EntityManager expected by parameter $entityManager of TrackRepository::__construct(). ( Ignorable by Annotation )

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

108
    new TrackRepository(/** @scrutinizer ignore-type */ $sqliteConnection, $storage->getEntityManager())
Loading history...
109
110
);
111
112
// Fetch items from database
113
114
$artist = $storage->get(Artist::class, 1); // This is equivalent to: SELECT *FROM artists WHERE ArtistId = 1
115
$track = $storage->get(Track::class, 1); // This is equivalent to: SELECT *FROM tracks WHERE TrackId = 1
116
117
// Iterate through all tracks that belong to given artist
118
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 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

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