UserCollector   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 38
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A collect() 0 32 5
1
<?php
2
3
namespace Startwind\Inventorio\Collector\System;
4
5
use Startwind\Inventorio\Collector\BasicCollector;
6
use Startwind\Inventorio\Exec\File;
7
use Startwind\Inventorio\Exec\Runner;
8
9
class UserCollector extends BasicCollector
10
{
11
    private const PASSWORD_FILE = '/etc/passwd';
12
13
    protected string $identifier = 'SystemUser';
14
15
    public function collect(): array
16
    {
17
        $passwdFile = self::PASSWORD_FILE;
18
19
        $runner = Runner::getInstance();
20
21
        if (!$runner->fileExists($passwdFile) || !File::getInstance()->isReadable($passwdFile)) {
22
            return [];
23
        }
24
25
        $users = [];
26
27
        foreach (File::getInstance()->getContents($passwdFile, true) as $line) {
28
            $parts = explode(':', trim($line));
29
30
            if (count($parts) < 7) {
31
                continue;
32
            }
33
34
            list($username, $password, $uid, $gid, $comment, $home, $shell) = $parts;
35
36
            $users[] = [
37
                'username' => $username,
38
                'uid' => (int)$uid,
39
                'gid' => (int)$gid,
40
                'comment' => $comment,
41
                'home' => $home,
42
                'shell' => $shell,
43
            ];
44
        }
45
46
        return ['users' => $users];
47
    }
48
49
}
50