Completed
Pull Request — master (#451)
by Joas
13:14
created

PlainTextParser::parseFederatedCloudIDParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 5
ccs 2
cts 3
cp 0.6667
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1.037
1
<?php
2
/**
3
 * ownCloud
4
 *
5
 * @author Joas Schilling
6
 * @copyright 2015 Joas Schilling [email protected]
7
 *
8
 * This file is licensed under the Affero General Public License version 3 or
9
 * later.
10
 * See the COPYING-README file.
11
 */
12
13
namespace OCA\Activity;
14
15
16
use OCP\IL10N;
17
18
class PlainTextParser {
19
20
	/** @var IL10N */
21
	protected $l;
22
23
	/**
24
	 * @param IL10N $l
25
	 */
26 8
	public function __construct(IL10N $l) {
27 8
		$this->l = $l;
28 8
	}
29
30
	/**
31
	 * Parse the parameters in the subject and message
32
	 *
33
	 * @param string $message
34
	 * @return string
35
	 */
36 5
	public function parseMessage($message) {
37 5
		$message = $this->parseCollections($message);
38 5
		$message = $this->parseParameters($message);
39 5
		return $message;
40
	}
41
42
	/**
43
	 * Parse collections
44
	 *
45
	 * @param string $message
46
	 * @return string
47
	 */
48 5
	protected function parseCollections($message) {
49
		return preg_replace_callback('/<collection>(.*?)<\/collection>/', function($match) {
50
			$parameterList = explode('><', $match[1]);
51
			$parameterListLength = sizeof($parameterList);
52
53
			$parameters = [];
54
			for ($i = 0; $i < $parameterListLength; $i++) {
55
				$parameter = $parameterList[$i];
56
				if ($i > 0) {
57
					$parameter = '<' . $parameter;
58
				}
59
				if ($i + 1 < $parameterListLength) {
60
					$parameter = $parameter . '>';
61
				}
62
63
				$parameters[] = $this->parseParameters($parameter);
64
			}
65
			if ($parameterListLength === 1) {
66
				return array_pop($parameters);
67
			} else {
68
				$lastParameter = array_pop($parameters);
69
				return $this->l->t('%s and %s', [
70
					implode($this->l->t(', '), $parameters),
71
					$lastParameter,
72
				]);
73
			}
74 5
		}, $message);
75
	}
76
77
	/**
78
	 * Parse the parameters in the subject and message
79
	 *
80
	 * @param string $message
81
	 * @return string
82
	 */
83 5
	protected function parseParameters($message) {
84 5
		$message = $this->parseUntypedParameters($message);
85 5
		$message = $this->parseUserParameters($message);
86 5
		$message = $this->parseFederatedCloudIDParameters($message);
87 5
		$message = $this->parseFileParameters($message);
88 5
		return $message;
89
	}
90
91
	/**
92
	 * Display the parameter value
93
	 *
94
	 * @param string $message
95
	 * @return string
96
	 */
97 5
	protected function parseUntypedParameters($message) {
98
		return preg_replace_callback('/<parameter>(.*?)<\/parameter>/', function($match) {
99
			return $match[1];
100 5
		}, $message);
101
	}
102
103
	/**
104
	 * Display the users display name
105
	 *
106
	 * @param string $message
107
	 * @return string
108
	 */
109 5
	protected function parseUserParameters($message) {
110
		return preg_replace_callback('/<user\ display\-name=\"(.*?)\">(.*?)<\/user>/', function($match) {
111 3
			return $match[1];
112 5
		}, $message);
113
	}
114
115
	/**
116
	 * Display the full cloud id
117
	 *
118
	 * @param string $message
119
	 * @return string
120
	 */
121 5
	protected function parseFederatedCloudIDParameters($message) {
122
		return preg_replace_callback('/<federated-cloud-id\ display\-name=\"(.*?)\"\ user=\"(.*?)\"\ server=\"(.*?)\">(.*?)<\/federated-cloud-id>/', function($match) {
123
			return $match[1];
124 5
		}, $message);
125
	}
126
127
	/**
128
	 * Display the path for files
129
	 *
130
	 * @param string $message
131
	 * @return string
132
	 */
133
	protected function parseFileParameters($message) {
134 5
		return preg_replace_callback('/<file\ link=\"(.*?)\"\ id=\"(.*?)\">(.*?)<\/file>/', function($match) {
135 4
			return $match[3];
136 5
		}, $message);
137
	}
138
}
139