From 961fe396af613385ddf3a92cc563b9ece4fddabc Mon Sep 17 00:00:00 2001 From: Randall Wilk Date: Sun, 10 Mar 2024 17:08:54 -0500 Subject: [PATCH] Format code with new pint config --- config/printing.php | 2 + src/Api/PrintNode/Entity/Entity.php | 30 ++++++------- src/Api/PrintNode/Entity/PrintJob.php | 24 +++++------ .../PrintNode/Requests/PrintNodeRequest.php | 18 ++++---- src/Contracts/Driver.php | 2 + src/Contracts/PrintJob.php | 2 + src/Contracts/PrintTask.php | 2 + src/Contracts/Printer.php | 2 + src/Receipts/ReceiptPrinter.php | 42 +++++++++---------- tests/Concerns/FakesPrintNodeRequests.php | 2 + .../CustomDriver/Driver/CustomDriver.php | 40 +++++++++--------- .../Drivers/CustomDriver/Driver/PrintTask.php | 2 + tests/Pest.php | 2 + tests/TestCase.php | 2 + 14 files changed, 95 insertions(+), 77 deletions(-) diff --git a/config/printing.php b/config/printing.php index c3ba911..49a6fcb 100644 --- a/config/printing.php +++ b/config/printing.php @@ -1,5 +1,7 @@ mapResponse($data); } + public function toArray(): array + { + $publicProperties = (new ReflectionObject($this))->getProperties(ReflectionProperty::IS_PUBLIC); + + return collect($publicProperties) + ->mapWithKeys(function (ReflectionProperty $property) { + return [$property->name => $this->{$property->name}]; + })->toArray(); + } + + public function jsonSerialize(): mixed + { + return $this->toArray(); + } + protected function mapResponse(array $data): void { foreach ($data as $key => $value) { @@ -44,19 +59,4 @@ protected function getTimestamp($timestamp): ?Carbon return $date; } - - public function toArray(): array - { - $publicProperties = (new ReflectionObject($this))->getProperties(ReflectionProperty::IS_PUBLIC); - - return collect($publicProperties) - ->mapWithKeys(function (ReflectionProperty $property) { - return [$property->name => $this->{$property->name}]; - })->toArray(); - } - - public function jsonSerialize(): mixed - { - return $this->toArray(); - } } diff --git a/src/Api/PrintNode/Entity/PrintJob.php b/src/Api/PrintNode/Entity/PrintJob.php index c7eedd1..f0b99a0 100644 --- a/src/Api/PrintNode/Entity/PrintJob.php +++ b/src/Api/PrintNode/Entity/PrintJob.php @@ -10,6 +10,13 @@ class PrintJob extends Entity { + protected const VALID_CONTENT_TYPES = [ + ContentType::PDF_BASE64, + ContentType::RAW_BASE64, + ContentType::PDF_URI, + ContentType::RAW_URI, + ]; + /** * The print job's ID. */ @@ -60,13 +67,6 @@ class PrintJob extends Entity */ public ?Printer $printer = null; - protected const VALID_CONTENT_TYPES = [ - ContentType::PDF_BASE64, - ContentType::RAW_BASE64, - ContentType::PDF_URI, - ContentType::RAW_URI, - ]; - public function setPrinter(array $data): self { $this->printer = new Printer($data); @@ -180,15 +180,15 @@ public function setCreateTimestamp($date): self return $this; } - protected function isValidContentType(string $type): bool - { - return in_array($type, static::VALID_CONTENT_TYPES, true); - } - public function toArray(): array { return array_merge(parent::toArray(), [ 'createTimestamp' => $this->created, ]); } + + protected function isValidContentType(string $type): bool + { + return in_array($type, static::VALID_CONTENT_TYPES, true); + } } diff --git a/src/Api/PrintNode/Requests/PrintNodeRequest.php b/src/Api/PrintNode/Requests/PrintNodeRequest.php index 334d212..59bcfeb 100644 --- a/src/Api/PrintNode/Requests/PrintNodeRequest.php +++ b/src/Api/PrintNode/Requests/PrintNodeRequest.php @@ -31,14 +31,9 @@ public function __construct(protected string $apiKey) ])->acceptJson(); } - protected function endpoint(string $service): string - { - return $this->applyPaginationToUrl(static::BASE_URL . $service); - } - - protected function getRequest(string $service): array + public function postRequest(string $service, array $data = []) { - $response = $this->http->get($this->endpoint($service)); + $response = $this->http->post($this->endpoint($service), $data); if (! $response->successful()) { $this->handleFailedResponse($response); @@ -47,9 +42,14 @@ protected function getRequest(string $service): array return $response->json(); } - public function postRequest(string $service, array $data = []) + protected function endpoint(string $service): string { - $response = $this->http->post($this->endpoint($service), $data); + return $this->applyPaginationToUrl(static::BASE_URL . $service); + } + + protected function getRequest(string $service): array + { + $response = $this->http->get($this->endpoint($service)); if (! $response->successful()) { $this->handleFailedResponse($response); diff --git a/src/Contracts/Driver.php b/src/Contracts/Driver.php index 430703b..d496deb 100644 --- a/src/Contracts/Driver.php +++ b/src/Contracts/Driver.php @@ -1,5 +1,7 @@ close(); + } + + public function __toString(): string + { + return $this->connector->getData(); + } + + public function __call($name, $arguments) + { + if (method_exists($this->printer, $name)) { + $this->printer->{$name}(...$arguments); + + return $this; + } + + throw new InvalidArgumentException("Method [{$name}] not found on receipt printer object."); + } + public function centerAlign(): self { $this->printer->setJustification(Printer::JUSTIFY_CENTER); @@ -131,25 +152,4 @@ public function doubleLine(): self { return $this->text(str_repeat('=', static::$lineCharacterLength)); } - - public function __toString(): string - { - return $this->connector->getData(); - } - - public function __call($name, $arguments) - { - if (method_exists($this->printer, $name)) { - $this->printer->{$name}(...$arguments); - - return $this; - } - - throw new InvalidArgumentException("Method [{$name}] not found on receipt printer object."); - } - - public function __destruct() - { - $this->close(); - } } diff --git a/tests/Concerns/FakesPrintNodeRequests.php b/tests/Concerns/FakesPrintNodeRequests.php index 1a2cc10..6be6430 100644 --- a/tests/Concerns/FakesPrintNodeRequests.php +++ b/tests/Concerns/FakesPrintNodeRequests.php @@ -1,5 +1,7 @@ values(); } - protected function customPrinters(): array - { - return [ - [ - 'id' => 'printer_one', - 'name' => 'Printer One', - 'status' => 'online', - 'capabilities' => [], - 'description' => 'Printer one description', - ], - [ - 'id' => 'printer_two', - 'name' => 'Printer Two', - 'status' => 'offline', - 'capabilities' => [], - 'description' => 'Printer two description', - ], - ]; - } - public function printJobs(?int $limit = null, ?int $offset = null, ?string $dir = null): Collection { return collect(); @@ -79,4 +59,24 @@ public function printerPrintJob($printerId, $jobId): ?PrintJob { return null; } + + protected function customPrinters(): array + { + return [ + [ + 'id' => 'printer_one', + 'name' => 'Printer One', + 'status' => 'online', + 'capabilities' => [], + 'description' => 'Printer one description', + ], + [ + 'id' => 'printer_two', + 'name' => 'Printer Two', + 'status' => 'offline', + 'capabilities' => [], + 'description' => 'Printer two description', + ], + ]; + } } diff --git a/tests/Feature/Drivers/CustomDriver/Driver/PrintTask.php b/tests/Feature/Drivers/CustomDriver/Driver/PrintTask.php index d5eec31..0b3e896 100644 --- a/tests/Feature/Drivers/CustomDriver/Driver/PrintTask.php +++ b/tests/Feature/Drivers/CustomDriver/Driver/PrintTask.php @@ -1,5 +1,7 @@