<?php
/**
 * Copyright © Simplyin. All rights reserved.
 */
declare(strict_types=1);

namespace Simplyin\Payment\Model\Resolver;

use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Psr\Log\LoggerInterface;

/**
 * Abstract base resolver for SimplyIN GraphQL resolvers
 *
 * @SuppressWarnings(PHPMD.NumberOfChildren) Thin shared base for the module's GraphQL
 * resolvers (one per mutation/query); the large child count reflects the number of GraphQL
 * operations, not a design imbalance. Splitting the hierarchy would add artificial layers
 * without benefit.
 */
abstract class AbstractResolver implements ResolverInterface
{
    /**
     * @var LoggerInterface
     */
    protected LoggerInterface $logger;

    /**
     * @param LoggerInterface $logger
     */
    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    /**
     * Validate required string argument
     *
     * @param array $args
     * @param string $key
     * @throws GraphQlInputException
     */
    protected function requireArg(array $args, string $key): void
    {
        if (empty($args[$key])) {
            throw new GraphQlInputException(__('"%1" is required.', $key));
        }
    }

    /**
     * Build a failure output array (success => false) with the given message
     *
     * @param string $message
     * @return array
     */
    protected function errorOutput(string $message): array
    {
        return ['success' => false, 'message' => $message];
    }
}
