Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Update Pint Config #86

Merged
merged 2 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/printing.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

return [
/*
|--------------------------------------------------------------------------
Expand Down
32 changes: 31 additions & 1 deletion pint.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,36 @@
"types_spaces": {
"space": "none"
},
"single_trait_insert_per_statement": true
"combine_consecutive_issets": true,
"combine_consecutive_unsets": true,
"declare_parentheses": true,
"declare_strict_types": true,
"explicit_string_variable": true,
"single_trait_insert_per_statement": true,
"ordered_class_elements": {
"order": [
"use_trait",
"case",
"constant",
"constant_public",
"constant_protected",
"constant_private",
"property_public",
"property_protected",
"property_private",
"construct",
"destruct",
"magic",
"phpunit",
"method_abstract",
"method_public_static",
"method_public",
"method_protected_static",
"method_protected",
"method_private_static",
"method_private"
],
"sort_algorithm": "none"
}
}
}
30 changes: 15 additions & 15 deletions src/Api/PrintNode/Entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ public function __construct(array $data = [])
$this->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) {
Expand Down Expand Up @@ -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();
}
}
24 changes: 12 additions & 12 deletions src/Api/PrintNode/Entity/PrintJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
18 changes: 9 additions & 9 deletions src/Api/PrintNode/Requests/PrintNodeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/Driver.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Rawilk\Printing\Contracts;

use Illuminate\Support\Collection;
Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/PrintJob.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Rawilk\Printing\Contracts;

use Carbon\Carbon;
Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/PrintTask.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Rawilk\Printing\Contracts;

interface PrintTask
Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/Printer.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Rawilk\Printing\Contracts;

use Illuminate\Support\Collection;
Expand Down
42 changes: 21 additions & 21 deletions src/Receipts/ReceiptPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ public function __construct()
static::$lineCharacterLength = config('printing.receipts.line_character_length', 45);
}

public function __destruct()
{
$this->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);
Expand Down Expand Up @@ -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();
}
}
2 changes: 2 additions & 0 deletions tests/Concerns/FakesPrintNodeRequests.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Rawilk\Printing\Tests\Concerns;

use Illuminate\Support\Facades\Http;
Expand Down
40 changes: 20 additions & 20 deletions tests/Feature/Drivers/CustomDriver/Driver/CustomDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,6 @@ public function printers(?int $limit = null, ?int $offset = null, string|int|nul
->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();
Expand All @@ -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',
],
];
}
}
2 changes: 2 additions & 0 deletions tests/Feature/Drivers/CustomDriver/Driver/PrintTask.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Rawilk\Printing\Tests\Feature\Drivers\CustomDriver\Driver;

use Rawilk\Printing\Contracts\Printer;
Expand Down
2 changes: 2 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

use Rawilk\Printing\Tests\Feature\Api\PrintNode\PrintNodeTestCase;
use Rawilk\Printing\Tests\TestCase;

Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Rawilk\Printing\Tests;

use Dotenv\Dotenv;
Expand Down
Loading