Passed
Push — master ( 1f0b7e...5bb3b6 )
by Allan
02:45
created

Sanitizer::sanitize()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 15
nc 5
nop 0
dl 0
loc 30
rs 9.4555
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerPatches\Repository\Lock;
7
8
use Vaimo\ComposerPatches\Composer\ConfigKeys as Config;
9
use Vaimo\ComposerPatches\Config as PluginConfig;
10
use Vaimo\ComposerPatches\Composer\Constraint;
11
12
class Sanitizer
13
{
14
    /**
15
     * @var \Vaimo\ComposerPatches\Managers\LockerManager
16
     */
17
    private $lockerManager;
18
19
    /**
20
     * @var \Vaimo\ComposerPatches\Utils\DataUtils
21
     */
22
    private $dataUtils;
23
24
    /**
25
     * @param \Composer\IO\IOInterface $cliIO
26
     */
27
    public function __construct(
28
        \Composer\IO\IOInterface $cliIO
29
    ) {
30
        $this->lockerManager = new \Vaimo\ComposerPatches\Managers\LockerManager($cliIO);
31
32
        $this->dataUtils = new \Vaimo\ComposerPatches\Utils\DataUtils();
33
    }
34
    
35
    public function sanitize()
36
    {
37
        if (!$lockData = $this->lockerManager->readLockData()) {
38
            return;
39
        }
40
41
        $queriedPaths = array(
42
            implode('/', array(Config::PACKAGES, Constraint::ANY)),
43
            implode('/', array(Config::PACKAGES_DEV, Constraint::ANY))
44
        );
45
46
        $nodes = $this->dataUtils->getNodeReferencesByPaths($lockData, $queriedPaths);
47
48
        foreach ($nodes as &$node) {
49
            if (!isset($node[Config::CONFIG_ROOT][PluginConfig::APPLIED_FLAG])) {
50
                continue;
51
            }
52
53
            unset($node[Config::CONFIG_ROOT][PluginConfig::APPLIED_FLAG]);
54
55
            if ($node[Config::CONFIG_ROOT]) {
56
                continue;
57
            }
58
59
            unset($node[Config::CONFIG_ROOT]);
60
        }
61
62
        unset($node);
63
64
        $this->lockerManager->writeLockData($lockData);
65
    }
66
}
67