Hi Anton,
> > *Not reusable/resettable objects it`s potential problem in future.**Yes, > we can send factories or add empty instances, but it's harder than add > method to reset content.* The pattern that PSR-7 promote is the use of immutable objects. It is maybe a bit harder to use, in some peoples opinion, but on the long run it reduces the number of bugs in a software. Maintaining (unnecessary) state is a source of increased maintenance cost (bugs or harder to change code). Performance wise, there is very small insesizabile impact (when cloning) so don't worry about that. In terms of creating requests, responses or streams, whoever wants to use it, they can just instantiate it if they depend on the implementation. If they however wants to not depend on a specific implementation, they can just use PSR-17 interfaces and bind them at DI config. Also, I want to mention that the code you give as an example has a lot to improve, if you would be willing to accept fast review: - stop using inheritance between classes. Prefer composition over inheritance: https://en.wikipedia.org/wiki/Composition_over_inheritance - stop maintaining unnecessary state, just pass the things you want to keep as a state to methods. I suggest you try learning some functional programming like erlang or haskell - stop using protected keyword, only private. This comes together with not using inheritance. - don't clone the request as body would be the same object. Just write to the body. - use identity operator instead of equal operator Regards, Alex Patranescu On Sat, Oct 12, 2019 at 5:39 PM Anton Fedonjuk <[email protected]> wrote: > *Not reusable/resettable objects it`s potential problem in future.* > *Yes, we can send factories or add empty instances, but it's harder than > add method to reset content.* > > My current code: > abstract class HttpGateway extends Gateway > { > protected $client; > protected $request; > protected $dataSeparator; > > public function __construct(ClientInterface $client, > RequestFactoryInterface $factory) > { > $this->client = $client; > // Checks properties because extended class can overwrite them. > if (! $this->request) { > $this->request = $factory->createRequest('GET', $this->getUrl > ()); > } > if (! $this->dataSeparator) { > $this->dataSeparator = ini_get('arg_separator.output') ?: '&'; > } > } > > protected function sendRequest(array $data) > { > $data = http_build_query($data, '', $this->dataSeparator, > PHP_QUERY_RFC3986); > if ($request->getMethod() == 'POST') { > $request = clone $this->request; > $request->getBody()->write($data); > } else { > $uri = $this->request->getUri()->withQuery($data); > $request = $this->request->withUri($uri, true); > } > $response = $this->client->sendRequest($request); > return $this->parseResponse($response); > } > } > > *As result: I can`t use PSR-7 without PSR-17, than why not merge it and > adds exception iterfaces?* > >> -- > You received this message because you are subscribed to the Google Groups > "PHP Framework Interoperability Group" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/php-fig/5e72e957-3393-431c-a416-597df44bcd0d%40googlegroups.com > <https://groups.google.com/d/msgid/php-fig/5e72e957-3393-431c-a416-597df44bcd0d%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/CAAwdEzA%3D0QoiV50TO_sYZ1yB5WPYv%3D3C_nVg_%3DO_HsGPd8AGZQ%40mail.gmail.com.
