UnlinkCommand::execute()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 10
c 2
b 0
f 0
dl 0
loc 18
rs 9.9332
cc 3
nc 3
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the composer-link plugin.
7
 *
8
 * Copyright (c) 2021-2022 Sander Visser <[email protected]>.
9
 *
10
 * For the full copyright and license information, please view the LICENSE.md
11
 * file that was distributed with this source code.
12
 *
13
 * @link https://github.com/SanderSander/composer-link
14
 */
15
16
namespace ComposerLink\Commands;
17
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
class UnlinkCommand extends Command
23
{
24
    protected function configure(): void
25
    {
26
        $this->setName('unlink');
27
        $this->setDescription('Unlink a linked package');
28
        $this->addArgument('path', InputArgument::REQUIRED, 'The path of the package');
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    protected function execute(InputInterface $input, OutputInterface $output): int
35
    {
36
        $paths = $this->getPaths($input);
37
38
        foreach ($paths as $path) {
39
            $repository = $this->plugin->getRepository();
40
            $linkedPackage = $repository->findByPath($path->getNormalizedPath());
41
42
            if ($linkedPackage === null) {
43
                continue;
44
            }
45
46
            $this->plugin->getLinkManager()->unlinkPackage($linkedPackage);
47
            $this->plugin->getRepository()->remove($linkedPackage);
48
            $this->plugin->getRepository()->persist();
49
        }
50
51
        return 0;
52
    }
53
}
54