Passed
Push — master ( 816200...411754 )
by Joas
21:43 queued 12s
created

LocalTimeProvider::process()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 16
rs 9.8666
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2023, Joas Schilling <[email protected]>
7
 *
8
 * @author Joas Schilling <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OC\Contacts\ContactsMenu\Providers;
28
29
use OCP\AppFramework\Utility\ITimeFactory;
30
use OCP\Contacts\ContactsMenu\IActionFactory;
31
use OCP\Contacts\ContactsMenu\IEntry;
32
use OCP\Contacts\ContactsMenu\IProvider;
33
use OCP\IConfig;
34
use OCP\IDateTimeFormatter;
35
use OCP\IURLGenerator;
36
use OCP\IUserManager;
37
use OCP\L10N\IFactory as IL10NFactory;
38
39
class LocalTimeProvider implements IProvider {
40
	private IActionFactory $actionFactory;
41
	private IL10NFactory $l10nFactory;
42
	private IURLGenerator $urlGenerator;
43
	private IUserManager $userManager;
44
	private ITimeFactory $timeFactory;
45
	private IDateTimeFormatter $dateTimeFormatter;
46
	private IConfig $config;
47
48
	public function __construct(
49
		IActionFactory $actionFactory,
50
		IL10NFactory $l10nFactory,
51
		IURLGenerator $urlGenerator,
52
		IUserManager $userManager,
53
		ITimeFactory $timeFactory,
54
		IDateTimeFormatter $dateTimeFormatter,
55
		IConfig $config
56
	) {
57
		$this->actionFactory = $actionFactory;
58
		$this->l10nFactory = $l10nFactory;
59
		$this->urlGenerator = $urlGenerator;
60
		$this->userManager = $userManager;
61
		$this->timeFactory = $timeFactory;
62
		$this->dateTimeFormatter = $dateTimeFormatter;
63
		$this->config = $config;
64
	}
65
66
	/**
67
	 * @param IEntry $entry
68
	 */
69
	public function process(IEntry $entry) {
70
		$targetUserId = $entry->getProperty('UID');
71
		$targetUser = $this->userManager->get($targetUserId);
72
		if (!empty($targetUser)) {
73
			$timezone = $this->config->getUserValue($targetUser->getUID(), 'core', 'timezone') ?: date_default_timezone_get();
74
			$dateTimeZone = new \DateTimeZone($timezone);
75
			$localTime = $this->dateTimeFormatter->formatTime($this->timeFactory->getDateTime(), 'short', $dateTimeZone);
76
77
			$iconUrl = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/recent.svg'));
78
			$l = $this->l10nFactory->get('lib');
79
			$profileActionText = $l->t('Local time: %s', [$localTime]);
80
81
			$action = $this->actionFactory->newLinkAction($iconUrl, $profileActionText, '#', 'timezone');
82
			// Order after the profile page
83
			$action->setPriority(19);
84
			$entry->addAction($action);
85
		}
86
	}
87
}
88