Skip to content

Commit

Permalink
Format code with new pint config
Browse files Browse the repository at this point in the history
  • Loading branch information
rawilk committed Mar 10, 2024
1 parent 3ca3591 commit 961fe39
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 77 deletions.
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
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

0 comments on commit 961fe39

Please sign in to comment.