Passed
Push — master ( fbf25e...f37e15 )
by Christoph
18:24 queued 11s
created

JrdResponse::setExpires()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
nc 1
nop 1
dl 0
loc 4
c 1
b 0
f 0
cc 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * @copyright 2020 Christoph Wurst <[email protected]>
7
 *
8
 * @author 2020 Christoph Wurst <[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 OCP\Http\WellKnown;
27
28
use OCP\AppFramework\Http\JSONResponse;
29
use OCP\AppFramework\Http\Response;
30
use function array_filter;
31
32
/**
33
 * A JSON Document Format (JDF) response to a well-known request
34
 *
35
 * @ref https://tools.ietf.org/html/rfc6415#appendix-A
36
 * @ref https://tools.ietf.org/html/rfc7033#section-4.4
37
 *
38
 * @since 21.0.0
39
 */
40
final class JrdResponse implements IResponse {
41
42
	/** @var string */
43
	private $subject;
44
45
	/** @var string|null */
46
	private $expires;
47
48
	/** @var string[] */
49
	private $aliases = [];
50
51
	/** @var (string|null)[] */
52
	private $properties = [];
53
54
	/** @var mixed[] */
55
	private $links;
56
57
	/**
58
	 * @param string $subject https://tools.ietf.org/html/rfc7033#section-4.4.1
59
	 *
60
	 * @since 21.0.0
61
	 */
62
	public function __construct(string $subject) {
63
		$this->subject = $subject;
64
	}
65
66
	/**
67
	 * @param string $expires
68
	 *
69
	 * @return $this
70
	 *
71
	 * @since 21.0.0
72
	 */
73
	public function setExpires(string $expires): self {
74
		$this->expires = $expires;
75
76
		return $this;
77
	}
78
79
	/**
80
	 * Add an alias
81
	 *
82
	 * @ref https://tools.ietf.org/html/rfc7033#section-4.4.2
83
	 *
84
	 * @param string $alias
85
	 *
86
	 * @return $this
87
	 *
88
	 * @since 21.0.0
89
	 */
90
	public function addAlias(string $alias): self {
91
		$this->aliases[] = $alias;
92
93
		return $this;
94
	}
95
96
	/**
97
	 * Add a property
98
	 *
99
	 * @ref https://tools.ietf.org/html/rfc7033#section-4.4.3
100
	 *
101
	 * @param string $property
102
	 * @param string|null $value
103
	 *
104
	 * @return $this
105
	 *
106
	 * @since 21.0.0
107
	 */
108
	public function addProperty(string $property, ?string $value): self {
109
		$this->properties[$property] = $value;
110
111
		return $this;
112
	}
113
114
	/**
115
	 * Add a link
116
	 *
117
	 * @ref https://tools.ietf.org/html/rfc7033#section-8.4
118
	 *
119
	 * @param string $rel https://tools.ietf.org/html/rfc7033#section-4.4.4.1
120
	 * @param string|null $type https://tools.ietf.org/html/rfc7033#section-4.4.4.2
121
	 * @param string|null $href https://tools.ietf.org/html/rfc7033#section-4.4.4.3
122
	 * @param string[]|null $titles https://tools.ietf.org/html/rfc7033#section-4.4.4.4
123
	 * @param string|null $properties https://tools.ietf.org/html/rfc7033#section-4.4.4.5
124
	 *
125
	 * @psalm-param array<string,(string|null)>|null $properties https://tools.ietf.org/html/rfc7033#section-4.4.4.5
126
	 *
127
	 * @return JrdResponse
128
	 * @since 21.0.0
129
	 */
130
	public function addLink(string $rel,
131
							?string $type,
132
							?string $href,
133
							?array $titles = [],
134
							?array $properties = []): self {
135
		$this->links[] = array_filter([
136
			'rel' => $rel,
137
			'type' => $type,
138
			'href' => $href,
139
			'titles' => $titles,
140
			'properties' => $properties,
141
		]);
142
143
		return $this;
144
	}
145
146
	/**
147
	 * @since 21.0.0
148
	 */
149
	public function toHttpResponse(): Response {
150
		return new JSONResponse(array_filter([
151
			'subject' => $this->subject,
152
			'expires' => $this->expires,
153
			'aliases' => $this->aliases,
154
			'properties' => $this->properties,
155
			'links' => $this->links,
156
		]));
157
	}
158
159
	/**
160
	 * Does this response have any data attached to it?
161
	 *
162
	 * @since 21.0.0
163
	 */
164
	public function isEmpty(): bool {
165
		return $this->expires === null
166
			&& empty($this->aliases)
167
			&& empty($this->properties)
168
			&& empty($this->links);
169
	}
170
}
171