| 1 |
<?php |
| 2 |
/** |
| 3 |
* Cloud-print provider capabilities value object. |
| 4 |
* |
| 5 |
* Single source of truth for per-provider knowledge: validity, polling, |
| 6 |
* content types, poll endpoints, server diagnostics and thermal wire formats. |
| 7 |
* |
| 8 |
* @package WCPOS\WooCommercePOS\Services |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace WCPOS\WooCommercePOS\Services; |
| 12 |
|
| 13 |
/** |
| 14 |
* Provider class. |
| 15 |
*/ |
| 16 |
class Provider { |
| 17 |
/** |
| 18 |
* Per-provider capability map. |
| 19 |
* |
| 20 |
* @var array<string, array<string, mixed>> |
| 21 |
*/ |
| 22 |
private const CAPABILITIES = array( |
| 23 |
// StarPRNT-native printers (the whole TSP100 line) cannot decode |
| 24 |
// ESC/POS; Star's docs advise against octet-stream for command data, |
| 25 |
// so jobs are emitted as native StarPRNT under the vnd.star type. |
| 26 |
'star-cloudprnt' => array( |
| 27 |
'polling' => true, |
| 28 |
'content_type' => 'application/vnd.star.starprnt', |
| 29 |
'poll_endpoint' => 'cloudprnt', |
| 30 |
'supports_server_diagnostic' => true, |
| 31 |
'thermal_wire_format' => 'starprnt', |
| 32 |
), |
| 33 |
'epson-sdp' => array( |
| 34 |
'polling' => true, |
| 35 |
'content_type' => 'application/xml', |
| 36 |
'poll_endpoint' => 'epson-sdp', |
| 37 |
'supports_server_diagnostic' => true, |
| 38 |
'thermal_wire_format' => 'epos-xml', |
| 39 |
), |
| 40 |
'printnode' => array( |
| 41 |
'polling' => false, |
| 42 |
'content_type' => 'application/pdf', |
| 43 |
'poll_endpoint' => null, |
| 44 |
'supports_server_diagnostic' => false, |
| 45 |
'thermal_wire_format' => null, |
| 46 |
), |
| 47 |
'star-online' => array( |
| 48 |
'polling' => false, |
| 49 |
'content_type' => 'text/vnd.star.markup', |
| 50 |
'poll_endpoint' => null, |
| 51 |
'supports_server_diagnostic' => false, |
| 52 |
'thermal_wire_format' => 'star-markup', |
| 53 |
), |
| 54 |
); |
| 55 |
|
| 56 |
/** |
| 57 |
* List of valid provider keys. |
| 58 |
* |
| 59 |
* @return array<int, string> |
| 60 |
*/ |
| 61 |
public static function valid(): array { |
| 62 |
return array_keys( self::CAPABILITIES ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Whether the provider polls the server for jobs. |
| 67 |
* |
| 68 |
* @param string $provider Provider key. |
| 69 |
* |
| 70 |
* @return bool |
| 71 |
*/ |
| 72 |
public static function is_polling( string $provider ): bool { |
| 73 |
return (bool) ( self::CAPABILITIES[ $provider ]['polling'] ?? false ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Whether the provider needs an out-of-band submit (we push jobs to it), |
| 78 |
* as opposed to a polling provider that fetches jobs itself. |
| 79 |
* |
| 80 |
* @param string $provider Provider key. |
| 81 |
* |
| 82 |
* @return bool |
| 83 |
*/ |
| 84 |
public static function requires_submit( string $provider ): bool { |
| 85 |
return \in_array( $provider, self::valid(), true ) && ! self::is_polling( $provider ); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* HTTP content type for the provider's job payloads. |
| 90 |
* |
| 91 |
* @param string $provider Provider key. |
| 92 |
* |
| 93 |
* @return string |
| 94 |
*/ |
| 95 |
public static function content_type( string $provider ): string { |
| 96 |
return (string) ( self::CAPABILITIES[ $provider ]['content_type'] ?? 'application/octet-stream' ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* REST poll-endpoint slug for the provider. |
| 101 |
* |
| 102 |
* @param string $provider Provider key. |
| 103 |
* |
| 104 |
* @return string|null |
| 105 |
*/ |
| 106 |
public static function poll_endpoint( string $provider ): ?string { |
| 107 |
return self::CAPABILITIES[ $provider ]['poll_endpoint'] ?? null; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Whether the provider supports a server-built diagnostic payload. |
| 112 |
* |
| 113 |
* @param string $provider Provider key. |
| 114 |
* |
| 115 |
* @return bool |
| 116 |
*/ |
| 117 |
public static function supports_server_diagnostic( string $provider ): bool { |
| 118 |
return (bool) ( self::CAPABILITIES[ $provider ]['supports_server_diagnostic'] ?? false ); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Thermal wire format for the given provider/engine pair. |
| 123 |
* |
| 124 |
* Only the 'thermal' engine on a direct printer yields a wire format; |
| 125 |
* any other engine, or an unknown provider, returns null. |
| 126 |
* |
| 127 |
* @param string $provider Provider key. |
| 128 |
* @param string $engine Render engine (e.g. 'thermal', 'logicless'). |
| 129 |
* |
| 130 |
* @return string|null |
| 131 |
*/ |
| 132 |
public static function wire_format( string $provider, string $engine ): ?string { |
| 133 |
if ( 'thermal' !== $engine ) { |
| 134 |
return null; |
| 135 |
} |
| 136 |
|
| 137 |
return self::CAPABILITIES[ $provider ]['thermal_wire_format'] ?? null; |
| 138 |
} |
| 139 |
} |
| 140 |
|