diff --git a/src/Controller/BindAPI.php b/src/Controller/BindAPI.php index 261dba5..bbbc803 100755 --- a/src/Controller/BindAPI.php +++ b/src/Controller/BindAPI.php @@ -4,11 +4,8 @@ namespace App\Controller; error_reporting(error_level: E_ALL); -use App\Repository\ApikeyRepository; use App\Repository\DomainRepository; -use App\Repository\NameserverRepository; -use App\Repository\PanelRepository; -use Arubacao\TldChecker\Validator; +use App\Repository\DynDNSRepository; use DI\Container; use DI\ContainerBuilder; use Monolog\Formatter\LineFormatter; @@ -16,105 +13,77 @@ use Monolog\Handler\StreamHandler; use Monolog\Logger; use function DI\autowire; - /** * */ class BindAPI { - public Logger $log; - public ApiController $apiController; - public ApikeyRepository $apikeyRepository; - public DomainController $domainController; - public DomainRepository $domainRepository; - public NameserverRepository $nameserverRepository; - public PanelRepository $panelRepository; + private Logger $logger; private Container $container; + /** + * @throws \Exception + */ + public function __construct($verbose = false) + { + // init the logger + $dateFormat = "Y:m:d H:i:s"; + $output = "%datetime% %channel%.%level_name% %message%\n"; // %context% %extra% + $formatter = new LineFormatter(format: $output, dateFormat: $dateFormat); + + $debug = (new ConfigController)->getConfig(configKey: 'debug'); + if ($debug) { + $stream = new StreamHandler(stream: dirname(path: __DIR__, levels: 2) . '/bindAPI.log', level: Logger::DEBUG); + } else { + $stream = new StreamHandler(stream: dirname(path: __DIR__, levels: 2) . '/bindAPI.log', level: Logger::INFO); + } + $stream->setFormatter(formatter: $formatter); + + $this->logger = new Logger(name: 'bindAPI'); + $this->logger->pushHandler(handler: $stream); + $this->logger->debug(message: 'bindAPI started'); + + + $containerBuilder = new ContainerBuilder(); + $containerBuilder->addDefinitions([ + ConfigController::class => autowire() + ->constructorParameter(parameter: 'verbose', value: $verbose), + CLIController::class => autowire() + ->constructorParameter(parameter: 'logger', value: $this->logger), + DomainController::class => autowire() + ->constructorParameter(parameter: 'logger', value: $this->logger), + DomainRepository::class => autowire() + ->constructorParameter(parameter: 'logger', value: $this->logger), + DynDnsRepository::class => autowire() + ->constructorParameter(parameter: 'logger', value: $this->logger), + RequestController::class => autowire() + ->constructorParameter(parameter: 'logger', value: $this->logger) + ]); + $this->container = $containerBuilder->build(); + } + + /** * @throws \DI\DependencyException * @throws \DI\NotFoundException - * @throws \Exception */ - public function __construct(private readonly array $config) + public function runCommand(int $argumentsCount, array $arguments): void { - $dateFormat = "Y:m:d H:i:s"; - $output = "%datetime% %channel%.%level_name% %message%\n"; // %context% %extra% - $formatter = new LineFormatter(format: $output, dateFormat: $dateFormat); - - $stream = new StreamHandler(stream: dirname(path: __DIR__, levels: 2) . '/bindAPI.log'); - $stream->setFormatter(formatter: $formatter); - - $this->log = new Logger(name: 'bindAPI'); - $this->log->pushHandler(handler: $stream); - $containerBuilder = new ContainerBuilder(); - $containerBuilder->addDefinitions([ - DatabaseConnection::class => autowire()->constructorParameter(parameter: 'config', value: $this->config), - DomainController::class => autowire() - ->constructorParameter(parameter: 'config', value: $this->config) - ->constructorParameter(parameter: 'log', value: $this->log), - DomainRepository::class => autowire() - ->constructorParameter(parameter: 'config', value: $this->config) - ->constructorParameter(parameter: 'log', value: $this->log), - ]); - $this->container = $containerBuilder->build(); - - $this->apiController = $this->container->get(name: ApiController::class); - $this->domainController = $this->container->get(name: DomainController::class); - $this->domainRepository = $this->container->get(name: DomainRepository::class); - $this->nameserverRepository = $this->container->get(name: NameserverRepository::class); - $this->panelRepository = $this->container->get(name: PanelRepository::class); - $this->apikeyRepository = $this->container->get(name: ApikeyRepository::class); - } - - - // TODO mode to domainController - function isValidSecondLevelDomain(string $domainName, string $panel, int $parent): bool - { - if ($this->config['debug']) { - $this->log->debug(message: "isValidSecondLevelDomain()"); - } - - // subdomain - if ($parent != 0) { - return false; - } - - // system domain - if (str_contains(haystack: $domainName, needle: $panel)) { - return false; - } - - // valid second level domain - if (!Validator::endsWithTld(value: $domainName)) { - return false; - } - - // no second level domain - if (substr_count(haystack: $domainName, needle: '.') > 1) { - return false; - } - - return true; + $this->logger->debug(message: 'runCommand()'); + $cliController = $this->container->get(name: CLIController::class); + $cliController->runCommand(argumentsCount: $argumentsCount, arguments: $arguments); } /** - * @return int|void + * @throws \DI\DependencyException + * @throws \DI\NotFoundException */ - public function getId() + public function handleRequest(string $requestMethod, array $uri): void { - if (!empty($this->arguments[1])) { - $id = intval(value: $this->arguments[1] ?? 0); - - if ($id != $this->arguments[1]) { - echo 'ID has to be a number.' . PHP_EOL; - exit(1); - } - } else { - $id = 0; - } - return $id; + $this->logger->debug(message: 'handleRequest()'); + $requestController = $this->container->get(name: RequestController::class); + $requestController->handleRequest(requestMethod: $requestMethod, uri: $uri); } }