Skip to content

Improve PHP examples #154

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
Apr 9, 2024
Merged

Improve PHP examples #154

merged 1 commit into from
Apr 9, 2024

Conversation

mnapoli
Copy link
Contributor

@mnapoli mnapoli commented Apr 8, 2024

Issue #, if available:

Description of changes:

I propose a few improvements over the PHP examples. I'll explain why some things changed in the diff.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Copy link
Contributor Author

@mnapoli mnapoli left a comment

Choose a reason for hiding this comment

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

Super happy to see Bref examples here!

Here are some suggestions to improve the examples!

$this->logger->info(json_encode($data));
// TODO: Do interesting work based on the new data

// Any exception thrown will be logged and the invocation will be marked as failed
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think it made sense to catch exceptions, only to rethrow them. The PHP runtime will always log them anyway.

I assumed that maybe it was to clarify what happened in case of an exception, that's why I added the comment: to prevent confusion/questions from AWS users.


function processMessage($record)
class Handler extends SnsHandler
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To improve consistency with other examples (and improve the dev experience) I turned this example into a class that uses the Bref helpers (typed events).

@@ -20,21 +19,13 @@ public function __construct(StderrLogger $logger)
}

/**
* @param SqsEvent $event
* @param Context $context
* @return void
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This duplicated types that already existed in the code. There is no strict standard about this in PHP, but major projects are moving away from duplicating types in phpdoc in favor of using native PHP types. This also makes this example more consistent with the others.

}
} catch (InvalidLambdaEvent $e) {
$this->logger->error($e->getMessage());
throw $e;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same deal here: no need to catch exceptions to rethrow them immediately. They will be logged anyway.

use Bref\Logger\StderrLogger;

require __DIR__ . '/vendor/autoload.php';

class Handler implements StdHandler
class Handler extends SqsHandler
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Bref has utilities for easily using partial batch failures: https://bref.sh/docs/use-cases/sqs#partial-batch-response

I made use of that here by using a more specific class (SqsHandler) than the generic "Handler" class.

@@ -35,25 +33,13 @@ public function handle(mixed $event, Context $context): array
} catch (Exception $e) {
$this->logger->error($e->getMessage());
// failed processing the record
$failedRecords[] = $record->getMessageId();
$this->markAsFailed($record);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It will automatically work with partial batch failures, no need to return anything from the function anymore.

}
}

$logger = new StderrLogger();
return new Handler($logger);

?>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Small cleanup, the closing tag is something we don't use anymore in PHP (it causes issues).

// TODO: Implement your custom processing logic here
// Any exception thrown will be logged and the invocation will be marked as failed

echo "Processed Message: $message" . PHP_EOL;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I also want to propose another change (not implemented yet, because I'd rather ask first): I would remove all usages of Bref's logger class and use echo instead.

The logger class isn't used much, echo is native and works fine. It will be understandable by all PHP developers, and that simplifies the examples a lot.

Let me know if you are OK with this and I'll get to it.

@@ -11,20 +11,23 @@
// Additional composer packages may be required when using Bref or any other PHP functions runtime.
// require __DIR__ . '/vendor/autoload.php';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A final change I'd suggest: I'd make the docblock about Bref consistent across examples, to prevent surprises for readers.

If that's OK with you I can push a change for you to review. I could do that in a separate PR if you want.

@jbesw jbesw merged commit 59cf950 into aws-samples:main Apr 9, 2024
@jbesw
Copy link
Contributor

jbesw commented Apr 9, 2024

Thank Matthieu! Much appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants