public function handleRequest(Request $request)
    {
        if ($this->cassette === null) {
            throw new \BadMethodCallException(
                'Invalid http request. No cassette inserted. '
                . 'Please make sure to insert a cassette in your unit test using '
                . "VCR::insertCassette('name');"
            );
        }

        $event = new BeforePlaybackEvent($request, $this->cassette);
        $this->dispatch(VCREvents::VCR_BEFORE_PLAYBACK, $event);

        $response = $this->cassette->playback($request);

        // Playback succeeded and the recorded response can be returned.
        if (!empty($response)) {
            $event = new AfterPlaybackEvent($request, $response, $this->cassette);
            $this->dispatch(VCREvents::VCR_AFTER_PLAYBACK, $event);
            return $response;
        }

        if (VCR::MODE_NONE === $this->config->getMode()
            || VCR::MODE_ONCE === $this->config->getMode()
            && $this->cassette->isNew() === false
        ) {
            throw new \LogicException(
                sprintf(
                    "The request does not match a previously recorded request and the 'mode' is set to '%s'. "
                    . "If you want to send the request anyway, make sure your 'mode' is set to 'new_episodes'. "
                    . 'Please see http://php-vcr.github.io/documentation/configuration/#record-modes.'
                    . "\nCassette: %s \n Request: %s",
                    $this->config->getMode(),
                    $this->cassette->getName(),
                    print_r($request->toArray(), true)
                )
            );
        }

        $this->disableLibraryHooks();

        $this->dispatch(VCREvents::VCR_BEFORE_HTTP_REQUEST, new BeforeHttpRequestEvent($request));
        $response = $this->client->send($request);
        $this->dispatch(VCREvents::VCR_AFTER_HTTP_REQUEST, new AfterHttpRequestEvent($request, $response));

        $this->dispatch(VCREvents::VCR_BEFORE_RECORD, new BeforeRecordEvent($request, $response, $this->cassette));
        $this->cassette->record($request, $response);
        $this->enableLibraryHooks();

        return $response;
    }