Skip to content

Commit 2fe6939

Browse files
committed
Fix: fix phpstan errors
1 parent 266d60a commit 2fe6939

File tree

5 files changed

+61
-75
lines changed

5 files changed

+61
-75
lines changed

phpstan.neon.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ parameters:
77
- src
88
- config
99
- database
10+
configDirectories:
11+
- config
1012
tmpDir: build/phpstan
1113
checkOctaneCompatibility: true
1214
checkModelProperties: true

routes/inbox.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Redberry\MailboxForLaravel\Http\Controllers\SeenController;
99
use Redberry\MailboxForLaravel\Http\Controllers\SendTestMailController;
1010

11-
if (! config('inbox.enabled', true)) {
11+
if (!config('inbox.enabled', true)) {
1212
return;
1313
}
1414

@@ -32,5 +32,3 @@
3232
->name('asset');
3333
Route::post('/messages/{id}/seen', SeenController::class)->name('messages.seen');
3434
});
35-
36-
Route::get('/mailbox/assets/{path}', PublicAssetController::class)->where('path', '.*')->name('mailbox.asset');

src/Http/Controllers/AssetController.php

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/Http/Controllers/InboxController.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,20 @@
44

55
use Illuminate\Http\JsonResponse;
66
use Illuminate\Http\Request;
7-
use Illuminate\View\View;
7+
use Illuminate\Support\Facades\View;
88
use Redberry\MailboxForLaravel\CaptureService;
99

1010
class InboxController
1111
{
12-
public function __invoke(Request $request, CaptureService $service): View|JsonResponse
12+
public function __invoke(Request $request, CaptureService $service): \Illuminate\Contracts\View\View
1313
{
1414
$messages = $service->all(); // adapt to your API
1515

1616
$payload = [
1717
'messages' => array_values($messages['data'] ?? $messages), // support both shapes
1818
];
1919

20-
// If the client asks for JSON, return JSON (Vue will update without reload)
21-
if ($request->expectsJson()) {
22-
return response()->json($payload);
23-
}
24-
2520
// Otherwise, render the Blade view and hydrate initial props
26-
return view('inbox::index', ['data' => $payload]);
21+
return View::make('inbox::index', ['data' => $payload]);
2722
}
2823
}

src/Support/MessageNormalizer.php

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
use Symfony\Component\Mailer\Envelope;
99
use Symfony\Component\Mime\Address;
1010
use Symfony\Component\Mime\Email;
11+
use Symfony\Component\Mime\Header\Headers;
1112
use Symfony\Component\Mime\Part\DataPart;
13+
use Symfony\Component\Mime\RawMessage;
1214

1315
final class MessageNormalizer
1416
{
@@ -18,22 +20,62 @@ final class MessageNormalizer
1820
*
1921
* @return array<string,mixed>
2022
*/
23+
/** @return array<string,mixed> */
2124
public static function normalize(
22-
Email $email,
25+
Email|RawMessage $message,
2326
?Envelope $envelope = null,
2427
?string $raw = null,
2528
bool $storeAttachmentsInline = false
2629
): array {
27-
$keepRaw = true;
30+
if ($message instanceof Email) {
31+
32+
return self::normalizeEmail($message, $envelope, $raw, $storeAttachmentsInline);
33+
}
34+
// RawMessage (non-Email) fallback
35+
return self::normalizeRaw($message, $envelope, $raw);
36+
}
37+
38+
/** @return array<string,mixed> */
39+
private static function normalizeRaw(
40+
RawMessage $rawMessage,
41+
?Envelope $envelope,
42+
?string $raw
43+
): array {
44+
return [
45+
'version' => 1,
46+
'saved_at' => (new \DateTimeImmutable())->format(\DateTimeInterface::ATOM),
47+
'subject' => null,
48+
'from' => [],
49+
'to' => [],
50+
'cc' => [],
51+
'bcc' => [],
52+
'reply_to' => [],
53+
'text' => null,
54+
'html' => null,
55+
'headers' => [],
56+
'attachments' => [],
57+
'raw' => $rawMessage,
58+
];
59+
}
60+
61+
62+
/** @return array<string,mixed> */
63+
private static function normalizeEmail(
64+
Email $email,
65+
?Envelope $envelope,
66+
?string $raw,
67+
bool $storeAttachmentsInline
68+
): array {
2869
$headers = [];
2970
foreach ($email->getHeaders()->all() as $header) {
30-
// flatten to "Name" => ["value1", "value2"...]
3171
$headers[$header->getName()][] = trim($header->getBodyAsString());
3272
}
3373

3474
$attachments = [];
3575
foreach ($email->getAttachments() as $part) {
3676
/** @var DataPart $part */
77+
78+
$filename = $part->getFilename();
3779
$contentId = $part->getPreparedHeaders()->has('Content-ID')
3880
? trim($part->getPreparedHeaders()->get('Content-ID')->getBodyAsString(), '<>')
3981
: null;
@@ -42,7 +84,6 @@ public static function normalize(
4284
? $part->getPreparedHeaders()->get('Content-Disposition')->getBodyAsString()
4385
: null;
4486

45-
$filename = method_exists($part, 'getFilename') ? $part->getFilename() : null;
4687

4788
$contentType = $part->getPreparedHeaders()->has('Content-Type')
4889
? $part->getPreparedHeaders()->get('Content-Type')->getBodyAsString()
@@ -71,7 +112,7 @@ public static function normalize(
71112
'inline' => $contentId !== null,
72113
'size' => $size,
73114
'content' => $bodyBase64, // base64 or null
74-
], static fn ($v) => $v !== null);
115+
], static fn($v) => $v !== null);
75116
}
76117

77118
// Prefer explicitly set envelope sender/recipients, fallback to headers
@@ -102,30 +143,27 @@ public static function normalize(
102143

103144
'headers' => $headers, // full header map
104145
'attachments' => $attachments, // metadata (+ content if enabled)
105-
];
106146

107-
if ($keepRaw && $raw !== null) {
108-
$payload['raw'] = $raw;
109-
}
147+
'raw' => $raw, // raw email source if provided
148+
];
110149

111150
return $payload;
112151
}
113152

114153
/**
115154
* @param Address[] $addresses
155+
*
116156
* @return array<int,array{name?:string,email:string}>
117157
*/
118158
private static function addressesToArray(iterable $addresses): array
119159
{
120160
$out = [];
121161
foreach ($addresses as $addr) {
122-
if ($addr instanceof Address) {
123-
$row = ['email' => $addr->getAddress()];
124-
if ($addr->getName() !== '') {
125-
$row['name'] = $addr->getName();
126-
}
127-
$out[] = $row;
162+
$row = ['email' => $addr->getAddress()];
163+
if ($addr->getName() !== '') {
164+
$row['name'] = $addr->getName();
128165
}
166+
$out[] = $row;
129167
}
130168

131169
return $out;
@@ -139,10 +177,11 @@ private static function addressToArray(Address $address): array
139177
]);
140178
}
141179

180+
142181
private static function firstHeader(Email $email, string $name): ?string
143182
{
144183
$headers = $email->getHeaders();
145-
if (! $headers->has($name)) {
184+
if (!$headers->has($name)) {
146185
return null;
147186
}
148187

0 commit comments

Comments
 (0)