HEX
Server: Apache
System: Linux srv13.cpanelhost.cl 3.10.0-962.3.2.lve1.5.38.el7.x86_64 #1 SMP Thu Jun 18 05:28:41 EDT 2020 x86_64
User: cca63905 (4205)
PHP: 7.3.20
Disabled: NONE
Upload Files
File: //proc/self/cwd/nueva/vendor/csa/guzzle-bundle/src/GuzzleHttp/Subscriber/StopwatchSubscriber.php
<?php

/*
 * This file is part of the CsaGuzzleBundle package
 *
 * (c) Charles Sarrazin <charles@sarraz.in>
 * (c) PrestaShop and Contributors
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code
 */

namespace Csa\Bundle\GuzzleBundle\GuzzleHttp\Subscriber;

use GuzzleHttp\Event\BeforeEvent;
use GuzzleHttp\Event\CompleteEvent;
use GuzzleHttp\Event\ErrorEvent;
use GuzzleHttp\Event\RequestEvents;
use GuzzleHttp\Event\SubscriberInterface;
use Symfony\Component\Stopwatch\Stopwatch;

/**
 * Csa Guzzle Stopwatch integration.
 *
 * @author Charles Sarrazin <charles@sarraz.in>
 */
class StopwatchSubscriber implements SubscriberInterface
{
    private $stopwatch;

    public function __construct(Stopwatch $stopwatch)
    {
        $this->stopwatch = $stopwatch;
    }

    public function getEvents()
    {
        return [
            'before' => ['onBefore', RequestEvents::EARLY],
            'complete' => ['onFinish', RequestEvents::LATE],
            'error' => ['onError', RequestEvents::EARLY],
        ];
    }

    public function onBefore(BeforeEvent $event)
    {
        $this->stopwatch->start($event->getRequest()->getUrl(), 'guzzle');
    }

    public function onFinish(CompleteEvent $event)
    {
        $url = $event->getRequest()->getUrl();

        if (!$this->stopwatch->isStarted($url)) {
            return;
        }

        $this->stopwatch->stop($url);
    }

    public function onError(ErrorEvent $event)
    {
        $url = $event->getRequest()->getUrl();

        if (!$this->stopwatch->isStarted($url)) {
            return;
        }

        $this->stopwatch->stop($url);
    }
}