| 1 |
<?php |
| 2 |
/** |
| 3 |
* Builds cloud-print test/diagnostic payloads per provider. |
| 4 |
* |
| 5 |
* @package WCPOS\WooCommercePOS\Services |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WCPOS\WooCommercePOS\Services; |
| 9 |
|
| 10 |
/** |
| 11 |
* Cloud_Print_Diagnostic class. |
| 12 |
*/ |
| 13 |
class Cloud_Print_Diagnostic { |
| 14 |
/** |
| 15 |
* Build a base64 diagnostic payload + content type for a provider. |
| 16 |
* |
| 17 |
* @param string $provider Provider key. |
| 18 |
* @param string $printer_name Display name. |
| 19 |
* |
| 20 |
* @return array{content_type:string, payload:string} |
| 21 |
* |
| 22 |
* @throws \RuntimeException When the provider has no server-side diagnostic. |
| 23 |
*/ |
| 24 |
public function build( string $provider, string $printer_name ): array { |
| 25 |
if ( ! Provider::supports_server_diagnostic( $provider ) ) { |
| 26 |
throw new \RuntimeException( esc_html( 'No server-side diagnostic for provider: ' . $provider ) ); |
| 27 |
} |
| 28 |
|
| 29 |
$adapter = Provider::adapter( $provider ); |
| 30 |
if ( null === $adapter ) { |
| 31 |
throw new \RuntimeException( esc_html( 'Unknown cloud-print provider: ' . $provider ) ); |
| 32 |
} |
| 33 |
|
| 34 |
return $adapter->diagnostic( $printer_name ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Build a PrintNode diagnostic receipt as PDF bytes. |
| 39 |
* |
| 40 |
* @param string $printer_name Display name. |
| 41 |
* |
| 42 |
* @throws \RuntimeException When the PrintNode adapter cannot be resolved. |
| 43 |
* |
| 44 |
* @return string PDF document bytes. |
| 45 |
*/ |
| 46 |
public function build_pdf( string $printer_name ): string { |
| 47 |
$adapter = Provider::adapter( 'printnode' ); |
| 48 |
if ( null === $adapter ) { |
| 49 |
throw new \RuntimeException( esc_html( 'Unknown cloud-print provider: printnode' ) ); |
| 50 |
} |
| 51 |
|
| 52 |
return base64_decode( $adapter->diagnostic( $printer_name )['payload'], true ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Build a Star Document Markup diagnostic receipt. |
| 57 |
* |
| 58 |
* @param string $printer_name Display name. |
| 59 |
* |
| 60 |
* @throws \RuntimeException When the Star Online adapter cannot be resolved. |
| 61 |
* |
| 62 |
* @return string Star Document Markup source. |
| 63 |
*/ |
| 64 |
public function star_markup( string $printer_name ): string { |
| 65 |
$adapter = Provider::adapter( 'star-online' ); |
| 66 |
if ( null === $adapter ) { |
| 67 |
throw new \RuntimeException( esc_html( 'Unknown cloud-print provider: star-online' ) ); |
| 68 |
} |
| 69 |
|
| 70 |
return base64_decode( $adapter->diagnostic( $printer_name )['payload'], true ); |
| 71 |
} |
| 72 |
} |
| 73 |
|