<?php

namespace PortalPeople\oauth2;

trait BootstrapTrait
{
    /**
     * @var array Model's map
     */
    private $_modelMap = [
        'OauthClients'               => 'PortalPeople\oauth2\models\OauthClients',
        'OauthAccessTokens'          => 'PortalPeople\oauth2\models\OauthAccessTokens',
        'OauthAuthorizationCodes'    => 'PortalPeople\oauth2\models\OauthAuthorizationCodes',
        'OauthRefreshTokens'         => 'PortalPeople\oauth2\models\OauthRefreshTokens',
        'OauthScopes'                => 'PortalPeople\oauth2\models\OauthScopes',
    ];
    
    /**
     * @var array Storage's map
     */
    private $_storageMap = [
        'access_token'          => 'PortalPeople\oauth2\storage\Pdo',
        'authorization_code'    => 'PortalPeople\oauth2\storage\Pdo',
        'client_credentials'    => 'PortalPeople\oauth2\storage\Pdo',
        'client'                => 'PortalPeople\oauth2\storage\Pdo',
        'refresh_token'         => 'PortalPeople\oauth2\storage\Pdo',
        'user_credentials'      => 'PortalPeople\oauth2\models\Pdo',
        'public_key'            => 'PortalPeople\oauth2\storage\Pdo',
        'jwt_bearer'            => 'PortalPeople\oauth2\storage\Pdo',
        'scope'                 => 'PortalPeople\oauth2\storage\Pdo',
    ];
    
    protected function initModule(Module $module)
    {
        $this->_modelMap = array_merge($this->_modelMap, $module->modelMap);
        foreach ($this->_modelMap as $name => $definition) {
            \Yii::$container->set("PortalPeople\\oauth2\\models\\" . $name, $definition);
            $module->modelMap[$name] = is_array($definition) ? $definition['class'] : $definition;
        }

        $this->_storageMap = array_merge($this->_storageMap, $module->storageMap);
        foreach ($this->_storageMap as $name => $definition) {
            \Yii::$container->set("PortalPeople\\oauth2\\storage\\" . $name, $definition);
            $module->storageMap[$name] = is_array($definition) ? $definition['class'] : $definition;
        }
    }
}