<commit_before><?php declare(strict_types = 1);
namespace PharIo\Phive;

use PharIo\FileSystem\Directory;
use PharIo\Version\AnyVersionConstraint;
use PharIo\Version\ExactVersionConstraint;
use PharIo\Version\VersionConstraint;
use PharIo\Version\VersionConstraintParser;

class InstallCommandConfig {

    /** @var Cli\Options */
    private $cliOptions;

    /** @var PhiveXmlConfig */
    private $phiveXmlConfig;

    /** @var Environment */
    private $environment;

    /** @var TargetDirectoryLocator */
    private $targetDirectoryLocator;

    public function __construct(
        Cli\Options $options,
        PhiveXmlConfig $phiveXmlConfig,
        Environment $environment,
        TargetDirectoryLocator $targetDirectoryLocator
    ) {
        $this->cliOptions             = $options;
        $this->phiveXmlConfig         = $phiveXmlConfig;
        $this->environment            = $environment;
        $this->targetDirectoryLocator = $targetDirectoryLocator;
    }

    /**
     * @throws ConfigException
     * @throws \PharIo\Phive\Cli\CommandOptionsException
     */
    public function getTargetDirectory(): Directory {
        if ($this->installGlobally()) {
            return $this->environment->getGlobalBinDir();
        }

        return $this->targetDirectoryLocator->getTargetDirectory();
    }

    /**
     * @return RequestedPhar[]
     * @throws InstallCommandConfigException
     * @throws ConfiguredPharException
     * @throws Cli\CommandOptionsException
     *
     * @throws UnsupportedVersionConstraintException
     */
    public function getRequestedPhars(): array {
        if ($this->cliOptions->getArgumentCount() === 0) {
            return $this->getPharsFromPhiveXmlConfig();
        }

        return $this->getPharsFromCliArguments();
    }

    public function doNotAddToPhiveXml(): bool {
        return $this->cliOptions->hasOption('temporary') || $this->installGlobally();
    }

    public function forceAcceptUnsignedPhars(): bool {
        return $this->cliOptions->hasOption('force-accept-unsigned');
    }

    /**
     * @return RequestedPhar[]
     * @throws ConfiguredPharException
     *
     */
    private function getPharsFromPhiveXmlConfig(): array {
        $phars = [];

        foreach ($this->phiveXmlConfig->getPhars() as $configuredPhar) {
            $location = $configuredPhar->hasLocation() ? $configuredPhar->getLocation() : null;

            $phars[] = new RequestedPhar(
                $this->getIdentifier($configuredPhar),
                $configuredPhar->getVersionConstraint(),
                $this->getVersionToInstall($configuredPhar),
                $location,
                $this->makeCopy() || $configuredPhar->isCopy()
            );
        }

        return $phars;
    }

    /**
     * @return RequestedPhar[]
     * @throws Cli\CommandOptionsException
     * @throws UnsupportedVersionConstraintException
     *
     * @throws InstallCommandConfigException
     */
    private function getPharsFromCliArguments(): array {
        $phars    = [];
        $argCount = $this->cliOptions->getArgumentCount();

        for ($i = 0; $i < $argCount; $i++) {
            $argument = $this->cliOptions->getArgument($i);

            if (Url::isUrl($argument)) {
                if (!Url::isHttpsUrl($argument)) {
                    throw new InstallCommandConfigException(<commit_msg>Run cs-fixer<commit_after><?php declare(strict_types = 1);
namespace PharIo\Phive;

use PharIo\FileSystem\Directory;
use PharIo\Version\AnyVersionConstraint;
use PharIo\Version\ExactVersionConstraint;
use PharIo\Version\VersionConstraint;
use PharIo\Version\VersionConstraintParser;

class InstallCommandConfig {

    /** @var Cli\Options */
    private $cliOptions;

    /** @var PhiveXmlConfig */
    private $phiveXmlConfig;

    /** @var Environment */
    private $environment;

    /** @var TargetDirectoryLocator */
    private $targetDirectoryLocator;

    public function __construct(
        Cli\Options $options,
        PhiveXmlConfig $phiveXmlConfig,
        Environment $environment,
        TargetDirectoryLocator $targetDirectoryLocator
    ) {
        $this->cliOptions             = $options;
        $this->phiveXmlConfig         = $phiveXmlConfig;
        $this->environment            = $environment;
        $this->targetDirectoryLocator = $targetDirectoryLocator;
    }

    /**
     * @throws ConfigException
     * @throws \PharIo\Phive\Cli\CommandOptionsException
     */
    public function getTargetDirectory(): Directory {
        if ($this->installGlobally()) {
            return $this->environment->getGlobalBinDir();
        }

        return $this->targetDirectoryLocator->getTargetDirectory();
    }

    /**
     * @throws InstallCommandConfigException
     * @throws ConfiguredPharException
     * @throws Cli\CommandOptionsException
     * @throws UnsupportedVersionConstraintException
     *
     * @return RequestedPhar[]
     */
    public function getRequestedPhars(): array {
        if ($this->cliOptions->getArgumentCount() === 0) {
            return $this->getPharsFromPhiveXmlConfig();
        }

        return $this->getPharsFromCliArguments();
    }

    public function doNotAddToPhiveXml(): bool {
        return $this->cliOptions->hasOption('temporary') || $this->installGlobally();
    }

    public function forceAcceptUnsignedPhars(): bool {
        return $this->cliOptions->hasOption('force-accept-unsigned');
    }

    /**
     * @throws ConfiguredPharException
     *
     * @return RequestedPhar[]
     */
    private function getPharsFromPhiveXmlConfig(): array {
        $phars = [];

        foreach ($this->phiveXmlConfig->getPhars() as $configuredPhar) {
            $location = $configuredPhar->hasLocation() ? $configuredPhar->getLocation() : null;

            $phars[] = new RequestedPhar(
                $this->getIdentifier($configuredPhar),
                $configuredPhar->getVersionConstraint(),
                $this->getVersionToInstall($configuredPhar),
                $location,
                $this->makeCopy() || $configuredPhar->isCopy()
            );
        }

        return $phars;
    }

    /**
     * @throws Cli\CommandOptionsException
     * @throws UnsupportedVersionConstraintException
     * @throws InstallCommandConfigException
     *
     * @return RequestedPhar[]
     */
    private function getPharsFromCliArguments(): array {
        $phars    = [];
        $argCount = $this->cliOptions->getArgumentCount();

        for ($i = 0; $i < $argCount; $i++) {
            $argument = $this->cliOptions->getArgument($i);

            if (Url::isUrl($argument)) {
                if (!Url::isHttpsUrl($argument)) {
                    throw new InstallCommandConfigException(
{'fraction_non_alphanumeric': 0.1180565842097467, 'fraction_numerical': 0.0008887572211524219, 'mean_word_length': 2.570597567424643, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1206562', 'n_tokens_mistral': 1875, 'n_tokens_neox': 1803, 'n_words': 316}