2020use Cake \Core \Plugin as CorePlugin ;
2121use Cake \Event \EventInterface ;
2222use Cake \Http \Exception \NotFoundException ;
23+ use Cake \Http \ServerRequest ;
2324use Cake \Routing \Router ;
2425use Cake \Utility \Inflector ;
2526use DebugKit \Mailer \AbstractResult ;
@@ -120,22 +121,22 @@ public function email(string $name, string $method): ?ResponseInterface
120121
121122 if ($ partType ) {
122123 $ result = $ this ->respondWithPart ($ email , $ partType );
123- if ($ restore ) {
124+ if ($ restore instanceof ServerRequest ) {
124125 Router::setRequest ($ restore );
125126 }
126127
127128 return $ result ;
128129 }
129130
130- $ humanName = Inflector::humanize (Inflector::underscore ($ name ) . " _ $ method" );
131+ $ humanName = Inflector::humanize (Inflector::underscore ($ name ) . ' _ ' . $ method );
131132 /** @var string $part */
132133 $ part = $ this ->request ->getQuery ('part ' );
133134 $ this ->set ('title ' , $ humanName );
134135 $ this ->set ('email ' , $ email );
135136 $ this ->set ('plugin ' , $ plugin );
136137 $ this ->set ('part ' , $ this ->findPreferredPart ($ email , $ part ));
137138
138- if ($ restore ) {
139+ if ($ restore instanceof ServerRequest ) {
139140 Router::setRequest ($ restore );
140141 }
141142
@@ -184,11 +185,11 @@ protected function getMailPreviews(): CollectionInterface
184185 protected function getMailPreviewClasses (): CollectionInterface
185186 {
186187 $ pluginPaths = collection (CorePlugin::loaded ())
187- ->reject (function ($ plugin ) {
188+ ->reject (function ($ plugin ): bool {
188189 return $ plugin === 'DebugKit ' ;
189190 })
190- ->map (function ($ plugin ) {
191- return [[CorePlugin::classPath ($ plugin ) . 'Mailer/Preview/ ' ], " $ plugin. " ];
191+ ->map (function (string $ plugin ): array {
192+ return [[CorePlugin::classPath ($ plugin ) . 'Mailer/Preview/ ' ], $ plugin . ' . ' ];
192193 });
193194
194195 $ appPaths = [App::classPath ('Mailer/Preview ' ), '' ];
@@ -201,7 +202,7 @@ protected function getMailPreviewClasses(): CollectionInterface
201202 yield $ plugin => $ path ;
202203 }
203204 })
204- ->unfold (function ($ path , $ plugin ) {
205+ ->unfold (function (string $ path , string $ plugin ) {
205206 /** @var list<string> $files */
206207 $ files = glob ($ path . '*Preview.php ' );
207208 foreach ($ files as $ file ) {
@@ -223,13 +224,7 @@ protected function getMailPreviewClasses(): CollectionInterface
223224 */
224225 protected function findPart (AbstractResult $ email , string $ partType ): ?string
225226 {
226- foreach ($ email ->getParts () as $ part => $ content ) {
227- if ($ part === $ partType ) {
228- return $ content ;
229- }
230- }
231-
232- return null ;
227+ return $ email ->getParts ()[$ partType ] ?? null ;
233228 }
234229
235230 /**
@@ -248,7 +243,7 @@ protected function findPreferredPart(AbstractResult $email, ?string $partType):
248243 }
249244
250245 if ($ partType === null ) {
251- foreach ($ email ->getParts () as $ part => $ content ) {
246+ foreach (array_keys ( $ email ->getParts ()) as $ part ) {
252247 return $ part ;
253248 }
254249 }
@@ -268,12 +263,12 @@ protected function findPreferredPart(AbstractResult $email, ?string $partType):
268263 protected function findPreview (string $ previewName , string $ emailName , string $ plugin = '' ): PreviewResult
269264 {
270265 if ($ plugin ) {
271- $ plugin = " $ plugin . " ;
266+ $ plugin .= ' . ' ;
272267 }
273268
274269 $ realClass = App::className ($ plugin . $ previewName , 'Mailer/Preview ' );
275270 if (!$ realClass ) {
276- throw new NotFoundException (" Mailer preview $ previewName not found " );
271+ throw new NotFoundException (sprintf ( ' Mailer preview %s not found ' , $ previewName ) );
277272 }
278273 /** @var \DebugKit\Mailer\MailPreview $mailPreview */
279274 $ mailPreview = new $ realClass ();
0 commit comments