Skip to content

Improved collector #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions ClientFactory/PluginClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ final class PluginClientFactory
/**
* @param Plugin[] $plugins
* @param ClientFactory $factory
* @param array $config
* @param array $config config to the client factory
* @param array $pluginClientOptions config forwarded to the PluginClient
*
* @return PluginClient
*/
public static function createPluginClient(array $plugins, ClientFactory $factory, array $config)
public static function createPluginClient(array $plugins, ClientFactory $factory, array $config, array $pluginClientOptions = [])
{
return new PluginClient($factory->createClient($config), $plugins);
return new PluginClient($factory->createClient($config), $plugins, $pluginClientOptions);
}
}
63 changes: 63 additions & 0 deletions Collector/DebugPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Http\HttplugBundle\Collector;

use Http\Client\Common\Plugin;
use Http\Client\Exception;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
* A plugin used for log requests and responses. This plugin is executed between each normal plugin.
*
* @author Tobias Nyholm <[email protected]>
*/
final class DebugPlugin implements Plugin
{
/**
* @var DebugPluginCollector
*/
private $collector;

/**
* @var string
*/
private $clientName;

/**
* @var int
*/
private $depth = -1;

/**
* @param DebugPluginCollector $collector
* @param string $clientName
*/
public function __construct(DebugPluginCollector $collector, $clientName)
{
$this->collector = $collector;
$this->clientName = $clientName;
}

/**
* {@inheritdoc}
*/
public function handleRequest(RequestInterface $request, callable $next, callable $first)
{
$collector = $this->collector;
$clientName = $this->clientName;
$depth = &$this->depth;

$collector->addRequest($request, $clientName, ++$depth);

return $next($request)->then(function (ResponseInterface $response) use ($collector, $clientName, &$depth) {
$collector->addResponse($response, $clientName, $depth--);

return $response;
}, function (Exception $exception) use ($collector, $clientName, &$depth) {
$collector->addFailure($exception, $clientName, $depth--);

throw $exception;
});
}
}
181 changes: 181 additions & 0 deletions Collector/DebugPluginCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
<?php

namespace Http\HttplugBundle\Collector;

use Http\Client\Exception;
use Http\Message\Formatter;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;

/**
* A data collector for the debug plugin.
*
* @author Tobias Nyholm <[email protected]>
*/
final class DebugPluginCollector extends DataCollector
{
/**
* @var Formatter
*/
private $formatter;

/**
* @var PluginJournal
*/
private $journal;

/**
* @param Formatter $formatter
* @param PluginJournal $journal
*/
public function __construct(Formatter $formatter, PluginJournal $journal)
{
$this->formatter = $formatter;
$this->journal = $journal;
}

/**
* @param RequestInterface $request
Copy link
Member

@sagikazarmark sagikazarmark Jul 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing phpdoc or inheritdoc

* @param string $clientName
* @param int $depth
*/
public function addRequest(RequestInterface $request, $clientName, $depth)
{
$this->data[$clientName]['request'][$depth][] = $this->formatter->formatRequest($request);
}

/**
* @param ResponseInterface $response
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing phpdoc or inheritdoc

* @param string $clientName
* @param int $depth
*/
public function addResponse(ResponseInterface $response, $clientName, $depth)
{
$this->data[$clientName]['response'][$depth][] = $this->formatter->formatResponse($response);
$this->data[$clientName]['failure'][$depth][] = false;
}

/**
* @param Exception $exception
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing phpdoc or inheritdoc

* @param string $clientName
* @param int $depth
*/
public function addFailure(Exception $exception, $clientName, $depth)
{
if ($exception instanceof Exception\HttpException) {
$formattedResponse = $this->formatter->formatResponse($exception->getResponse());
} elseif ($exception instanceof Exception\TransferException) {
$formattedResponse = $exception->getMessage();
} else {
$formattedResponse = sprintf('Unexpected exception of type "%s"', get_class($exception));
}

$this->data[$clientName]['response'][$depth][] = $formattedResponse;
$this->data[$clientName]['failure'][$depth][] = true;
}

/**
* Returns the successful request-resonse pairs.
*
* @return array
*/
public function getSucessfulRequests()
{
$count = 0;
foreach ($this->data as $client) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing newline before control structure

if (isset($client['failure'])) {
foreach ($client['failure'][0] as $failure) {
if (!$failure) {
++$count;
}
}
}
}

return $count;
}

/**
* Returns the failed request-resonse pairs.
*
* @return array
*/
public function getFailedRequests()
{
$count = 0;
foreach ($this->data as $client) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing newline before control structure

if (isset($client['failure'])) {
foreach ($client['failure'][0] as $failure) {
if ($failure) {
++$count;
}
}
}
}

return $count;
}

/**
* Returns the total number of request made.
*
* @return int
*/
public function getTotalRequests()
{
return $this->getSucessfulRequests() + $this->getFailedRequests();
}

/**
* Return a RequestStackProvider for each client.
*
* @return RequestStackProvider[]
*/
public function getClients()
{
return RequestStackProvider::createFromCollectedData($this->data);
}

/**
* @return PluginJournal
*/
public function getJournal()
{
return $this->journal;
}

/**
* {@inheritdoc}
*/
public function collect(Request $request, Response $response, \Exception $exception = null)
{
// We do not need to collect any data from the Symfony Request and Response
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'httplug';
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say we should add inheritdoc here.

/**
* {@inheritdoc}
*/
public function serialize()
{
return serialize([$this->data, $this->journal]);
}

/**
* {@inheritdoc}
*/
public function unserialize($data)
{
list($this->data, $this->journal) = unserialize($data);
}
}
109 changes: 0 additions & 109 deletions Collector/MessageJournal.php

This file was deleted.

Loading