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

LinkCommand::getPackage()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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