Add missing endpoints#65
Conversation
📝 WalkthroughWalkthroughThis PR adds comprehensive API support for Mailtrap webhooks, API tokens, sub-accounts, and additional sandbox message operations. It introduces new API client classes with corresponding DTOs, wires them into existing and new Mailtrap clients, includes example scripts, updates documentation, and provides full test coverage. ChangesAPI Expansion (Webhooks, Tokens, Sub-Accounts, Message Operations)
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 PHPStan (2.1.51)PHPStan was skipped because the sandbox runner could not parse its output. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Review rate limit: 0/1 reviews remaining, refill in 60 minutes.Comment |
d240511 to
743ef96
Compare
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@examples/api-tokens/all.php`:
- Around line 12-14: Add a guard that checks the required environment variables
before using them: validate $_ENV['MAILTRAP_ACCOUNT_ID'] and
$_ENV['MAILTRAP_API_KEY'] and throw/exit with a clear error message if either is
missing so the example fails fast; update the bootstrap around $accountId, the
Config(...) call, and the MailtrapGeneralClient(...)->apiTokens(...) usage to
rely on the validated values (or sanitized variables) rather than accessing
$_ENV directly.
In `@examples/webhooks/all.php`:
- Around line 12-14: The example currently reads $_ENV['MAILTRAP_ACCOUNT_ID']
and $_ENV['MAILTRAP_API_KEY'] directly which can be null and emit notices; add a
small bootstrap guard before creating Config and calling (new
MailtrapSendingClient($config))->webhooks($accountId) that checks required env
vars (MAILTRAP_ACCOUNT_ID and MAILTRAP_API_KEY), optionally MAILTRAP_DOMAIN_ID
if domain-scoped webhooks are intended, and exits with a clear error message
when any are missing so Config is never constructed with null values and the
script fails fast and informatively.
In `@src/DTO/Request/Webhook/CreateWebhook.php`:
- Around line 21-37: CreateWebhook currently allows building invalid payloads
for type-specific webhooks; add validation in the CreateWebhook constructor (or
in toArray) to enforce required fields based on $webhookType (e.g., when
$webhookType === 'email_sending' require non-null $sendingStream and non-empty
$eventTypes), throw a clear exception (InvalidArgumentException) on violation,
or alternatively refactor by splitting CreateWebhook into separate DTOs per
webhook kind (e.g., EmailSendingWebhook with required $sendingStream and
EventTypes) and use the appropriate DTO where needed; update
CreateWebhook::toArray to assume validated fields so it cannot serialize invalid
requests.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 60e92d97-9770-4f7f-abc9-d3da6787856a
📒 Files selected for processing (24)
.envrcCHANGELOG.mdexamples/api-tokens/all.phpexamples/sub-accounts/all.phpexamples/testing/messages.phpexamples/webhooks/all.phpsrc/Api/General/ApiToken.phpsrc/Api/Organization/OrganizationInterface.phpsrc/Api/Organization/SubAccount.phpsrc/Api/Sandbox/Message.phpsrc/Api/Sending/Webhook.phpsrc/DTO/Request/Webhook/CreateWebhook.phpsrc/DTO/Request/Webhook/UpdateWebhook.phpsrc/DTO/Request/Webhook/WebhookInterface.phpsrc/MailtrapGeneralClient.phpsrc/MailtrapOrganizationClient.phpsrc/MailtrapSendingClient.phptests/Api/General/ApiTokenTest.phptests/Api/Organization/SubAccountTest.phptests/Api/Sandbox/MessageTest.phptests/Api/Sending/WebhookTest.phptests/MailtrapGeneralClientTest.phptests/MailtrapOrganizationClientTest.phptests/MailtrapSendingClientTest.php
| $accountId = $_ENV['MAILTRAP_ACCOUNT_ID']; | ||
| $config = new Config($_ENV['MAILTRAP_API_KEY']); #your API token from here https://mailtrap.io/api-tokens | ||
| $apiTokens = (new MailtrapGeneralClient($config))->apiTokens($accountId); |
There was a problem hiding this comment.
Fail fast when required example env vars are missing.
$_ENV['MAILTRAP_ACCOUNT_ID'] and $_ENV['MAILTRAP_API_KEY'] are accessed directly, so a missing environment setup will surface as notices or a later client error instead of a clear bootstrap failure. A small guard here would make the example much easier to run.
♻️ Suggested bootstrap guard
require __DIR__ . '/../../vendor/autoload.php';
+foreach (['MAILTRAP_ACCOUNT_ID', 'MAILTRAP_API_KEY'] as $envVar) {
+ if (empty($_ENV[$envVar])) {
+ throw new RuntimeException(sprintf('%s must be set for this example.', $envVar));
+ }
+}
+
$accountId = $_ENV['MAILTRAP_ACCOUNT_ID'];
$config = new Config($_ENV['MAILTRAP_API_KEY']); `#your` API token from here https://mailtrap.io/api-tokens🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@examples/api-tokens/all.php` around lines 12 - 14, Add a guard that checks
the required environment variables before using them: validate
$_ENV['MAILTRAP_ACCOUNT_ID'] and $_ENV['MAILTRAP_API_KEY'] and throw/exit with a
clear error message if either is missing so the example fails fast; update the
bootstrap around $accountId, the Config(...) call, and the
MailtrapGeneralClient(...)->apiTokens(...) usage to rely on the validated values
(or sanitized variables) rather than accessing $_ENV directly.
| $accountId = $_ENV['MAILTRAP_ACCOUNT_ID']; | ||
| $config = new Config($_ENV['MAILTRAP_API_KEY']); #your API token from here https://mailtrap.io/api-tokens | ||
| $webhooks = (new MailtrapSendingClient($config))->webhooks($accountId); |
There was a problem hiding this comment.
Fail fast when required example env vars are missing.
Reading $_ENV['MAILTRAP_ACCOUNT_ID'] / $_ENV['MAILTRAP_API_KEY'] directly will emit notices and hand null to Config when the example is run without setup. A small bootstrap guard would make the script much more robust. If you want this sample to demonstrate domain-scoped webhooks, consider documenting MAILTRAP_DOMAIN_ID alongside the other env vars as well.
♻️ Suggested bootstrap guard
<?php
use Mailtrap\Config;
@@
require __DIR__ . '/../../vendor/autoload.php';
+foreach (['MAILTRAP_ACCOUNT_ID', 'MAILTRAP_API_KEY'] as $envVar) {
+ if (empty($_ENV[$envVar])) {
+ throw new RuntimeException(sprintf('%s must be set for this example.', $envVar));
+ }
+}
+
$accountId = $_ENV['MAILTRAP_ACCOUNT_ID'];
$config = new Config($_ENV['MAILTRAP_API_KEY']); `#your` API token from here https://mailtrap.io/api-tokens📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| $accountId = $_ENV['MAILTRAP_ACCOUNT_ID']; | |
| $config = new Config($_ENV['MAILTRAP_API_KEY']); #your API token from here https://mailtrap.io/api-tokens | |
| $webhooks = (new MailtrapSendingClient($config))->webhooks($accountId); | |
| <?php | |
| use Mailtrap\Config; | |
| use Mailtrap\MailtrapSendingClient; | |
| require __DIR__ . '/../../vendor/autoload.php'; | |
| foreach (['MAILTRAP_ACCOUNT_ID', 'MAILTRAP_API_KEY'] as $envVar) { | |
| if (empty($_ENV[$envVar])) { | |
| throw new RuntimeException(sprintf('%s must be set for this example.', $envVar)); | |
| } | |
| } | |
| $accountId = $_ENV['MAILTRAP_ACCOUNT_ID']; | |
| $config = new Config($_ENV['MAILTRAP_API_KEY']); `#your` API token from here https://mailtrap.io/api-tokens | |
| $webhooks = (new MailtrapSendingClient($config))->webhooks($accountId); |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@examples/webhooks/all.php` around lines 12 - 14, The example currently reads
$_ENV['MAILTRAP_ACCOUNT_ID'] and $_ENV['MAILTRAP_API_KEY'] directly which can be
null and emit notices; add a small bootstrap guard before creating Config and
calling (new MailtrapSendingClient($config))->webhooks($accountId) that checks
required env vars (MAILTRAP_ACCOUNT_ID and MAILTRAP_API_KEY), optionally
MAILTRAP_DOMAIN_ID if domain-scoped webhooks are intended, and exits with a
clear error message when any are missing so Config is never constructed with
null values and the script fails fast and informatively.
| public function __construct( | ||
| private string $url, | ||
| private string $webhookType, | ||
| private array $eventTypes = [], | ||
| private ?string $payloadFormat = null, | ||
| private ?string $sendingStream = null, | ||
| private ?int $domainId = null, | ||
| private ?bool $active = null, | ||
| ) { | ||
| } | ||
|
|
||
| public function toArray(): array | ||
| { | ||
| $payload = [ | ||
| 'url' => $this->url, | ||
| 'webhook_type' => $this->webhookType, | ||
| 'event_types' => $this->eventTypes, |
There was a problem hiding this comment.
Validate type-specific webhook requirements.
eventTypes defaults to an empty array and sendingStream is nullable, so callers can build an email_sending webhook that serializes into an invalid request. Please enforce the required fields when webhookType needs them, or split the DTO by webhook kind.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/DTO/Request/Webhook/CreateWebhook.php` around lines 21 - 37,
CreateWebhook currently allows building invalid payloads for type-specific
webhooks; add validation in the CreateWebhook constructor (or in toArray) to
enforce required fields based on $webhookType (e.g., when $webhookType ===
'email_sending' require non-null $sendingStream and non-empty $eventTypes),
throw a clear exception (InvalidArgumentException) on violation, or
alternatively refactor by splitting CreateWebhook into separate DTOs per webhook
kind (e.g., EmailSendingWebhook with required $sendingStream and EventTypes) and
use the appropriate DTO where needed; update CreateWebhook::toArray to assume
validated fields so it cannot serialize invalid requests.
Motivation
Changes
Webhooks
API Tokens
Sub-Accounts
Sandbox Messages
Summary by CodeRabbit
Release Notes
New Features
Documentation