<?php

declare(strict_types=1);

namespace App\Integrations\Google\PageSpeed;

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\GuzzleException;
use Symfony\Component\Messenger\MessageBusInterface;

final class GuzzleHandler
{
    private $client;
    private $url = 'https://www.googleapis.com/pagespeedonline/v5/runPagespeed';
    /**
     * @var MessageBusInterface
     */
    private $messageBus;
    /**
     * @var string
     */
    private $key;

    public function __construct(GuzzleClient $client, string $key, MessageBusInterface $messageBus)
    {
        $this->client = $client;
        $this->messageBus = $messageBus;
        $this->key = $key;
    }

    /**
     * @param RequestInterface $request
     *
     * @throws GuzzleException
     */
    public function makeRequest(RequestInterface $request)
    {
        $url = sprintf('%s/?key=%s&%s', $this->url, $this->key, $request->queryString());
        $response = $this->client->request($request->method(), $url);

        return (string) $response->getBody();
    }
}
