<?php

namespace AuthStack\OAuth2\Model\Protocol\Response\Implicit;

use AuthStack\OAuth2\Model\Protocol\Response\AbstractResponse;
use Illuminate\Http\Request;

class GrantResponse extends AbstractResponse
{
    public function model(): array
    {
        return [
            'access_token', 'token_type', 'expires_in', 'scope', 'state'
        ];
    }

    public function receive(Request $request = null)
    {
        $request = $request ?? Request::createFromGlobals();

        $collection = collect();

        collect($this->model())->map(function($value) use ($collection, $request)
        {
            $collection->put($value, $request->input($value));
        });

        return $this->setModel($collection->toArray());
    }

    /**
     * Returns ['access_token', 'token_type', 'expires_in', 'scope', 'state']
     *
     * @return array
     */
    public function toArray(): array
    {
        $result = collect($this->model)->reject(function($value, $item)
        {
            return empty($value);
        });

        return $result->toArray();
    }

    public function setAccessToken(string $access_token)
    {
        return $this->setProperty('access_token', $access_token);
    }

    public function setTokenType(string $token_type)
    {
        return $this->setProperty('token_type', $token_type);
    }

    public function setExpiresIn(int $expires_in = 600)
    {
        return $this->setProperty('expires_in', $expires_in);
    }

    public function setScope(string $scope)
    {
        return $this->setProperty('scope', $scope);
    }

    public function setState(string $state)
    {
        return $this->setProperty('state', $state);
    }
}
















