Completed
Branch dev (d5d70c)
by Raffael
11:00
created

LdapGroupsToLocalGroups   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 1
dl 0
loc 57
rs 10
c 0
b 0
f 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B start() 0 32 5
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2018 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Balloon\Migration\Delta;
13
14
use MongoDB\Database;
15
16
class LdapGroupsToLocalGroups implements DeltaInterface
17
{
18
    /**
19
     * Database.
20
     *
21
     * @var Database
22
     */
23
    protected $db;
24
25
    /**
26
     * Construct.
27
     *
28
     * @param Database $db
29
     */
30
    public function __construct(Database $db)
31
    {
32
        $this->db = $db;
33
    }
34
35
    /**
36
     * Start.
37
     *
38
     * @return bool
39
     */
40
    public function start(): bool
41
    {
42
        $cursor = $this->db->user->aggregate([
43
        ['$unwind' => '$groups'],
44
        ['$group' => [
45
                '_id' => '$groups',
46
                'member' => [
47
                    '$push' => '$_id',
48
                ],
49
            ]],
50
    ]);
51
52
        foreach ($cursor as $group) {
53
            $dn = explode(',', $group['_id']);
54
            $attrs = [];
55
            foreach ($dn as $part) {
56
                $parts = explode('=', $part);
57
                if ($parts[0] === 'cn') {
58
                    $attrs['name'] = $parts[1];
59
                } elseif ($parts[0] === 'o') {
60
                    $attrs['namespace'] = $parts[1];
61
                }
62
            }
63
64
            $attrs['ldapdn'] = $group['_id'];
65
            $attrs['member'] = $group['member'];
66
67
            $this->db->group->insertOne($attrs);
68
        }
69
70
        return true;
71
    }
72
}
73