Skip to content

Commit

Permalink
feat: use event name instead of const to be psr-14 compliant (#62)
Browse files Browse the repository at this point in the history
* feat: use event name instead of const to be psr-14 compliant

* chore: add typehints, update phpdoc format
  • Loading branch information
GautierLB authored Nov 7, 2019
1 parent 569f105 commit ccd52ec
Show file tree
Hide file tree
Showing 10 changed files with 313 additions and 319 deletions.
6 changes: 3 additions & 3 deletions src/DataCollector/GuzzleHttpDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use M6Web\Bundle\GuzzleHttpBundle\EventDispatcher\GuzzleHttpEvent;
use M6Web\Bundle\GuzzleHttpBundle\EventDispatcher\AbstractGuzzleHttpEvent;

/**
* Collect information about guzzlehttp client
Expand Down Expand Up @@ -54,9 +54,9 @@ public function getName()
/**
* Collect data for GuzzleHttp
*
* @param GuzzleHttpEvent $event
* @param AbstractGuzzleHttpEvent $event
*/
public function onGuzzleHttpCommand(GuzzleHttpEvent $event)
public function onGuzzleHttpCommand(AbstractGuzzleHttpEvent $event)
{
$request = $event->getRequest();
$response = $event->getResponse();
Expand Down
73 changes: 73 additions & 0 deletions src/EventDispatcher/AbstractGuzzleCacheEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
namespace M6Web\Bundle\GuzzleHttpBundle\EventDispatcher;

use Psr\Http\Message\RequestInterface;
use Symfony\Contracts\EventDispatcher\Event;

/**
* Class AbstractGuzzleCacheEvent
*/
abstract class AbstractGuzzleCacheEvent extends Event
{
/** @const string Name of standard event */
const NAME = 'm6web.guzzlecache';

/** @const string Name of Error event */
const NAME_ERROR = 'm6web.guzzlecache.error';

/** @var RequestInterface */
protected $request;

/** @var \Exception|null */
protected $exception;

/**
* GuzzleCacheEvent constructor.
*
* @param RequestInterface $request
*/
public function __construct(RequestInterface $request)
{
$this->request = $request;
}

/**
* Get Request
*
* @return RequestInterface
*/
public function getRequest(): RequestInterface
{
return $this->request;
}

/**
* Set Exception
*
* @param \Exception $e
*/
public function setException(\Exception $e)
{
$this->exception = $e;
}

/**
* Get Exception if isset
*
* @return \Exception|null
*/
public function getException() : ?\Exception
{
return $this->exception;
}

/**
* Get domain from Request, with graphite compatibility (remove point)
*
* @return string
*/
public function getDomain(): string
{
return str_replace('.', '_', $this->request->getUri()->getHost());
}
}
182 changes: 182 additions & 0 deletions src/EventDispatcher/AbstractGuzzleHttpEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<?php
namespace M6Web\Bundle\GuzzleHttpBundle\EventDispatcher;

use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Symfony\Contracts\EventDispatcher\Event;

/**
* Class AbstractGuzzleHttpEvent
*/
abstract class AbstractGuzzleHttpEvent extends Event
{
const EVENT_NAME = 'm6web.guzzlehttp';
const EVENT_ERROR_NAME = 'm6web.guzzlehttp.error';

/** @var float Command start time */
protected $executionStart;

/** @var float Command execution time */
protected $executionTime;

/** @var Request */
protected $request;

/** @var Response */
protected $response;

/** @var \Exception|null */
protected $reason;

/** @var string */
protected $clientId;

/**
* Set request
*
* @param Request $request
*
* @return static
*/
public function setRequest(RequestInterface $request)
{
$this->request = $request;

return $this;
}

/**
* Return request
*
* @return RequestInterface
*/
public function getRequest() : RequestInterface
{
return $this->request;
}

/**
* Set Response
*
* @param ResponseInterface $response
*
* @return static
*/
public function setResponse(ResponseInterface $response)
{
$this->response = $response;

return $this;
}

/**
* Return response
*
* @return ResponseInterface
*/
public function getResponse() : ResponseInterface
{
return $this->response;
}

/**
* Set reason
*
* @param \Exception $reason
*
* @return static
*/
public function setReason(\Exception $reason)
{
$this->reason = $reason;

return $this;
}

/**
* Return reason
*
* @return \Exception|null
*/
public function getReason() : ?\Exception
{
return $this->reason;
}

/**
* @return float
*/
public function getExecutionStart() : float
{
return $this->executionStart;
}

/**
* Set execution start of a request
*
* @return static
*/
public function setExecutionStart()
{
$this->executionStart = microtime(true);

return $this;
}

/**
* Stop the execution of a request
* and set the request execution time
*
* @return static
*/
public function setExecutionStop()
{
$this->executionTime = microtime(true) - $this->executionStart;

return $this;
}

/**
* @return float
*/
public function getExecutionTime() : float
{
return $this->executionTime;
}

/**
* Return execution time in milliseconds
*
* @return float
*/
public function getTiming() : float
{
return $this->getExecutionTime() * 1000;
}

/**
* Get client ID
*
* @return string
*/
public function getClientId() : string
{
return $this->clientId;
}

/**
* Set client ID
*
* @param string $clientId
*
* @return static
*/
public function setClientId($clientId)
{
$this->clientId = $clientId;

return $this;
}
}
12 changes: 12 additions & 0 deletions src/EventDispatcher/GuzzleCacheErrorEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace M6Web\Bundle\GuzzleHttpBundle\EventDispatcher;

/**
* Class GuzzleCacheErrorEvent
*/
class GuzzleCacheErrorEvent extends AbstractGuzzleCacheEvent
{
}
70 changes: 5 additions & 65 deletions src/EventDispatcher/GuzzleCacheEvent.php
Original file line number Diff line number Diff line change
@@ -1,72 +1,12 @@
<?php
namespace M6Web\Bundle\GuzzleHttpBundle\EventDispatcher;

use Psr\Http\Message\RequestInterface;
use Symfony\Component\EventDispatcher\Event;
declare(strict_types=1);

namespace M6Web\Bundle\GuzzleHttpBundle\EventDispatcher;

/**
* Class GuzzleHttpEvent
* Class GuzzleCacheEvent
*/
class GuzzleCacheEvent extends Event
class GuzzleCacheEvent extends AbstractGuzzleCacheEvent
{
/** @const string Name of standard event */
const NAME = 'm6web.guzzlecache';

/** @const string Name of Error event */
const NAME_ERROR = 'm6web.guzzlecache.error';

/** @var RequestInterface */
protected $request;
/** @var \Exception|null */
protected $exception;

/**
* GuzzleCacheEvent constructor.
*
* @param RequestInterface $request
*/
public function __construct(RequestInterface $request)
{
$this->request = $request;
}

/**
* Get Request
*
* @return RequestInterface
*/
public function getRequest(): RequestInterface
{
return $this->request;
}

/**
* Set Exception
*
* @param \Exception $e
*/
public function setException(\Exception $e)
{
$this->exception = $e;
}

/**
* Get Exception if isset
*
* @return mixed
*/
public function getException()
{
return $this->exception;
}

/**
* Get domain from Request, with graphite compatibility (remove point)
*
* @return string
*/
public function getDomain(): string
{
return str_replace('.', '_', $this->request->getUri()->getHost());
}
}
12 changes: 12 additions & 0 deletions src/EventDispatcher/GuzzleHttpErrorEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace M6Web\Bundle\GuzzleHttpBundle\EventDispatcher;

/**
* Class GuzzleHttpErrorEvent
*/
class GuzzleHttpErrorEvent extends AbstractGuzzleHttpEvent
{
}
Loading

0 comments on commit ccd52ec

Please sign in to comment.