Completed
Push — master ( 8255fa...61397e )
by Morris
16:01
created

Result::getRelevantMessagePart()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 5
nop 2
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018 Joas Schilling <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OCA\Comments\Search;
23
24
use OCP\Comments\IComment;
25
use OCP\Files\NotFoundException;
26
use OCP\Search\Result as BaseResult;
27
28
class Result extends BaseResult {
29
30
	public $type = 'comment';
31
	public $comment;
32
	public $authorId;
33
	public $authorName;
34
	public $path;
35
	public $fileName;
36
37
	/**
38
	 * @param string $search
39
	 * @param IComment $comment
40
	 * @param string $authorName
41
	 * @param string $path
42
	 * @throws NotFoundException
43
	 */
44
	public function __construct(string $search,
45
								IComment $comment,
46
								string $authorName,
47
								string $path) {
48
		parent::__construct(
49
			(int) $comment->getId(),
50
			$comment->getMessage()
51
		/* @todo , [link to file] */
52
		);
53
54
		$this->comment = $this->getRelevantMessagePart($comment->getMessage(), $search);
55
		$this->authorId = $comment->getActorId();
56
		$this->authorName = $authorName;
57
		$this->fileName = basename($path);
58
		$this->path = $this->getVisiblePath($path);
59
	}
60
61
	/**
62
	 * @param string $path
63
	 * @return string
64
	 * @throws NotFoundException
65
	 */
66
	protected function getVisiblePath(string $path): string {
67
		$segments = explode('/', trim($path, '/'), 3);
68
69
		if (!isset($segments[2])) {
70
			throw new NotFoundException('Path not inside visible section');
71
		}
72
73
		return $segments[2];
74
	}
75
76
	/**
77
	 * @param string $message
78
	 * @param string $search
79
	 * @return string
80
	 * @throws NotFoundException
81
	 */
82
	protected function getRelevantMessagePart(string $message, string $search): string {
83
		$start = stripos($message, $search);
84
		if ($start === false) {
85
			throw new NotFoundException('Comment section not found');
86
		}
87
88
		$end = $start + strlen($search);
89
90
		if ($start <= 25) {
91
			$start = 0;
92
			$prefix = '';
93
		} else {
94
			$start -= 25;
95
			$prefix = '…';
96
		}
97
98
		if ((strlen($message) - $end) <= 25) {
99
			$end = strlen($message);
100
			$suffix = '';
101
		} else {
102
			$end += 25;
103
			$suffix = '…';
104
		}
105
106
		return $prefix . substr($message, $start, $end - $start) . $suffix;
107
	}
108
109
}
110