Passed
Push — main ( be769d...9c5d5f )
by Diego
03:51
created

WikiPage   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 35
dl 0
loc 88
rs 10
c 2
b 0
f 0
wmc 14

11 Methods

Rating   Name   Duplication   Size   Complexity  
A isPersisted() 0 3 2
A addChild() 0 4 1
A __construct() 0 5 1
A addRevision() 0 4 2
A getMutations() 0 10 1
A getParent() 0 3 1
A removeChild() 0 4 2
A getId() 0 3 1
A getChildren() 0 3 1
A setParent() 0 3 1
A getRepositoryClass() 0 3 1
1
<?php
2
3
namespace Blackmine\Model\Project;
4
5
use Blackmine\Collection\HierarchyCollection;
6
use Blackmine\Collection\IdentityCollection;
7
use Blackmine\Model\FetchableInterface;
8
use Blackmine\Model\Identity;
9
use Blackmine\Model\Issue\Attachment;
10
use Blackmine\Model\ParentableInterface;
11
use Blackmine\Model\User\User;
12
use Blackmine\Mutator\MutableInterface;
13
use Blackmine\Mutator\Mutation\AddSubkeyMutation;
14
use Blackmine\Mutator\Mutation\RemoveKeyMutation;
15
use Blackmine\Mutator\Mutation\RenameKeyMutation;
16
use Blackmine\Repository\Projects\WikiPages;
17
use Carbon\CarbonImmutable;
18
use Doctrine\Common\Collections\Collection;
19
20
/**
21
 * @method void setTitle(string $title)
22
 * @method void setText(string $text)
23
 * @method void setComments(string $comments)
24
 * @method void setAuthor(User $author)
25
 * @method void setRevisions(IdentityCollection $revisions)
26
 *
27
 * @method string getTitle()
28
 * @method string getText()
29
 * @method int getVersion()
30
 * @method string getComments()
31
 * @method IdentityCollection getAttachments()
32
 * @method IdentityCollection getRevisions()
33
 * @method CarbonImmutable getCreatedOn()
34
 * @method CarbonImmutable getUpdatedOn()
35
 *
36
 * @method void addAttachment(Attachment $attachment)
37
 * @method void removeAttachment(Attachment $attachment)
38
 * @method void removeRevision(WikiPage $revision);
39
 */
40
class WikiPage extends Identity implements FetchableInterface, MutableInterface, ParentableInterface
41
{
42
    public const ENTITY_NAME = "wiki_page";
43
44
    protected string $title;
45
    protected string $text;
46
    protected int $version;
47
    protected string $comments;
48
49
    protected ?WikiPage $parent = null;
50
    protected User $author;
51
52
    protected ?IdentityCollection $attachments;
53
    protected ?IdentityCollection $revisions;
54
    protected ?HierarchyCollection $children;
55
56
    protected CarbonImmutable $created_on;
57
    protected CarbonImmutable $updated_on;
58
59
    public function __construct(protected ?int $id = null)
60
    {
61
        $this->attachments = new IdentityCollection();
62
        $this->revisions = new IdentityCollection();
63
        $this->children = new HierarchyCollection(parent_field: "title");
64
    }
65
66
    public function getId(): mixed
67
    {
68
        return $this->title;
69
    }
70
71
    public static function getRepositoryClass(): ?string
72
    {
73
        return WikiPages::class;
74
    }
75
76
    public function getMutations(): array
77
    {
78
        return [
79
            "parent_id" => [
80
                AddSubkeyMutation::class => ["title"],
81
                RenameKeyMutation::class => ["parent"],
82
            ],
83
            "attachments" => [RenameKeyMutation::class => ["uploads"]],
84
            "created_on" => [RemoveKeyMutation::class => []],
85
            "updated_on" => [RemoveKeyMutation::class  => []]
86
        ];
87
    }
88
89
    public function addRevision(WikiPage $revision): void
90
    {
91
        if (!$this->revisions->contains($revision)) {
0 ignored issues
show
Bug introduced by
The method contains() does not exist on null. ( Ignorable by Annotation )

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

91
        if (!$this->revisions->/** @scrutinizer ignore-call */ contains($revision)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
92
            $this->revisions->add($revision);
93
        }
94
    }
95
96
97
    public function getParent(): ?ParentableInterface
98
    {
99
        return $this->parent;
100
    }
101
102
    public function setParent(ParentableInterface | array $parent): void
103
    {
104
        $this->parent = $this->normalizeValue("parent", __CLASS__, $parent);
105
    }
106
107
    public function getChildren(): Collection
108
    {
109
        return $this->children;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->children could return the type null which is incompatible with the type-hinted return Doctrine\Common\Collections\Collection. Consider adding an additional type-check to rule them out.
Loading history...
110
    }
111
112
    public function addChild(ParentableInterface $child): void
113
    {
114
        $child->setParent($this);
115
        $this->children->addDirect($child);
0 ignored issues
show
Bug introduced by
The method addDirect() does not exist on null. ( Ignorable by Annotation )

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

115
        $this->children->/** @scrutinizer ignore-call */ 
116
                         addDirect($child);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
116
    }
117
118
    public function removeChild(ParentableInterface $child): void
119
    {
120
        if ($this->children->contains($child)) {
121
            $this->children->removeElement($child);
122
        }
123
    }
124
125
    public function isPersisted(): bool
126
    {
127
        return is_initialized($this, "version") && $this->version > 0;
128
    }
129
}
130