Skip to content

Commit 109d37e

Browse files
committed
Move decision logic to PostProcess::shouldProcess
- Create shouldProcesss to take a PathInfo and check: - Has a matching content_type - Has a body or filename - Simplify processIter to just looping over each response and checking shouldProcess, using rewriteFileContents if indicated and incrementing the appropriate counter.
1 parent a76381e commit 109d37e

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

src/PostProcessor.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,21 @@ public function __construct() {
1919
$this->config = new PostProcessConfig();
2020
}
2121

22-
public function processContentType( string $content_type ): bool {
22+
public function shouldProcess( PathInfo $path_info ): bool {
23+
$content_type = $path_info->content_type;
24+
25+
if ( ! $content_type ) {
26+
return false;
27+
}
28+
2329
if ( str_starts_with( $content_type, 'text/html' ) ||
2430
str_starts_with( $content_type, 'text/css' ) ||
2531
str_starts_with( $content_type, 'application/xml' ) ||
2632
str_starts_with( $content_type, 'text/plain' ) ||
2733
str_starts_with( $content_type, 'application/javascript' ) ) {
28-
return true;
34+
if ( $path_info->body || $path_info->filename ) {
35+
return true;
36+
}
2937
}
3038
return false;
3139
}
@@ -89,22 +97,15 @@ public function processIter(
8997
return $crawl_responses;
9098
}
9199

92-
$process = function ( $crawl_responses ) {
93-
foreach ( $crawl_responses as $crawled ) {
94-
$content_type = $crawled->content_type;
95-
if ( $content_type && $this->processContentType( $content_type ) ) {
96-
if ( $crawled->body || $crawled->filename ) {
97-
$crawled = $this->rewriteFileContents( $crawled );
98-
++$this->processed;
99-
}
100-
} else {
101-
++$this->skipped;
102-
}
103-
yield $crawled;
100+
foreach ( $crawl_responses as $path_info ) {
101+
if ( $this->shouldProcess( $path_info ) ) {
102+
++$this->processed;
103+
yield $this->rewriteFileContents( $path_info );
104+
} else {
105+
++$this->skipped;
106+
yield $path_info;
104107
}
105-
};
106-
107-
return $process( $crawl_responses );
108+
}
108109
}
109110

110111
/**

0 commit comments

Comments
 (0)