Passed
Push — master ( 4209dc...b3eb0b )
by Julius
16:39 queued 12s
created

Reference::getOpenGraphObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 7
rs 10
c 1
b 0
f 0
eloc 6
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2022 Julius Härtl <[email protected]>
7
 *
8
 * @author Julius Härtl <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24
 */
25
26
namespace OC\Collaboration\Reference;
27
28
use OCP\Collaboration\Reference\IReference;
29
30
class Reference implements IReference {
31
	private string $reference;
32
33
	private bool $accessible = true;
34
35
	private ?string $title = null;
36
	private ?string $description = null;
37
	private ?string $imageUrl = null;
38
	private ?string $contentType = null;
39
	private ?string $url = null;
40
41
	private ?string $richObjectType = null;
42
	private ?array $richObject = null;
43
44
	public function __construct(string $reference) {
45
		$this->reference = $reference;
46
	}
47
48
	public function getId(): string {
49
		return $this->reference;
50
	}
51
52
	public function setAccessible(bool $accessible): void {
53
		$this->accessible = $accessible;
54
	}
55
56
	public function getAccessible(): bool {
57
		return $this->accessible;
58
	}
59
60
	public function setTitle(string $title): void {
61
		$this->title = $title;
62
	}
63
64
	public function getTitle(): string {
65
		return $this->title ?? $this->reference;
66
	}
67
68
	public function setDescription(?string $description): void {
69
		$this->description = $description;
70
	}
71
72
	public function getDescription(): ?string {
73
		return $this->description;
74
	}
75
76
	public function setImageUrl(?string $imageUrl): void {
77
		$this->imageUrl = $imageUrl;
78
	}
79
80
	public function getImageUrl(): ?string {
81
		return $this->imageUrl;
82
	}
83
84
	public function setImageContentType(?string $contentType): void {
85
		$this->contentType = $contentType;
86
	}
87
88
	public function getImageContentType(): ?string {
89
		return $this->contentType;
90
	}
91
92
	public function setUrl(?string $url): void {
93
		$this->url = $url;
94
	}
95
96
	public function getUrl(): ?string {
97
		return $this->url;
98
	}
99
100
	public function setRichObject(string $type, ?array $richObject): void {
101
		$this->richObjectType = $type;
102
		$this->richObject = $richObject;
103
	}
104
105
	public function getRichObjectType(): string {
106
		if ($this->richObjectType === null) {
107
			return 'open-graph';
108
		}
109
		return $this->richObjectType;
110
	}
111
112
	public function getRichObject(): array {
113
		if ($this->richObject === null) {
114
			return $this->getOpenGraphObject();
115
		}
116
		return $this->richObject;
117
	}
118
119
	public function getOpenGraphObject(): array {
120
		return [
121
			'id' => $this->getId(),
122
			'name' => $this->getTitle(),
123
			'description' => $this->getDescription(),
124
			'thumb' => $this->getImageUrl(),
125
			'link' => $this->getUrl()
126
		];
127
	}
128
129
	public static function toCache(IReference $reference): array {
130
		return [
131
			'id' => $reference->getId(),
132
			'title' => $reference->getTitle(),
133
			'imageUrl' => $reference->getImageUrl(),
134
			'imageContentType' => $reference->getImageContentType(),
135
			'description' => $reference->getDescription(),
136
			'link' => $reference->getUrl(),
137
			'accessible' => $reference->getAccessible(),
138
			'richObjectType' => $reference->getRichObjectType(),
139
			'richObject' => $reference->getRichObject(),
140
		];
141
	}
142
143
	public static function fromCache(array $cache): IReference {
144
		$reference = new Reference($cache['id']);
145
		$reference->setTitle($cache['title']);
146
		$reference->setDescription($cache['description']);
147
		$reference->setImageUrl($cache['imageUrl']);
148
		$reference->setImageContentType($cache['imageContentType']);
149
		$reference->setUrl($cache['link']);
150
		$reference->setRichObject($cache['richObjectType'], $cache['richObject']);
151
		$reference->setAccessible($cache['accessible']);
152
		return $reference;
153
	}
154
155
	public function jsonSerialize() {
156
		return [
157
			'richObjectType' => $this->getRichObjectType(),
158
			'richObject' => $this->getRichObject(),
159
			'openGraphObject' => $this->getOpenGraphObject(),
160
			'accessible' => $this->accessible
161
		];
162
	}
163
}
164