Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upReturn results as a \Generator #185
Conversation
|
I guess I should also provide some kind of a migration path and a note on update ... will do shortly. |
|
Thanks @t1gor! You can post it as a comment on this PR and I'll include it in the release notes :) |
|
Great job! Thanks a lot! |
|
@t1gor Do you have some time to write some migration path? If not tell me please :) |
|
@Minishlink sorry, totally lost it in a daily routine. Sure, the migration path should be something like the following: ==================================== Breaking changes The way you handle the results of the PLEASE NOTE: BEFORE: $res = [
[ // first notification (failed)
'success' => false,
'endpoint' => $theEndpointToDeleteInYourDatabaseIfExpired,
'message' => $responseMessage,
'statusCode' => $responseStatusCode,
'headers' => $responseHeaders,
'content' => $responseContent, // you may have more infos here
'expired' => $isTheEndpointWrongOrExpired,
],
[ // second notification (succeeded)
'success' => true,
],
[ // third notification
...
], ...
];AFTER: var_dump($res); // \Generator
/** \Minishlink\WebPush\MessageSentReport */
foreach ($res as $result) {
// you now have access to request & response objects
/** @var \Psr\Http\Message\RequestInterface $request */
$request = $result-> getRequest();
/** @var \Psr\Http\Message\ResponseInterface $response */
$response = $result->getResponse();
if ($result->isSuccess()) {
// process successful message sent
$logger->log('Notification with payload %s successfully sent for endpoint %s.' [
json_decode((string) $response->getBody()),
$result->getEndpoint()
]);
} else {
// or a failed one - check expiration first
if ($result->isSubscriptionExpired()) {
// this is just an example code, not included in library!
$db->markExpired($result->getEndpoint());
} else {
// process faulty message
$logger->log('Notification failed: %s. Payload: %s, endpoint: %s' [
$result->getReason(),
json_decode((string) $response->getBody()),
$result->getEndpoint()
]);
}
}
}==================================== This should do the job. Please let me know if I forgot something. Cheers. |
| * @return \Generator | ||
| * @throws \ErrorException | ||
| */ | ||
| public function flush(?int $batchSize = null) : \Generator |
This comment has been minimized.
This comment has been minimized.
Spomky
Nov 5, 2018
•
Contributor
I prefer \iterable instead of \Generator. But it will also work that way.
Ref. http://php.net/manual/en/language.types.iterable.php
This comment has been minimized.
This comment has been minimized.
t1gor
Nov 5, 2018
Author
Contributor
Well, \iterable suggests that you can loop over the object, when the \Generator actually saves some memory for the interpreter, as far as I understand the difference. In this exact case, don't think it's much of a difference any way - the main cause of this pull request is to add possibility to match the request and response directly in flush() results.
This comment has been minimized.
This comment has been minimized.
Spomky
Nov 5, 2018
Contributor
\iterable is just a pseudo type and generators implement that type.
See ex. 4 of the link I gave.
Having \iterable instead of \Generator in the method signature won't change anything regarding the memory saving or the behaviour of the yield used in the method.
You will iterate the same way and foreach ($res as $result) { in your example will still be valid.
From my POV, it is preferable to indicate pseudo-types as return types instead of a concrete/specific type. As I mentioned, it won't change anything, but it will be easier to mock or override the class with the later.
This comment has been minimized.
This comment has been minimized.
t1gor
Nov 5, 2018
•
Author
Contributor
but it will be easier to mock or override the class with the later.
Fair enough. Will try to change in nearest future.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
@Minishlink would you be able to merge this any time soon? The migration path provided in the thread above. |
|
@t1gor Thanks! Sure, I'll try to fix the test suite before though. Let's say if I haven't succeeded by next Saturday night, I'll test manually and merge it :) |
|
Sounds like a plan, thank you.
вс, 11 нояб. 2018 г., 22:03 Louis Lagrange notifications@github.com:
… @t1gor <https://github.com/t1gor> Thanks! Sure, I'll try to fix the test
suite before though. Let's say if I haven't succeeded by next Saturday
night, I'll test manually and merge it :)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#185 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABOIDY9pxofVq4mtxhVurd5Bi5iUKa5bks5uuHSIgaJpZM4WzrBe>
.
|
|
@t1gor Oops, 1 week late, sorry :o |
|
@Minishlink really not sure with the re-base :/ could you please check it? Seems like something went wrong ... |
|
@t1gor Looks like it's good except for a "Vagrantfile" that appears? |
|
@Minishlink my bad, removed. |
eac6697
into
web-push-libs:master
|
@t1gor This is in v5 :) Can you do a quick update of https://github.com/web-push-libs/web-push-php#server-errors please? You either forgot to edit it or the rebase cut it :o |
Seems like I did forget, thanks for pointing out. Would you like another merge request? |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

t1gor commentedSep 21, 2018
As discussed in #183:
\Generatorobject for better performance