-
Notifications
You must be signed in to change notification settings - Fork 50
add profile client #136
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
add profile client #136
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for this PR!
Collector/ProfileClient.php
Outdated
*/ | ||
public function __construct($client, Collector $collector) | ||
{ | ||
if ($client instanceof HttpAsyncClient && $client instanceof HttpClient) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove all this logic by using the FlexibleHttpClient
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I use it directly without testing anything or should I use a condition to juste use FlexibleHttpClient
when only one of both interface is implemented?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved this logic to the ProfileClientFactory
and now FlexibleHttpClient
is used.
Collector/ProfileClientFactory.php
Outdated
public function __construct($factory, Collector $collector) | ||
{ | ||
if (!$factory instanceof ClientFactory && !is_callable($factory)) { | ||
throw new \RuntimeException(sprintf('Second argument to PluginClientFactory::createPluginClient must be a "%s" or a callable.', ClientFactory::class)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First argument...
Collector/ProfileClient.php
Outdated
use Http\Client\HttpClient; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
class ProfileClient implements HttpClient, HttpAsyncClient |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a class comment that explains the purpose of this new client.
I added the |
I would do extensive tests about my code but how can I create a client using a callable instead of a factory with the bundle? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great, thanks! i only added some comments on phpdoc and code clarity.
re callable: maybe this could help? http://stackoverflow.com/questions/29792862/how-can-i-inject-callable-into-symfon2-dependency-injection-container-service - i assume you can do the same but configure a class as factory class.
that said, not sure if you need a callable - you can also set a service to a fixture class or something, if you need that instead.
Collector/ProfileClient.php
Outdated
*/ | ||
public function __construct($client, Collector $collector) | ||
{ | ||
if (!$client instanceof HttpClient || !$client instanceof HttpAsyncClient) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i would write this as if (!($client instanceof HttpClient && $client instanceof HttpAsyncClient)) {
to make it more explicit. (just rewriting the expression, the logic is the same)
Collector/ProfileClient.php
Outdated
private $collector; | ||
|
||
/** | ||
* @param HttpClient|HttpAsyncClient $client |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this indicates that the client can be either, which is not correct. it needs to implement both. no idea if there even is a syntax for that, but the description should say that.
Collector/ProfileClient.php
Outdated
use Psr\Http\Message\RequestInterface; | ||
|
||
/** | ||
* The ProfileClient decorates any HttpClient (or HttpAsyncClient) to gather target url and response status code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
description is not accurate, it decorates a FlexibleClient or something else implementing both interfaces
Collector/ProfileClientFactory.php
Outdated
{ | ||
$client = is_callable($this->factory) ? $this->factory($config) : $this->factory->createClient($config); | ||
|
||
if (!$client instanceof HttpClient || !$client instanceof HttpAsyncClient) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same remark as above, i think !(instanceof && instanceof) would be easier to read
} | ||
} | ||
|
||
interface ClientInterface extends HttpClient, HttpAsyncClient |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or just mock flexible client? not sure whats better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FlexibleHttpClient
is final
!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does that prevent from doing $this->createMock(FlexiblehttpClient::class)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
argh, thats quite stupid. ok then this is fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the drawback of the final
keyword. But this case is special as we have to Mock both HttpClient
and HttpAsyncClient
interface.
Should this interface moved in an other package (in the tests section)?
EDIT: having this interface here makes it not autoloadable which is good!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah i think its right to have it here. its not something that anybody should rely on.
From my POV, this is ready :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great. Im happy with this PR.
Good work and good review by David!
private $collector; | ||
|
||
/** | ||
* @param HttpClient|HttpAsyncClient $client The client to profile. Client must implement both HttpClient and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about introducing a new interface extending both interfaces and type hinting against that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That may be convenient but it does not comply with the Interface Segregation principle.
I know we talked about this before and decided not to, but I cannot find that thread atm.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found it on Slack: https://php-http.slack.com/archives/C0D2U9SR3/p1487721978001035
(register here: http://slack.httplug.io/)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ISP states not to make big interfaces, so that people don't need to implement all of the methods inside. In your case, they still can choose to implement either inherited interface, so ISP wouldn't be violated here IMO. And if you do need both methods, it looks like the best bay to express that need.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be honest, I don't understand what inversion of control has to do with this, but I trust you have your (maybe fairly complicated) reasons. Plus, I wasn't aware there were other capabilities, and if an interface is introduced for every combination, this could get quite messy… the real thing to fix here might be php itself , to add the possibility to type hint against several interfaces :P
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More about this here
What's in this PR?
This add tracking of request target url and method. I choose to decorate the Client at the lowest possible level (i.e not the PluginClient, but the inner one) to be able to catch what goes in and out the Client and avoid false informations because of the registered plugins.
Technically, when the profiling is enabled, each client factory is decorated with a
ProfileClientFactory
which returns the original client decorated with aProfileClient
.This is what I suggested for #135
Why?
Those new collected informations are required to write a better profiler.
To Do