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

ReferenceApiController::extract()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
dl 0
loc 15
rs 9.9666
c 1
b 0
f 0
eloc 9
nc 4
nop 3
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * @copyright Copyright (c) 2022 Julius Härtl <[email protected]>
6
 *
7
 * @author Julius Härtl <[email protected]>
8
 *
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace OC\Core\Controller;
26
27
use OCP\AppFramework\Http\DataResponse;
28
use OCP\Collaboration\Reference\IReferenceManager;
29
use OCP\IRequest;
30
31
class ReferenceApiController extends \OCP\AppFramework\OCSController {
32
	private IReferenceManager $referenceManager;
33
34
	public function __construct(string $appName, IRequest $request, IReferenceManager $referenceManager) {
35
		parent::__construct($appName, $request);
36
		$this->referenceManager = $referenceManager;
37
	}
38
39
	/**
40
	 * @NoAdminRequired
41
	 */
42
	public function extract(string $text, bool $resolve = false, int $limit = 1): DataResponse {
43
		$references = $this->referenceManager->extractReferences($text);
44
45
		$result = [];
46
		$index = 0;
47
		foreach ($references as $reference) {
48
			if ($index++ >= $limit) {
49
				break;
50
			}
51
52
			$result[$reference] = $resolve ? $this->referenceManager->resolveReference($reference) : null;
53
		}
54
55
		return new DataResponse([
56
			'references' => $result
57
		]);
58
	}
59
60
61
	/**
62
	 * @NoAdminRequired
63
	 *
64
	 * @param string[] $references
65
	 */
66
	public function resolve(array $references, int $limit = 1): DataResponse {
67
		$result = [];
68
		$index = 0;
69
		foreach ($references as $reference) {
70
			if ($index++ >= $limit) {
71
				break;
72
			}
73
74
			$result[$reference] = $this->referenceManager->resolveReference($reference);
75
		}
76
77
		return new DataResponse([
78
			'references' => array_filter($result)
79
		]);
80
	}
81
}
82