Completed
Push — master ( 8b9cee...95d6ab )
by Maxence
02:21
created

Provider::generateOpenGraphParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 2
1
<?php
2
3
4
namespace OCA\Mood\Activity;
5
6
use OCA\Circles\Model\Share;
7
use OCA\Mood\Service\MiscService;
8
use OCP\Activity\IEvent;
9
use OCP\Activity\IManager;
10
use OCP\Activity\IProvider;
11
use OCP\Comments\NotFoundException;
12
use OCP\IL10N;
13
use OCP\IURLGenerator;
14
15
class Provider implements IProvider {
16
17
	/** @var MiscService */
18
	protected $miscService;
19
20
	/** @var IL10N */
21
	protected $l10n;
22
23
	/** @var IURLGenerator */
24
	protected $url;
25
26
	/** @var IManager */
27
	protected $activityManager;
28
29
	public function __construct(
30
		IURLGenerator $url, IManager $activityManager, IL10N $l10n, MiscService $miscService
31
	) {
32
		$this->url = $url;
33
		$this->activityManager = $activityManager;
34
		$this->l10n = $l10n;
35
		$this->miscService = $miscService;
36
	}
37
38
	/**
39
	 * @param string $lang
40
	 * @param IEvent $event
41
	 * @param IEvent|null $previousEvent
42
	 *
43
	 * @return IEvent
44
	 * @since 11.0.0
45
	 */
46
	public function parse($lang, IEvent $event, IEvent $previousEvent = null) {
47
48
//		$this->miscService->log(">>> " . var_export($event, true));
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
49
		if ($event->getApp() !== 'mood') {
50
			throw new \InvalidArgumentException();
51
		}
52
53
		switch ($event->getSubject()) {
54
			case 'mood_item':
55
				$params = $event->getSubjectParameters();
56
				if (!key_exists('share', $params)) {
57
					throw new \InvalidArgumentException();
58
				}
59
60
				$event->setIcon(
61
					$this->url->getAbsoluteURL($this->url->imagePath('mood', 'mood.svg'))
62
				);
63
64
				$share = Share::fromJSON($params['share']);
65
				$mood = $share->getItem();
66
67
				$this->parseActivityHeader($event, $share);
68
				$this->parseMood($event, $mood);
69
70
				break;
71
72
			default:
73
				throw new \InvalidArgumentException();
74
		}
75
76
		return $event;
77
78
	}
79
80
81
	private function parseMood(IEvent &$event, $mood) {
82
83
		if (key_exists('website', $mood)) {
84
			$event->setRichMessage(
85
				$mood['text'] . '{opengraph}',
86
				['opengraph' => $this->generateOpenGraphParameter('_id_', $mood['website'])]
87
			);
88
		} else {
89
			$event->setParsedMessage($mood['text']);
90
		}
91
92
93
	}
94
95
96
	private function parseActivityHeader(IEvent &$event, Share $share) {
97
98
		$this->activityManager->getCurrentUserId();
99
100
		if ($share->getAuthor() === $this->activityManager->getCurrentUserId()) {
101
102
			$event->setParsedSubject(
103
				$this->l10n->t(
104
					'You shared a mood with %1$s', ['circle1, circle2']
105
				)
106
			)
107
				  ->setRichSubject(
108
					  $this->l10n->t(
109
						  'You shared a mood with {circles}'
110
					  ),
111
					  ['circles' => $this->generateCircleParameter($share)]
112
113
				  );
114
115
		} else {
116
117
			$author = $this->generateUserParameter($share->getAuthor());
118
			$event->setParsedSubject(
119
				$this->l10n->t(
120
					'%1$s shared a mood with %2$s', [
121
													  $author['name'],
122
													  'circle1, circle2'
123
												  ]
124
				)
125
			)
126
				  ->setRichSubject(
127
					  $this->l10n->t(
128
						  '{author} shared a mood with {circles}'
129
					  ), [
130
						  'author'  => $author,
131
						  'circles' => $this->generateCircleParameter($share)
132
					  ]
133
				  );
134
		}
135
	}
136
137
138
	private function generateOpenGraphParameter($id, $website) {
139
		return [
140
			'type'        => 'open-graph',
141
			'id'          => $id,
142
			'name'       => $website['title'],
143
			'description' => $website['description'],
144
			'website'     => 'youtube.com',
145
			'thumb' => \OC::$server->getURLGenerator()
146
								   ->linkToRoute('mood.Tools.binFromExternalImage') . '?url='
147
					   . rawurlencode($website['thumb']),
148
			'link' => 'https://www.google.com/'
149
		];
150
	}
151
152
153
	private function generateCircleParameter(Share $share) {
154
		return [
155
			'type' => 'circle',
156
			'id'   => $share->getCircleId(),
157
			'name' => $share->getCircleName(),
158
			'link' => 'http://nextcloud/index.php/apps/circles/#' . $share->getCircleId()
159
		];
160
	}
161
162
163
	private function generateUserParameter($uid) {
164
		return [
165
			'type' => 'user',
166
			'id'   => $uid,
167
			'name' => $uid,// FIXME Use display name
168
		];
169
	}
170
}
171