Passed
Push — master ( bac7e1...6bb14b )
by Nils
02:24
created

UserCollector::collect()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 17
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 30
rs 9.3888
1
<?php
2
3
namespace Startwind\Inventorio\Collector\System;
4
5
use Startwind\Inventorio\Collector\BasicCollector;
6
7
class UserCollector extends BasicCollector
8
{
9
    private const PASSWORD_FILE = '/etc/passwd';
10
11
    protected string $identifier = 'SystemUser';
12
13
    public function collect(): array
14
    {
15
        $passwdFile = self::PASSWORD_FILE;
16
17
        if (!file_exists($passwdFile) || !is_readable($passwdFile)) {
18
            return [];
19
        }
20
21
        $users = [];
22
23
        foreach (file($passwdFile) as $line) {
24
            $parts = explode(':', trim($line));
25
26
            if (count($parts) < 7) {
27
                continue;
28
            }
29
30
            list($username, $password, $uid, $gid, $comment, $home, $shell) = $parts;
31
32
            $users[] = [
33
                'username' => $username,
34
                'uid' => (int)$uid,
35
                'gid' => (int)$gid,
36
                'comment' => $comment,
37
                'home' => $home,
38
                'shell' => $shell,
39
            ];
40
        }
41
42
        return ['users' => $users];
43
    }
44
45
}
46