<?php
namespace Incoviba\Service;

use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Ideal;
use Incoviba\Exception\MissingAuthorizationHeader;

class API extends Ideal\Service
{
    /**
     * @throws MissingAuthorizationHeader
     */
    public function getKey(ServerRequestInterface $request): string
    {
        $auth_headers = $request->getHeader('Authorization');
        foreach ($auth_headers as $header) {
            if (str_contains($header, 'Bearer')) {
                return substr($header, strlen('Bearer '));
            }
        }
        throw new MissingAuthorizationHeader();
    }
}
