Passed
Pull Request — master (#33)
by Sander
01:53
created

LinkCommand::linkPackage()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 28
rs 9.7333
cc 3
nc 3
nop 1
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 ComposerLink\PathHelper;
19
use Symfony\Component\Console\Input\InputArgument;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Input\InputOption;
22
use Symfony\Component\Console\Output\OutputInterface;
23
24
class LinkCommand extends Command
25
{
26
    // TODO We need to add a flag, to skip packages that are not installed (For when a wildcard is used)
27
    protected function configure(): void
28
    {
29
        $this->setName('link');
30
        $this->setDescription('Link a package to a local directory');
31
        $this->addArgument('path', InputArgument::REQUIRED, 'The path of the package');
32
        $this->addOption(
33
            '--only-installed',
34
            null,
35
            InputOption::VALUE_NEGATABLE,
36
            'Link only installed packages',
37
        );
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    protected function execute(InputInterface $input, OutputInterface $output): int
44
    {
45
        $helper = new PathHelper($input->getArgument('path'));
46
47
        // When run in global we should transform path to absolute path
48
        if ($this->plugin->isGlobal()) {
49
            /** @var string $working */
50
            $working = $this->getApplication()->getInitialWorkingDirectory();
51
            $helper = $helper->toAbsolutePath($working);
52
        }
53
54
        $paths = $helper->isWildCard() ? $helper->getPathsFromWildcard() : [$helper];
55
        // TODO add support for --only-installed
56
        foreach ($paths as $path) {
57
            $this->linkPackage($path);
58
        }
59
60
        return 0;
61
    }
62
63
    protected function linkPackage(PathHelper $helper): void
64
    {
65
        $linkedPackage = $this->plugin->getPackageFactory()->fromPath($helper->getNormalizedPath());
66
67
        if (!is_null($this->plugin->getRepository()->findByPath($helper->getNormalizedPath()))) {
68
            $this->getIO()->writeError(
69
                sprintf('Package in path "%s" already linked', $helper->getNormalizedPath())
70
            );
71
72
            return;
73
        }
74
75
        $currentLinked = $this->plugin->getRepository()->findByName($linkedPackage->getName());
76
        if (!is_null($currentLinked)) {
77
            $this->getIO()->writeError(
78
                sprintf(
79
                    'Package "%s" already linked from path "%s"',
80
                    $linkedPackage->getName(),
81
                    $currentLinked->getPath()
82
                )
83
            );
84
85
            return;
86
        }
87
88
        $this->plugin->getRepository()->store($linkedPackage);
89
        $this->plugin->getRepository()->persist();
90
        $this->plugin->getLinkManager()->linkPackage($linkedPackage);
91
    }
92
}
93