Validate and enforce repository type enum usage

This commit is contained in:
netkas 2024-09-18 00:54:12 -04:00
parent ffc6588ff9
commit 836d0f33eb
2 changed files with 14 additions and 12 deletions

View file

@ -25,6 +25,7 @@
use Exception;
use ncc\Enums\ConsoleColors;
use ncc\Enums\Scopes;
use ncc\Enums\Types\RepositoryType;
use ncc\Managers\RepositoryManager;
use ncc\Objects\CliHelpSection;
use ncc\Objects\RepositoryConfiguration;
@ -164,6 +165,13 @@
return 1;
}
$parsed_type = RepositoryType::tryFrom($type);
if($parsed_type === null)
{
Console::outError(sprintf('Unknown repository type \'%s\'.', $type), true, 1);
return 1;
}
if($host === null)
{
Console::outError(sprintf('Missing required argument \'%s\'.', 'host'), true, 1);
@ -172,7 +180,7 @@
try
{
(new RepositoryManager())->addRepository(new RepositoryConfiguration($name, $host, $type, $ssl));
(new RepositoryManager())->addRepository(new RepositoryConfiguration($name, $host, $parsed_type, $ssl));
}
catch(Exception $e)
{

View file

@ -67,10 +67,10 @@
*
* @param string $name The unique name of the remote source. (e.g. 'github')
* @param string $host The host of the service ncc should use with this source (gitlab.com, github.com, git.example.com:8080 etc...).
* @param string $type The type of service ncc should use with this source (gitlab, github, etc...).
* @param RepositoryType $type The type of service ncc should use with this source (gitlab, github, etc...).
* @param bool $ssl If SSL should be used when connecting to the service
*/
public function __construct(string $name, string $host, string $type, bool $ssl=true)
public function __construct(string $name, string $host, RepositoryType $type, bool $ssl=true)
{
$this->setName($name);
$this->setHost($host);
@ -111,18 +111,12 @@
/**
* Sets the type of service ncc should use with this source (gitlab, github, etc...).
*
* @param string $type
*
* @param RepositoryType $type
* @see RepositoryType
*/
public function setType(string $type): void
public function setType(RepositoryType $type): void
{
// TODO: Fix this, not a proper use of cases()
if(!in_array(strtolower($type), RepositoryType::cases(), true))
{
throw new InvalidArgumentException(sprintf('Invalid repository type \'%s\'', $type));
}
$this->type = $type;
}