<commit_before><?php
namespace Platformsh\Cli\Command\User;

use Platformsh\Cli\Command\PlatformCommand;
use Platformsh\Cli\Util\ActivityUtil;
use Platformsh\Client\Model\ProjectAccess;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;

class UserAddCommand extends PlatformCommand
{

    protected function configure()
    {
        $this
          ->setName('user:add')
          ->setDescription('Add a user to the project')
          ->addArgument('email', InputArgument::OPTIONAL, "The new user's email address")
          ->addOption('role', null, InputOption::VALUE_REQUIRED, "The new user's role: 'admin' or 'viewer'")
          ->addOption('no-wait', null, InputOption::VALUE_NONE, 'Do not wait for environment(s) to be redeployed');
        $this->addProjectOption();
        $this->addExample('Add Alice as a new administrator', '<EMAIL> --role admin');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->validateInput($input);

        /** @var \Platformsh\Cli\Helper\PlatformQuestionHelper $questionHelper */
        $questionHelper = $this->getHelper('question');

        $email = $input->getArgument('email');
        if ($email && !$this->validateEmail($email)) {
            return 1;
        }
        elseif (!$email) {
            $question = new Question('Email address: ');
            $question->setValidator(array($this, 'validateEmail'));
            $question->setMaxAttempts(5);
            $email = $questionHelper->ask($input, $this->stdErr, $question);
        }

        $project = $this->getSelectedProject();

        $users = $project->getUsers();
        foreach ($users as $projectAccess) {
            if ($projectAccess->getAccount()['email'] === $email) {
                $this->stdErr->writeln("The user already exists: <comment>$email</comment>");
                return 1;
            }
        }

        $projectRole = $input->getOption('role');
        if ($projectRole && !in_array($projectRole, ProjectAccess::$roles)) {
            $this->stdErr->writeln("Valid project-level roles are 'admin' or 'viewer'");
            return 1;
        }
        elseif (!$projectRole) {
            if (!$input->isInteractive()) {
                $this->stdErr->writeln('You must specify a project role for the user.');
                return 1;
            }
            $this->stdErr->writeln("The user's project role can be 'viewer' ('v') or 'admin' ('a').");
            $question = new Question('Project role <question>[V/a]</question>: ', 'viewer');
            $question->setValidator(array($this, 'validateRole'));
            $question->setMaxAttempts(5);
            $projectRole = $this->standardizeRole($questionHelper->ask($input, $this->stdErr, $question));
        }

        $environmentRoles = [];
        $environments = [];
        if ($projectRole !== 'admin') {
            $environments = $this->getEnvironments($project);
            if ($input->isInteractive()) {
                $this->stdErr->writeln("The user's environment-level roles can be 'viewer', 'contributor', 'admin', or 'none'.");
            }
            foreach ($environments as $environment) {
                $question = new Question('<info>' . $environment->id . '</info> environment role <question>[v/c/a/N]</question>: ', 'none');
                $question->setValidator(array($this, 'validateRole'));
                $question->setMaxAttempts(5);
                $environmentRoles[$environment->id] = $this->standardizeRole($questionHelper->ask($input, $this->stdErr, $question));
            }
        }

        $summaryFields = [
            'Email address' => $email,
            'Project role' => $projectRole,
        ];
        if (!empty($environmentRoles)) {
            foreach ($environments as $environment) {
                if (isset($environmentRoles[$environment->id])) {
                    $summaryFields[$environment['title']] = $environmentRoles[$environment->id];
                }
            }
        }

        $this->stdErr->writeln('Summary:');
        foreach ($summaryFields as $field => $value) {
            $this->stdErr->writeln("    $field: <info>$value</info>");
        }

        $this->stdErr->writeln("<comment>Adding users can result in additional charges.</comment>");

        if ($input->isInteractive()) {
            if (!$questionHelper->confirm("Are you sure you want to add this user?", $input, $this->stdErr)) {
                return 1;
            }
        }

        $this->stdErr->writeln("Adding the user to the project");
        $projectAccess = $project->addUser($email, $projectRole);

        if (!empty($environmentRoles)) {
            $this->stdErr->writeln("Setting environment role(s)");
            $activities = [];
            foreach ($environmentRoles as $environmentId => $role) {
                if (!isset($environments[$environmentId])) {
                    $this->stdErr->writeln("<error>Environment not found: $environmentId</error>");
                    continue;
                }
                if ($role == 'none') {
                    continue;
                }
                $access = $environments[$environmentId]->getUser($projectAccess->id);
                if ($access) {
                    $this->stdErr->writeln("Modifying the user's role on the environment: <info>$environmentId</info>");
                    $activity = $access->update(['role' => $role]);
                }
                else {<commit_msg>Unused var<commit_after><?php
namespace Platformsh\Cli\Command\User;

use Platformsh\Cli\Command\PlatformCommand;
use Platformsh\Cli\Util\ActivityUtil;
use Platformsh\Client\Model\ProjectAccess;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;

class UserAddCommand extends PlatformCommand
{

    protected function configure()
    {
        $this
          ->setName('user:add')
          ->setDescription('Add a user to the project')
          ->addArgument('email', InputArgument::OPTIONAL, "The new user's email address")
          ->addOption('role', null, InputOption::VALUE_REQUIRED, "The new user's role: 'admin' or 'viewer'")
          ->addOption('no-wait', null, InputOption::VALUE_NONE, 'Do not wait for environment(s) to be redeployed');
        $this->addProjectOption();
        $this->addExample('Add Alice as a new administrator', '<EMAIL> --role admin');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->validateInput($input);

        /** @var \Platformsh\Cli\Helper\PlatformQuestionHelper $questionHelper */
        $questionHelper = $this->getHelper('question');

        $email = $input->getArgument('email');
        if ($email && !$this->validateEmail($email)) {
            return 1;
        }
        elseif (!$email) {
            $question = new Question('Email address: ');
            $question->setValidator(array($this, 'validateEmail'));
            $question->setMaxAttempts(5);
            $email = $questionHelper->ask($input, $this->stdErr, $question);
        }

        $project = $this->getSelectedProject();

        $users = $project->getUsers();
        foreach ($users as $projectAccess) {
            if ($projectAccess->getAccount()['email'] === $email) {
                $this->stdErr->writeln("The user already exists: <comment>$email</comment>");
                return 1;
            }
        }

        $projectRole = $input->getOption('role');
        if ($projectRole && !in_array($projectRole, ProjectAccess::$roles)) {
            $this->stdErr->writeln("Valid project-level roles are 'admin' or 'viewer'");
            return 1;
        }
        elseif (!$projectRole) {
            if (!$input->isInteractive()) {
                $this->stdErr->writeln('You must specify a project role for the user.');
                return 1;
            }
            $this->stdErr->writeln("The user's project role can be 'viewer' ('v') or 'admin' ('a').");
            $question = new Question('Project role <question>[V/a]</question>: ', 'viewer');
            $question->setValidator(array($this, 'validateRole'));
            $question->setMaxAttempts(5);
            $projectRole = $this->standardizeRole($questionHelper->ask($input, $this->stdErr, $question));
        }

        $environmentRoles = [];
        $environments = [];
        if ($projectRole !== 'admin') {
            $environments = $this->getEnvironments($project);
            if ($input->isInteractive()) {
                $this->stdErr->writeln("The user's environment-level roles can be 'viewer', 'contributor', 'admin', or 'none'.");
            }
            foreach ($environments as $environment) {
                $question = new Question('<info>' . $environment->id . '</info> environment role <question>[v/c/a/N]</question>: ', 'none');
                $question->setValidator(array($this, 'validateRole'));
                $question->setMaxAttempts(5);
                $environmentRoles[$environment->id] = $this->standardizeRole($questionHelper->ask($input, $this->stdErr, $question));
            }
        }

        $summaryFields = [
            'Email address' => $email,
            'Project role' => $projectRole,
        ];
        if (!empty($environmentRoles)) {
            foreach ($environments as $environment) {
                if (isset($environmentRoles[$environment->id])) {
                    $summaryFields[$environment['title']] = $environmentRoles[$environment->id];
                }
            }
        }

        $this->stdErr->writeln('Summary:');
        foreach ($summaryFields as $field => $value) {
            $this->stdErr->writeln("    $field: <info>$value</info>");
        }

        $this->stdErr->writeln("<comment>Adding users can result in additional charges.</comment>");

        if ($input->isInteractive()) {
            if (!$questionHelper->confirm("Are you sure you want to add this user?", $input, $this->stdErr)) {
                return 1;
            }
        }

        $this->stdErr->writeln("Adding the user to the project");
        $projectAccess = $project->addUser($email, $projectRole);

        if (!empty($environmentRoles)) {
            $this->stdErr->writeln("Setting environment role(s)");
            foreach ($environmentRoles as $environmentId => $role) {
                if (!isset($environments[$environmentId])) {
                    $this->stdErr->writeln("<error>Environment not found: $environmentId</error>");
                    continue;
                }
                if ($role == 'none') {
                    continue;
                }
                $access = $environments[$environmentId]->getUser($projectAccess->id);
                if ($access) {
                    $this->stdErr->writeln("Modifying the user's role on the environment: <info>$environmentId</info>");
                    $activity = $access->update(['role' => $role]);
                }
                else {
{'fraction_non_alphanumeric': 0.15421835136799508, 'fraction_numerical': 0.0014075833553268233, 'mean_word_length': 2.311389455286921, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 181, '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': '1991362', 'n_tokens_mistral': 3048, 'n_tokens_neox': 2937, 'n_words': 624}