| 1 |
<?php |
| 2 |
/** |
| 3 |
* Print format resolver. |
| 4 |
* |
| 5 |
* Single source of truth for the wire format a print job uses, given a |
| 6 |
* printer and a template. PrintNode chooses between PDF and raw ESC/POS based |
| 7 |
* on the printer's configured format; other providers delegate to Provider. |
| 8 |
* |
| 9 |
* @package WCPOS\WooCommercePOS\Services |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace WCPOS\WooCommercePOS\Services; |
| 13 |
|
| 14 |
/** |
| 15 |
* Print_Format_Resolver class. |
| 16 |
*/ |
| 17 |
class Print_Format_Resolver { |
| 18 |
/** |
| 19 |
* Resolve the wire format and HTTP content type for a print job. |
| 20 |
* |
| 21 |
* @param array $printer Printer configuration. |
| 22 |
* @param array $template Template configuration. |
| 23 |
* |
| 24 |
* @return array{kind:string, content_type:string} |
| 25 |
*/ |
| 26 |
public function resolve( array $printer, array $template ): array { |
| 27 |
// Printer rows saved before the provider field existed have none; they |
| 28 |
// must behave as the default provider, not fall through every branch. |
| 29 |
$provider = Provider::normalize( \is_string( $printer['provider'] ?? null ) ? $printer['provider'] : null ); |
| 30 |
$adapter = Provider::adapter( $provider ); |
| 31 |
if ( null === $adapter ) { |
| 32 |
return array( |
| 33 |
'kind' => '', |
| 34 |
'content_type' => '', |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
return $adapter->format( $printer, $template ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Resolve the HTTP content type for a printer when no template is in hand. |
| 43 |
* |
| 44 |
* Two callers have a printer but no template to resolve against. The |
| 45 |
* diagnostic builder always does — its payload is hand-built, not rendered |
| 46 |
* from a template — and always gets the provider's declared type. The |
| 47 |
* reprint path reaches it only when the source job's template can no longer |
| 48 |
* be rendered on the printer, and even then only when the job carries no |
| 49 |
* `pn_kind`; a job that has one keeps its stored content type instead, so |
| 50 |
* the two halves cannot drift apart. |
| 51 |
* |
| 52 |
* PrintNode therefore reports its PDF default here even for a printer in raw |
| 53 |
* mode, unlike resolve(), which sees the engine and can honour |
| 54 |
* `printnode_format`. Neither caller can act on the difference: the |
| 55 |
* diagnostic builder throws for PrintNode before it gets here, and the |
| 56 |
* reprint path's `pn_kind` condition above keeps a raw job away from this |
| 57 |
* answer. Prefer resolve() wherever a template is in hand — it answers both |
| 58 |
* halves of the pairing at once. |
| 59 |
* |
| 60 |
* @param array $printer Printer configuration. |
| 61 |
* |
| 62 |
* @return string |
| 63 |
*/ |
| 64 |
public function content_type_for_printer( array $printer ): string { |
| 65 |
$provider = Provider::normalize( \is_string( $printer['provider'] ?? null ) ? $printer['provider'] : null ); |
| 66 |
$adapter = Provider::adapter( $provider ); |
| 67 |
|
| 68 |
return null === $adapter ? 'application/octet-stream' : $adapter->content_type(); |
| 69 |
} |
| 70 |
} |
| 71 |
|