Passed
Pull Request — master (#33)
by Sander
02:18
created

LinkCommand::linkPackage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
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\LinkedPackage;
19
use ComposerLink\PathHelper;
20
use Symfony\Component\Console\Input\InputArgument;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Input\InputOption;
23
use Symfony\Component\Console\Output\OutputInterface;
24
25
class LinkCommand extends Command
26
{
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
        $paths = $this->getPaths($input);
46
47
        foreach ($paths as $path) {
48
            $package = $this->getPackage($path, $output);
49
50
            if (is_null($package)) {
51
                continue;
52
            }
53
54
            if ($input->getOption('only-installed') === true && is_null($package->getOriginalPackage())) {
55
                continue;
56
            }
57
58
            $this->linkPackage($package);
59
        }
60
61
        return 0;
62
    }
63
64
    protected function linkPackage(LinkedPackage $package): void
65
    {
66
        $this->plugin->getRepository()->store($package);
67
        $this->plugin->getLinkManager()->linkPackage($package);
68
69
        // Could be optimized, but for now we persist every package,
70
        // so we know what we have done when a package fails
71
        $this->plugin->getRepository()->persist();
72
    }
73
74
    /**
75
     * @return PathHelper[]
76
     */
77
    protected function getPaths(InputInterface $input): array
78
    {
79
        $helper = new PathHelper($input->getArgument('path'));
80
81
        // When run in global we should transform path to absolute path
82
        if ($this->plugin->isGlobal()) {
83
            /** @var string $working */
84
            $working = $this->getApplication()->getInitialWorkingDirectory();
85
            $helper = $helper->toAbsolutePath($working);
86
        }
87
88
        return $helper->isWildCard() ? $helper->getPathsFromWildcard() : [$helper];
89
    }
90
91
    protected function getPackage(PathHelper $helper, OutputInterface $output): ?LinkedPackage
92
    {
93
        $linkedPackage = $this->plugin->getPackageFactory()->fromPath($helper->getNormalizedPath());
94
        $repository = $this->plugin->getRepository();
95
96
        if (!is_null($repository->findByPath($helper->getNormalizedPath()))) {
97
            $output->writeln(
98
                sprintf('<warning>Package in path "%s" already linked</warning>', $helper->getNormalizedPath())
99
            );
100
101
            return null;
102
        }
103
104
        $currentLinked = $repository->findByName($linkedPackage->getName());
105
        if (!is_null($currentLinked)) {
106
            $output->writeln(
107
                sprintf(
108
                    '<warning>Package "%s" already linked from path "%s"</warning>',
109
                    $linkedPackage->getName(),
110
                    $currentLinked->getPath()
111
                )
112
            );
113
114
            return null;
115
        }
116
117
        return $linkedPackage;
118
    }
119
}
120