<?php

namespace portalpeople\stripe\controllers\api;

use Yii;
use yii\filters\RateLimiter;
use yii\helpers\ArrayHelper;

class BaseController extends \yii\rest\Controller
{
    public function behaviors(): array
    {
        return ArrayHelper::merge(
            parent::behaviors(),
            $this->getSharedBaseBehaviors(),
            [
                'rateLimiter' => [
                    'class' => RateLimiter::class,
                    'enableRateLimitHeaders' => true,
                ],
            ]
        );
    }

    private function getSharedBaseBehaviors(): array
    {
        $sharedBaseControllerClass = \api\defaults\controllers\BaseController::class;
        $sharedBaseControllerFile = dirname(__DIR__, 5) . '/NGauge_Base/api/defaults/controllers/BaseController.php';

        if (!class_exists($sharedBaseControllerClass, false) && is_file($sharedBaseControllerFile)) {
            require_once $sharedBaseControllerFile;
        }

        if (!class_exists($sharedBaseControllerClass)) {
            return [];
        }

        /** @var \yii\rest\Controller $sharedController */
        $sharedController = new $sharedBaseControllerClass($this->id, $this->module);
        $behaviors = $sharedController->behaviors();

        return array_intersect_key($behaviors, array_flip([
            'authenticator',
            'exceptionFilter',
        ]));
    }
}
