Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate ServiceConfig #11

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions includes/Services/MachineTranslationServiceConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Miraheze\MachineTranslation\Services;

use ConfigException;
use MediaWiki\Config\ServiceOptions;
use Miraheze\MachineTranslation\ConfigNames;

class MachineTranslationServiceConfig {

public const CONSTRUCTOR_OPTIONS = [
ConfigNames::ServiceConfig,
];

private array $serviceConfig;

public function __construct( ServiceOptions $options ) {
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
$this->serviceConfig = $options->get( ConfigNames::ServiceConfig );
}

public function getType(): string {
if ( !array_key_exists( $this->serviceConfig['type'] ?? '', $this->getRequired() ) ) {
throw new ConfigException( 'Unsupported machine translation service configured.' );
}

return $this->serviceConfig['type'];
}

public function getUrl(): string {
if ( in_array( 'url', $this->getRequired()[$this->getType()] ) ) {
throw new ConfigException( 'URL is required for the configured type.' );
}

return $this->serviceConfig['url'];
}

public function getApiKey(): string {
if ( in_array( 'apikey', $this->getRequired()[$this->getType()] ) ) {
throw new ConfigException( 'API key is required for the configured type.' );
}

return $this->serviceConfig['apikey'];
}

private function getRequired(): array {
return MachineTranslationUtils::REQUIRED_SERVICE_OPTIONS;
}
}
Loading