<?php

declare(strict_types=1);
/**
 * Copyright 2022-2025 FOSSBilling
 * Copyright 2011-2021 BoxBilling, Inc.
 * SPDX-License-Identifier: Apache-2.0.
 *
 * @copyright FOSSBilling (https://www.fossbilling.org)
 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
 */

use FOSSBilling\InjectionAwareInterface;

class Api_Abstract implements InjectionAwareInterface
{
    /**
     * @var string - request ip
     */
    protected $ip;

    /**
     * @var FOSSBilling\Module
     */
    protected $mod;

    // TODO: Find a way to correctly set the type. Maybe a module's service should extend a "Service" class?
    protected $service;

    /**
     * @var Model_Admin|Model_Client|Model_Guest
     */
    protected $identity;

    protected ?Pimple\Container $di = null;

    public function __construct()
    {
        if (function_exists('Tests\Helpers\container')) {
            $this->di = Tests\Helpers\container();
        }
    }

    public function setDi(Pimple\Container $di): void
    {
        $this->di = $di;
    }

    public function getDi(): ?Pimple\Container
    {
        if ($this->di === null && function_exists('Tests\Helpers\container')) {
            $this->di = Tests\Helpers\container();
        }

        return $this->di;
    }

    /**
     * @param FOSSBilling\Module $mod
     */
    public function setMod($mod): void
    {
        $this->mod = $mod;
    }

    /**
     * @return FOSSBilling\Module
     */
    public function getMod()
    {
        // @phpstan-ignore booleanNot.alwaysFalse (Runtime check to ensure mod is set)
        if (!$this->mod) {
            throw new FOSSBilling\Exception('Mod object is not set for the service');
        }

        return $this->mod;
    }

    /**
     * @param Model_Admin|Model_Client|Model_Guest $identity
     */
    public function setIdentity($identity): void
    {
        $this->identity = $identity;
    }

    /**
     * @return Model_Admin|Model_Client|Model_Guest
     */
    public function getIdentity()
    {
        return $this->identity;
    }

    // TODO: Find a way to correctly set the type. Maybe a module's service should extend a "Service" class?
    public function setService($service): void
    {
        $this->service = $service;
    }

    // TODO: Find a way to correctly set the type. Maybe a module's service should extend a "Service" class?
    public function getService()
    {
        return $this->service;
    }

    /**
     * @param string $ip
     */
    public function setIp($ip): void
    {
        $this->ip = $ip;
    }

    /**
     * @return string
     */
    public function getIp()
    {
        return $this->ip;
    }
}
