PlainTextParser   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 58.14%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 122
ccs 25
cts 43
cp 0.5814
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A parseMessage() 0 5 1
A parseParameters() 0 7 1
A parseUntypedParameters() 0 5 1
A parseUserParameters() 0 6 1
A parseFederatedCloudIDParameters() 0 5 1
A parseFileParameters() 0 5 1
A parseCollections() 0 28 5
1
<?php
2
/**
3
 * @author Joas Schilling <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2016, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
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, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\Activity;
23
24
use OCP\IL10N;
25
26
class PlainTextParser {
27
28
	/** @var IL10N */
29
	protected $l;
30
31
	/**
32
	 * @param IL10N $l
33
	 */
34 8
	public function __construct(IL10N $l) {
35 8
		$this->l = $l;
36 8
	}
37
38
	/**
39
	 * Parse the parameters in the subject and message
40
	 *
41
	 * @param string $message
42
	 * @return string
43
	 */
44 5
	public function parseMessage($message) {
45 5
		$message = $this->parseCollections($message);
46 5
		$message = $this->parseParameters($message);
47 5
		return $message;
48
	}
49
50
	/**
51
	 * Parse collections
52
	 *
53
	 * @param string $message
54
	 * @return string
55
	 */
56 5
	protected function parseCollections($message) {
57
		return \preg_replace_callback('/<collection>(.*?)<\/collection>/', function ($match) {
58
			$parameterList = \explode('><', $match[1]);
59
			$parameterListLength = \sizeof($parameterList);
60
61
			$parameters = [];
62
			for ($i = 0; $i < $parameterListLength; $i++) {
63
				$parameter = $parameterList[$i];
64
				if ($i > 0) {
65
					$parameter = '<' . $parameter;
66
				}
67
				if ($i + 1 < $parameterListLength) {
68
					$parameter = $parameter . '>';
69
				}
70
71
				$parameters[] = $this->parseParameters($parameter);
72
			}
73
			if ($parameterListLength === 1) {
74
				return \array_pop($parameters);
75
			} else {
76
				$lastParameter = \array_pop($parameters);
77
				return $this->l->t('%s and %s', [
78
					\implode($this->l->t(', '), $parameters),
79
					$lastParameter,
80
				]);
81
			}
82 5
		}, $message);
83
	}
84
85
	/**
86
	 * Parse the parameters in the subject and message
87
	 *
88
	 * @param string $message
89
	 * @return string
90
	 */
91 5
	protected function parseParameters($message) {
92 5
		$message = $this->parseUntypedParameters($message);
93 5
		$message = $this->parseUserParameters($message);
94 5
		$message = $this->parseFederatedCloudIDParameters($message);
95 5
		$message = $this->parseFileParameters($message);
96 5
		return $message;
97
	}
98
99
	/**
100
	 * Display the parameter value
101
	 *
102
	 * @param string $message
103
	 * @return string
104
	 */
105 5
	protected function parseUntypedParameters($message) {
106
		return \preg_replace_callback('/<parameter>(.*?)<\/parameter>/', function ($match) {
107
			return $match[1];
108 5
		}, $message);
109
	}
110
111
	/**
112
	 * Display the users display name
113
	 *
114
	 * @param string $message
115
	 * @return string
116
	 */
117 5
	protected function parseUserParameters($message) {
118
		return \preg_replace_callback('/<user\ display\-name=\"(.*?)\">(.*?)<\/user>/', function ($match) {
119
			// We don't want HTML to work, but quote signs are okay.
120 3
			return \str_replace('&quot;', '"', $match[1]);
121 5
		}, $message);
122
	}
123
124
	/**
125
	 * Display the full cloud id
126
	 *
127
	 * @param string $message
128
	 * @return string
129
	 */
130 5
	protected function parseFederatedCloudIDParameters($message) {
131
		return \preg_replace_callback('/<federated-cloud-id\ display\-name=\"(.*?)\"\ user=\"(.*?)\"\ server=\"(.*?)\">(.*?)<\/federated-cloud-id>/', function ($match) {
132
			return $match[1];
133 5
		}, $message);
134
	}
135
136
	/**
137
	 * Display the path for files
138
	 *
139
	 * @param string $message
140
	 * @return string
141
	 */
142
	protected function parseFileParameters($message) {
143 5
		return \preg_replace_callback('/<file\ link=\"(.*?)\"\ id=\"(.*?)\">(.*?)<\/file>/', function ($match) {
144 4
			return $match[3];
145 5
		}, $message);
146
	}
147
}
148