Book::delete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Domain\Book;
6
7
use App\Domain\Book\Event\BookDescriptionWasChanged;
8
use App\Domain\Book\Event\BookNameWasChanged;
9
use App\Domain\Book\Event\BookWasCreated;
10
use App\Domain\Book\Event\BookWasDeleted;
11
use App\Domain\Book\ValueObject\Description;
12
use App\Domain\Common\ValueObject\AggregateRootId;
13
use App\Domain\Common\ValueObject\Name;
14
use Prooph\EventSourcing\AggregateChanged;
0 ignored issues
show
Bug introduced by
The type Prooph\EventSourcing\AggregateChanged was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Prooph\EventSourcing\AggregateRoot;
0 ignored issues
show
Bug introduced by
The type Prooph\EventSourcing\AggregateRoot was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
17
class Book extends AggregateRoot
18
{
19
    /**
20
     * @var AggregateRootId
21
     */
22
    protected $id;
23
24
    /**
25
     * @var Name
26
     */
27
    protected $name;
28
29
    /**
30
     * @var Description
31
     */
32
    protected $description;
33
34
    /**
35
     * @var string
36
     */
37
    protected $category;
38
39
    /**
40
     * @var string
41
     */
42
    protected $author;
43
44
    public static function create(
45
        AggregateRootId $id,
46
        Name $name,
47
        ValueObject\Description $description,
48
        string $category,
49
        string $author
50
    ): self {
51
        $self = new self();
52
        $self->recordThat(BookWasCreated::createWithData($id, $name, $description, $category, $author));
53
54
        return $self;
55
    }
56
57
    public function applyBookWasCreated(BookWasCreated $bookWasCreated)
58
    {
59
        $this->id = $bookWasCreated->getId();
60
        $this->name = $bookWasCreated->getName();
61
        $this->description = $bookWasCreated->getDescription();
62
        $this->author = $bookWasCreated->getAuthor();
63
        $this->category = $bookWasCreated->getCategory();
64
    }
65
66
    public function changeName(string $name)
67
    {
68
        $this->name->changeName($name);
69
        $this->recordThat(BookNameWasChanged::createWithData($this->id, $this->name));
70
    }
71
72
    public function changeDescription(string $description)
73
    {
74
        $this->description->changeDescription($description);
75
        $this->recordThat(BookDescriptionWasChanged::createWithData($this->id, $this->description));
76
    }
77
78
    public function delete()
79
    {
80
        $this->recordThat(BookWasDeleted::createWithData($this->id));
81
    }
82
83
    protected function applyBookWasDeleted(BookWasDeleted $bookWasDeleted)
84
    {
85
    }
86
87
    protected function applyBookDescriptionWasChanged(BookDescriptionWasChanged $bookDescriptionWasChanged)
88
    {
89
        $this->description = $bookDescriptionWasChanged->getDescription();
90
    }
91
92
    protected function applyBookNameWasChanged(BookNameWasChanged $bookNameWasChanged)
93
    {
94
        $this->name = $bookNameWasChanged->getName();
95
    }
96
97
    protected function aggregateId(): string
98
    {
99
        return $this->id->toString();
100
    }
101
102
    /**
103
     * Apply given event.
104
     */
105
    protected function apply(AggregateChanged $e): void
106
    {
107
        $handler = $this->determineEventHandlerMethodFor($e);
108
        if (!\method_exists($this, $handler)) {
109
            throw new \RuntimeException(\sprintf(
110
                'Missing event handler method %s for aggregate root %s',
111
                $handler,
112
                \get_class($this)
113
            ));
114
        }
115
        $this->{$handler}($e);
116
    }
117
118
    protected function determineEventHandlerMethodFor(AggregateChanged $e): string
119
    {
120
        return 'apply'.\implode(\array_slice(\explode('\\', \get_class($e)), -1));
121
    }
122
}
123