Passed
Push — master ( c233ac...ceefe0 )
by Joas
14:23 queued 13s
created

LastSeen   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A configure() 0 8 1
A execute() 0 18 3
A completeArgumentValues() 0 5 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Christoph Wurst <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 * @author Pierre Ozoux <[email protected]>
9
 * @author Roeland Jago Douma <[email protected]>
10
 *
11
 * @license AGPL-3.0
12
 *
13
 * This code is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License, version 3,
15
 * as published by the Free Software Foundation.
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, version 3,
23
 * along with this program. If not, see <http://www.gnu.org/licenses/>
24
 *
25
 */
26
namespace OC\Core\Command\User;
27
28
use OC\Core\Command\Base;
29
use OCP\IUser;
30
use OCP\IUserManager;
31
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
32
use Symfony\Component\Console\Input\InputArgument;
33
use Symfony\Component\Console\Input\InputInterface;
34
use Symfony\Component\Console\Output\OutputInterface;
35
36
class LastSeen extends Base {
37
	/** @var IUserManager */
38
	protected $userManager;
39
40
	/**
41
	 * @param IUserManager $userManager
42
	 */
43
	public function __construct(IUserManager $userManager) {
44
		$this->userManager = $userManager;
45
		parent::__construct();
46
	}
47
48
	protected function configure() {
49
		$this
50
			->setName('user:lastseen')
51
			->setDescription('shows when the user was logged in last time')
52
			->addArgument(
53
				'uid',
54
				InputArgument::REQUIRED,
55
				'the username'
56
			);
57
	}
58
59
	protected function execute(InputInterface $input, OutputInterface $output): int {
60
		$user = $this->userManager->get($input->getArgument('uid'));
61
		if (is_null($user)) {
62
			$output->writeln('<error>User does not exist</error>');
63
			return 1;
64
		}
65
66
		$lastLogin = $user->getLastLogin();
67
		if ($lastLogin === 0) {
68
			$output->writeln('User ' . $user->getUID() .
69
				' has never logged in, yet.');
70
		} else {
71
			$date = new \DateTime();
72
			$date->setTimestamp($lastLogin);
73
			$output->writeln($user->getUID() .
74
				'`s last login: ' . $date->format('d.m.Y H:i'));
75
		}
76
		return 0;
77
	}
78
79
	/**
80
	 * @param string $argumentName
81
	 * @param CompletionContext $context
82
	 * @return string[]
83
	 */
84
	public function completeArgumentValues($argumentName, CompletionContext $context) {
85
		if ($argumentName === 'uid') {
86
			return array_map(static fn (IUser $user) => $user->getUID(), $this->userManager->search($context->getCurrentWord()));
87
		}
88
		return [];
89
	}
90
}
91