PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.18
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.18
1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 1.9.14 1.9.13 All 162 releases
woocommerce-pos / includes / Services / Print_Job_Lifecycle.php

Print_Job_Lifecycle.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.18, at includes/Services/Print_Job_Lifecycle.php

161 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Shared polling claim conversation.
4 *
5 * @package WCPOS\WooCommercePOS\Services
6 */
7
8 namespace WCPOS\WooCommercePOS\Services;
9
10 use WCPOS\WooCommercePOS\Interfaces\Poll_Provider_Adapter_Interface;
11 use WCPOS\WooCommercePOS\Logger;
12
13 /**
14 * Protocol adapters own bytes; this service owns the order of job transitions.
15 */
16 class Print_Job_Lifecycle {
17 /**
18 * Job store.
19 *
20 * @var Print_Job_Service
21 */
22 private $jobs;
23 /**
24 * Printer registry.
25 *
26 * @var Cloud_Print_Registry
27 */
28 private $registry;
29 /**
30 * Explicit adapter.
31 *
32 * @var Poll_Provider_Adapter_Interface|null
33 */
34 private $adapter;
35
36 /**
37 * Construct the polling conversation.
38 *
39 * @param Print_Job_Service|null $jobs Job store.
40 * @param Cloud_Print_Registry|null $registry Printer registry.
41 * @param Poll_Provider_Adapter_Interface|null $adapter Explicit protocol adapter.
42 */
43 public function __construct( ?Print_Job_Service $jobs = null, ?Cloud_Print_Registry $registry = null, ?Poll_Provider_Adapter_Interface $adapter = null ) {
44 $this->jobs = $jobs ?? new Print_Job_Service();
45 $this->registry = $registry ?? new Cloud_Print_Registry();
46 $this->adapter = $adapter;
47 }
48
49 /**
50 * Run the ordered job lifecycle.
51 *
52 * @param string $provider_key Protocol selected by the route.
53 * @param array $printer Registered printer.
54 * @param array $poll Parsed plain-data request.
55 * @return array{status:int, headers:array, body:string|array}
56 */
57 public function handle_poll( string $provider_key, array $printer, array $poll ): array {
58 $adapter = $this->adapter ?? Provider::poll_adapter( $provider_key );
59 $printer_id = $printer['id'];
60 $this->registry->record_seen( $printer_id );
61 $this->jobs->release_stale_claims( $printer_id );
62
63 if ( 'result' === $poll['phase'] ) {
64 $match = $adapter->match_result( $poll );
65 if ( null !== $match['job_id'] ) {
66 $job = $this->jobs->get( $match['job_id'] );
67 } else {
68 $job = $this->jobs->find_active_claim( $printer_id );
69 $unconfirmed = $this->jobs->find_unconfirmed( $printer_id );
70 if ( null !== $job && null !== $unconfirmed ) {
71 Logger::warning( sprintf( 'Printer "%s": result recorded against claimed job %d while unconfirmed job %d is still awaiting one.', $printer_id, (int) $job['id'], (int) $unconfirmed['id'] ) );
72 }
73 $job = $job ?? $unconfirmed;
74 }
75 if ( null !== $match['job_id'] && ( null === $job || $printer_id !== $job['printer_id'] ) ) {
76 return $adapter->advertise( $printer, $this->missing_job( $poll, $printer_id, $match['job_id'] ), null );
77 }
78 if ( null !== $job ) {
79 $result = $adapter->interpret_result( $poll );
80 $this->jobs->record_printer_result( (int) $job['id'], $result['ok'] );
81 if ( ! $result['ok'] ) {
82 $this->log_printer_failure( $poll, $printer_id, $result, (int) $job['id'] );
83 }
84 }
85 return $adapter->advertise( $printer, $poll, null );
86 }
87
88 if ( isset( $poll['answers'] ) ) {
89 $this->registry->record_capabilities( $printer_id, $poll['answers'], $poll['status_code'] );
90 }
91 $job = null;
92 if ( 'fetch' === $poll['phase'] && isset( $poll['token'] ) ) {
93 $job = $this->jobs->get( $poll['token'] );
94 if ( null === $job || $printer_id !== $job['printer_id'] ) {
95 return $adapter->advertise( $printer, $this->missing_job( $poll, $printer_id, $poll['token'] ), null );
96 }
97 } elseif ( empty( $poll['busy'] ) && null === $this->jobs->find_active_claim( $printer_id ) ) {
98 $job = $this->jobs->next_pending( $printer_id );
99 }
100 if ( null !== $job && ! empty( $poll['needs_capabilities'] ) ) {
101 $printer['encodings'] = $this->registry->get_capabilities( $printer_id )['encodings'];
102 }
103 if ( 'advertise' === $poll['phase'] ) {
104 $poll['request_capabilities'] = isset( $poll['answers'] ) && $this->registry->should_request_capabilities( $printer_id );
105 $response = $adapter->advertise( $printer, $poll, $job );
106 if ( $poll['request_capabilities'] ) {
107 $this->registry->record_capability_request( $printer_id );
108 }
109 return $response;
110 }
111 if ( null === $job ) {
112 return $adapter->advertise( $printer, $poll, null );
113 }
114 $negotiated = $adapter->negotiate( $printer, $poll, $job );
115 if ( isset( $negotiated['response'] ) ) {
116 return $negotiated['response'];
117 }
118 if ( ! $this->jobs->try_claim( (int) $job['id'] ) ) {
119 return $adapter->advertise( $printer, $poll, null );
120 }
121 $poll['media_type'] = $negotiated['media_type'];
122 $render = $this->jobs->render_job( $job, $poll['media_type'] );
123 if ( '' === $render['body'] ) {
124 Logger::error( sprintf( '%s: print job %d rendered an empty payload for printer "%s"; nothing was sent to the printer.', $poll['route'], (int) $job['id'], $printer_id ) );
125 update_post_meta( (int) $job['id'], Print_Job_Service::META_ERROR, 'empty_rendered_payload' );
126 $this->jobs->set_status( (int) $job['id'], Print_Job_Service::STATUS_FAILED );
127 }
128 return $adapter->deliver( $job, $render, $poll );
129 }
130
131 /**
132 * Log a missing job and pass a neutral intent to the adapter.
133 *
134 * @param array $poll Parsed request.
135 * @param string $printer_id Printer ID.
136 * @param int $job_id Missing or foreign job token.
137 * @return array Parsed request with a neutral not-found intent.
138 */
139 private function missing_job( array $poll, string $printer_id, int $job_id ): array {
140 Logger::warning( sprintf( '%s: print job "%d" was not found for printer "%s".', $poll['route'], $job_id, $printer_id ) );
141 $poll['intent'] = 'not_found';
142 return $poll;
143 }
144
145 /**
146 * Persist and log the printer failure.
147 *
148 * @param array $poll Parsed request.
149 * @param string $printer_id Printer ID.
150 * @param array $result Interpreted vendor result.
151 * @param int $job_id Job ID.
152 */
153 private function log_printer_failure( array $poll, string $printer_id, array $result, int $job_id ): void {
154 $reason = $result['code'] . $result['detail'];
155 if ( '' !== $reason ) {
156 update_post_meta( $job_id, Print_Job_Service::META_ERROR, sanitize_text_field( $reason ) );
157 }
158 Logger::error( sprintf( '%s: printer "%s" reported failure code "%s"%s for print job %d.', $poll['route'], $printer_id, $result['code'], $result['detail'], $job_id ) );
159 }
160 }
161