Passed
Push — main ( a33de3...4370d9 )
by Artem
02:12
created

AbstractContent::setTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace CloudPlayDev\ConfluenceClient\Entity;
5
6
7
use CloudPlayDev\ConfluenceClient\Api\Content;
8
use CloudPlayDev\ConfluenceClient\Exception\HydrationException;
9
use Webmozart\Assert\Assert;
10
11
abstract class AbstractContent implements Hydratable
12
{
13
    private ?int $id = null;
14
    private ?string $title = null;
15
    private ?string $space = null;
16
17
    private ?string $content = null;
18
19
    /**
20
     * @var int[]
21
     */
22
    private array $ancestors = [];
23
24
    private int $version = 1;
25
    /**
26
     * @var array<string, string> $children
27
     */
28
    private array $children = [];
29
    private ?string $url = null;
30
    protected string $type = Content::CONTENT_TYPE_GLOBAL;
31
32
    private ?int $containerId = null;
33
    private string $containerType = Content::CONTENT_TYPE_PAGE;
34
35
    /**
36
     * @return string
37
     */
38
    public function getType(): string
39
    {
40
        return $this->type;
41
    }
42
43
    /**
44
     * @param string $type
45
     * @return self
46
     */
47
    public function setType(string $type): self
48
    {
49
        $this->type = $type;
50
        return $this;
51
    }
52
53
    /**
54
     * @return int|null
55
     */
56
    public function getId(): ?int
57
    {
58
        return $this->id;
59
    }
60
61
    /**
62
     * @param int $id
63
     * @return self
64
     */
65
    public function setId(int $id): self
66
    {
67
        $this->id = $id;
68
        return $this;
69
    }
70
71
    public function getTitle(): ?string
72
    {
73
        return $this->title;
74
    }
75
76
    /**
77
     * @param string $title
78
     * @return self
79
     */
80
    public function setTitle(string $title): self
81
    {
82
        $this->title = $title;
83
        return $this;
84
    }
85
86
    /**
87
     * @return null|string
88
     */
89
    public function getSpace(): ?string
90
    {
91
        return $this->space;
92
    }
93
94
    /**
95
     * @param string|null $space
96
     * @return self
97
     */
98
    public function setSpace(?string $space): self
99
    {
100
        $this->space = $space;
101
        return $this;
102
    }
103
104
    /**
105
     * @return null|string
106
     */
107
    public function getContent(): ?string
108
    {
109
        return $this->content;
110
    }
111
112
    /**
113
     * @param string $content
114
     * @return self
115
     */
116
    public function setContent(string $content): self
117
    {
118
        $this->content = $content;
119
        return $this;
120
    }
121
122
    /**
123
     * @return int
124
     */
125
    public function getVersion(): int
126
    {
127
        return $this->version;
128
    }
129
130
    /**
131
     * @param int $version
132
     * @return self
133
     */
134
    public function setVersion(int $version): self
135
    {
136
        $this->version = $version;
137
        return $this;
138
    }
139
140
    /**
141
     * @return array<string, string>
142
     */
143
    public function getChildren(): array
144
    {
145
        return $this->children;
146
    }
147
148
    /**
149
     * @param array<string, string> $children
150
     * @return self
151
     */
152
    public function setChildren(array $children): self
153
    {
154
        $this->children = $children;
155
        return $this;
156
    }
157
158
    /**
159
     * @return null|string
160
     */
161
    public function getUrl(): ?string
162
    {
163
        return $this->url;
164
    }
165
166
    /**
167
     * @param string $url
168
     * @return self
169
     */
170
    public function setUrl(string $url): self
171
    {
172
        $this->url = $url;
173
        return $this;
174
    }
175
176
    /**
177
     * @param string $comment
178
     * @return ContentComment
179
     */
180
    public function createComment(string $comment): ContentComment
181
    {
182
        $contentComment = new ContentComment();
183
        $contentComment->setContainerId($this->getId());
184
        $contentComment->setContainerType($this->getType());
185
        $contentComment->setContent($comment);
186
        $contentComment->setSpace($this->getSpace());
187
        return $contentComment;
188
    }
189
190
    /**
191
     * @param string $title
192
     * @param string $body
193
     * @return ContentPage
194
     */
195
    public function createSubpage(string $title, string $body): ContentPage
196
    {
197
        $contentPage = new ContentPage();
198
        $contentPage->setContent($body);
199
        $contentPage->setTitle($title);
200
        $contentPage->setSpace($this->getSpace());
201
        if ($this->id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->id of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
202
            $contentPage->addAncestor($this->id);
203
        }
204
        return $contentPage;
205
    }
206
207
    /**
208
     * @return int|null
209
     */
210
    public function getContainerId(): ?int
211
    {
212
        return $this->containerId;
213
    }
214
215
    /**
216
     * @param int|null $containerId
217
     */
218
    public function setContainerId(?int $containerId): void
219
    {
220
        $this->containerId = $containerId;
221
    }
222
223
    /**
224
     * @return string
225
     */
226
    public function getContainerType(): string
227
    {
228
        return $this->containerType;
229
    }
230
231
    /**
232
     * @param string $containerType
233
     */
234
    public function setContainerType(string $containerType): void
235
    {
236
        $this->containerType = $containerType;
237
    }
238
239
    /**
240
     * @return int[]
241
     */
242
    public function getAncestors(): array
243
    {
244
        return $this->ancestors;
245
    }
246
247
    /**
248
     * @param int[] $ancestors
249
     * @return self
250
     */
251
    public function setAncestors(array $ancestors): self
252
    {
253
        $this->ancestors = $ancestors;
254
        return $this;
255
    }
256
257
    /**
258
     * @param int $id
259
     * @return self
260
     */
261
    public function addAncestor(int $id): self
262
    {
263
        $this->ancestors[] = $id;
264
        return $this;
265
    }
266
267
    /**
268
     * @param mixed[] $data
269
     * @return AbstractContent|ContentPage|ContentComment
270
     * @throws HydrationException
271
     */
272
    public static function load(array $data): self
273
    {
274
        Assert::true(isset($data['id'],
275
            $data['type'],
276
            $data['title'],
277
            $data['_links']['self']));
278
        Assert::string($data['type']);
279
280
        switch ($data['type']) {
281
            case Content::CONTENT_TYPE_PAGE:
282
                $content = new ContentPage();
283
                break;
284
            case Content::CONTENT_TYPE_COMMENT:
285
                $content = new ContentComment();
286
                break;
287
            default:
288
                throw new HydrationException('Invalid content type: ' . $data['type']);
289
        }
290
291
        $content->setId((int)$data['id']);
292
        $content->setTitle((string)$data['title']);
293
        $content->setUrl((string)$data['_links']['self']);
294
        if (isset($data['space']['key'])) {
295
            $content->setSpace((string)$data['space']['key']);
296
        }
297
        if (isset($data['version']['number'])) {
298
            $content->setVersion((int)$data['version']['number']);
299
        }
300
        if (isset($data['body']['storage']['value'])) {
301
            Assert::isArray($data['body']['storage']);
302
            $content->setContent((string)$data['body']['storage']['value']);
303
        }
304
305
        return $content;
306
    }
307
308
309
}
310