Book::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LibraryCatalog\Entity;
6
7
class Book
8
{
9
    /** @var mixed */
10
    public $id;
11
    /** @var string */
12
    public string $title;
13
    /** @var string */
14
    public ?string $summary;
15
    /** @var mixed */
16
    public $authorId;
17
    /** @var Author|null */
18
    public ?Author $author;
19
20
    /** @var bool */
21
    protected bool $isAuthorLoaded = false;
22
23
    /**
24
     * @param mixed $authorId
25
     * @param string $title
26
     * @param string|null $summary
27
     * @return Book
28
     */
29 6
    public static function create($authorId, string $title, string $summary = null): Book
30
    {
31 6
        $book = new static();
32
33 6
        $book->authorId = $authorId;
34 6
        $book->title = $title;
35 6
        $book->summary = $summary;
36
37 6
        return $book;
38
    }
39
40
    /**
41
     * @param array $data
42
     * @return Book
43
     */
44 1
    public function fill(array $data): Book
45
    {
46 1
        if (isset($data['title'])) {
47 1
            $this->title = (string)$data['title'];
48
        }
49 1
        if (isset($data['summary'])) {
50 1
            $this->summary = (string)$data['summary'];
51
        }
52 1
        if (isset($data['authorId'])) {
53 1
            $this->authorId = $data['authorId'];
54
        }
55 1
        return $this;
56
    }
57
58
    /**
59
     * @return bool
60
     */
61 4
    public function isAuthorLoaded(): bool
62
    {
63 4
        return $this->isAuthorLoaded;
64
    }
65
66
    /**
67
     * @param Author $author
68
     * @return Book
69
     */
70 2
    public function setAuthor(Author $author): Book
71
    {
72 2
        $this->author = $author;
73 2
        $this->isAuthorLoaded = true;
74 2
        return $this;
75
    }
76
}
77