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

LinkCommand::linkPackage()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
c 0
b 0
f 0
dl 0
loc 24
rs 9.7998
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 InvalidArgumentException;
20
use Symfony\Component\Console\Input\InputArgument;
21
use Symfony\Component\Console\Input\InputInterface;
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
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    protected function execute(InputInterface $input, OutputInterface $output): int
38
    {
39
        $helper = new PathHelper($input->getArgument('path'));
40
41
        // When run in global we should transform path to absolute path
42
        if ($this->plugin->isGlobal()) {
43
            /** @var string $working */
44
            $working = $this->getApplication()->getInitialWorkingDirectory();
45
            $helper = $helper->toAbsolutePath($working);
46
        }
47
48
        $paths = $helper->isWildCard() ? $helper->getPathsFromWildcard() : [$helper];
49
        foreach ($paths as $path) {
50
            $this->linkPackage($path);
51
        }
52
53
        return 0;
54
    }
55
56
57
    // TODO instead of throwing exception, we should show a warning and continue,
58
    //      this is needed when use wildcards so we can continue the process
59
    protected function linkPackage(PathHelper $helper): void
60
    {
61
        $linkedPackage = $this->plugin->getPackageFactory()->fromPath($helper->getNormalizedPath());
62
63
        if (!is_null($this->plugin->getRepository()->findByPath($helper->getNormalizedPath()))) {
64
            throw new InvalidArgumentException(
65
                sprintf('Package in path "%s" already linked', $helper->getNormalizedPath())
66
            );
67
        }
68
69
        $currentLinked = $this->plugin->getRepository()->findByName($linkedPackage->getName());
70
        if (!is_null($currentLinked)) {
71
            throw new InvalidArgumentException(
72
                sprintf(
73
                    'Package "%s" already linked from path "%s"',
74
                    $linkedPackage->getName(),
75
                    $currentLinked->getPath()
76
                )
77
            );
78
        }
79
80
        $this->plugin->getRepository()->store($linkedPackage);
81
        $this->plugin->getRepository()->persist();
82
        $this->plugin->getLinkManager()->linkPackage($linkedPackage);
83
    }
84
}
85