SanderSander/composer-link

View on GitHub
src/Commands/UnlinkCommand.php

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
<?php

declare(strict_types=1);

/*
 * This file is part of the composer-link plugin.
 *
 * Copyright (c) 2021-2023 Sander Visser <themastersleader@hotmail.com>.
 *
 * For the full copyright and license information, please view the LICENSE.md
 * file that was distributed with this source code.
 *
 * @link https://github.com/SanderSander/composer-link
 */

namespace ComposerLink\Commands;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class UnlinkCommand extends Command
{
    protected function configure(): void
    {
        $this->setName('unlink');
        $this->setDescription('Unlink a linked package');
        $this->addArgument('path', InputArgument::REQUIRED, 'The path of the package');
    }

    /**
     * {@inheritDoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $paths = $this->getPaths($input);

        foreach ($paths as $path) {
            $repository = $this->plugin->getRepository();
            $linkedPackage = $repository->findByPath($path->getNormalizedPath());

            if ($linkedPackage === null) {
                continue;
            }

            $this->plugin->getLinkManager()->unlinkPackage($linkedPackage);
            $this->plugin->getRepository()->remove($linkedPackage);
            $this->plugin->getRepository()->persist();
        }

        return 0;
    }
}