Skip to content
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
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,18 @@ composer require digitalrevolution/ipp

```php
$printJob = $ipp->print($printer, $ippFile)->getJobs()[0];
$updatedPrintJob = $ipp->getJobAttributes($printJob->getJobUri());
$updatedPrintJob = $ipp->getJobAttributes($printJob)->getJobs()[0];
```

### Cancel job
```php
$printJob = $ipp->print($printer, $ippFile)->getJobs()[0];
$ipp->cancelJob($printJob);
```

### Get all printers
```php
$ipp->printerAdministration()->getPrinters()->getPrinters();
```

### Register a printer with cups
Expand Down Expand Up @@ -69,8 +80,7 @@ composer require digitalrevolution/ipp
$printer->setHostname('my.printer');

$response = $ipp->getPrinterAttributes($printer);
$attributes = $response->getAttributes();
$printerName = $attributes["printer-name"]->getValue();
$printerName = $response->getAttribute("printer-name")?->getValue();
```

### Creating a custom IPP operation
Expand Down
25 changes: 9 additions & 16 deletions src/Client/CupsIppHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

class CupsIppHttpClient implements IppHttpClientInterface
{
private const int HTTP_STATUS_UNAUTHORIZED = 401;
private const int HTTP_STATUS_SERVER_ERROR = 500;

public function __construct(private readonly IppServer $server, private readonly ClientInterface $client)
Expand All @@ -28,28 +29,20 @@ public function __construct(private readonly IppServer $server, private readonly
public function sendRequest(IppOperation $operation): ResponseInterface
{
if ($operation->getOperation()->value >= IppOperationEnum::CupsGetDefault->value) {
$request = new Request(
'POST',
$this->server->getUri() . '/admin',
[
'Content-Type' => 'application/ipp',
'Authorization' => 'Basic ' . base64_encode($this->server->getUsername() . ":" . $this->server->getPassword()),
],
(string)$operation,
);
$url = $this->server->getUri() . '/admin';
} else {
$request = new Request(
'POST',
$this->server->getUri(),
['Content-Type' => 'application/ipp'],
(string)$operation,
);
$url = $this->server->getUri();
}

$headers = ['Content-Type' => 'application/ipp'];
if ($this->server->hasCredentials()) {
$headers['Authorization'] = 'Basic ' . base64_encode($this->server->getUsername() . ":" . $this->server->getPassword());
}
$request = new Request('POST', $url, $headers, (string)$operation);
$response = $this->client->sendRequest($request);

// a server error won't be an ipp response we can parse.
if ($response->getStatusCode() >= self::HTTP_STATUS_SERVER_ERROR) {
if ($response->getStatusCode() === self::HTTP_STATUS_UNAUTHORIZED || $response->getStatusCode() >= self::HTTP_STATUS_SERVER_ERROR) {
throw new IppRequestException($request, $response);
}

Expand Down
7 changes: 4 additions & 3 deletions src/Entity/IppJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

use DateTimeInterface;
use DR\Ipp\Enum\JobStateEnum;
use DR\Ipp\Enum\JobStateReasonEnum;

class IppJob
{
private int $id;
private string $uri;
private JobStateEnum $jobState;
private ?string $jobStateReason;
private JobStateReasonEnum $jobStateReason;
private ?string $userName;
private ?int $fileSize;
private ?int $numberOfDocuments;
Expand Down Expand Up @@ -45,12 +46,12 @@ public function setUri(string $uri): self
return $this;
}

public function getJobStateReason(): ?string
public function getJobStateReason(): JobStateReasonEnum
{
return $this->jobStateReason;
}

public function setJobStateReason(?string $jobStateReason): self
public function setJobStateReason(JobStateReasonEnum $jobStateReason): self
{
$this->jobStateReason = $jobStateReason;

Expand Down
42 changes: 42 additions & 0 deletions src/Entity/IppPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@

namespace DR\Ipp\Entity;

use DR\Ipp\Enum\PrinterStateEnum;
use DR\Ipp\Enum\PrinterStateReasonEnum;

class IppPrinter
{
private string $hostname;
private string $deviceUri;
private string $location;
private ?string $trayName = null;
private ?string $ppdName = null;
private ?PrinterStateEnum $printerState = null;
private ?PrinterStateReasonEnum $printerStateReason = null;
private ?string $printerType = null;

public function getHostname(): string
{
Expand Down Expand Up @@ -71,4 +77,40 @@ public function setPpdName(?string $ppdName): self

return $this;
}

public function getPrinterState(): ?PrinterStateEnum
{
return $this->printerState;
}

public function setPrinterState(?PrinterStateEnum $printerState): self
{
$this->printerState = $printerState;

return $this;
}

public function getPrinterStateReason(): ?PrinterStateReasonEnum
{
return $this->printerStateReason;
}

public function setPrinterStateReason(?PrinterStateReasonEnum $printerStateReason): self
{
$this->printerStateReason = $printerStateReason;

return $this;
}

public function getPrinterType(): ?string
{
return $this->printerType;
}

public function setPrinterType(?string $printerType): self
{
$this->printerType = $printerType;

return $this;
}
}
5 changes: 5 additions & 0 deletions src/Entity/IppServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ public function setPassword(string $password): self

return $this;
}

public function hasCredentials(): bool
{
return isset($this->username, $this->password);
}
}
18 changes: 16 additions & 2 deletions src/Entity/Response/CupsIppResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace DR\Ipp\Entity\Response;

use DR\Ipp\Entity\IppJob;
use DR\Ipp\Entity\IppPrinter;
use DR\Ipp\Enum\IppStatusCodeEnum;
use DR\Ipp\Protocol\IppAttribute;
use DR\Ipp\Protocol\IppStatusMessageService;
Expand All @@ -14,9 +15,14 @@ class CupsIppResponse implements IppResponseInterface
/**
* @param array<string, IppAttribute> $attributes
* @param IppJob[] $jobs
* @param IppPrinter[] $printers
*/
public function __construct(private readonly IppStatusCodeEnum $statusCode, private readonly array $attributes, private readonly array $jobs)
{
public function __construct(
private readonly IppStatusCodeEnum $statusCode,
private readonly array $attributes,
private readonly array $jobs,
private readonly array $printers,
) {
}

public function getStatusCode(): IppStatusCodeEnum
Expand All @@ -41,6 +47,14 @@ public function getJobs(): array
return $this->jobs;
}

/**
* @return IppPrinter[]
*/
public function getPrinters(): array
{
return $this->printers;
}

/**
* @inheritDoc
*/
Expand Down
6 changes: 6 additions & 0 deletions src/Entity/Response/IppResponseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace DR\Ipp\Entity\Response;

use DR\Ipp\Entity\IppJob;
use DR\Ipp\Entity\IppPrinter;
use DR\Ipp\Enum\IppStatusCodeEnum;
use DR\Ipp\Protocol\IppAttribute;

Expand All @@ -19,6 +20,11 @@ public function getStatusMessage(): ?string;
*/
public function getJobs(): array;

/**
* @return IppPrinter[]
*/
public function getPrinters(): array;

/**
* @return array<string, IppAttribute>
*/
Expand Down
47 changes: 47 additions & 0 deletions src/Enum/JobStateReasonEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace DR\Ipp\Enum;

enum JobStateReasonEnum: string
{
case None = 'none';
case JobIncoming = 'job-incoming';
case JobDataInsufficient = 'job-data-insufficient';
case JobOutgoing = 'job-outgoing';
case JobHoldUntilSpecified = 'job-hold-until-specified';
case JobInterpreting = 'job-interpreting';
case JobQueued = 'job-queued';
case JobFetchable = 'job-fetchable';
case JobSpooling = 'job-spooling';
case JobTransforming = 'job-transforming';
case JobQueuedForMarker = 'job-queued-for-marker';
case JobPrinting = 'job-printing';
case JobCanceledByUser = 'job-canceled-by-user';
case JobCanceledByOperator = 'job-canceled-by-operator';
case JobCanceledAtDevice = 'job-canceled-at-device';
case JobCompletedSuccessfully = 'job-completed-successfully';
case JobCompletedWithWarnings = 'job-completed-with-warnings';
case JobCompletedWithErrors = 'job-completed-with-errors';
case JobRestartable = 'job-restartable';
case PrinterStoppedPartly = 'printer-stopped-partly';
case PrinterStopped = 'printer-stopped';
case ResourcesAreNotReady = 'resources-are-not-ready';
case UnsupportedDocumentFormat = 'unsupported-document-format';
case DocumentAccessError = 'document-access-error';
case DocumentFormatError = 'document-format-error';
case DocumentPasswordError = 'document-password-error';
case DocumentPermissionError = 'document-permission-error';
case DocumentUnprintableError = 'document-unprintable-error';
case CompressionError = 'compression-error';
case UnsupportedCompression = 'unsupported-compression';
case ProcessingToStopPoint = 'processing-to-stop-point';
case SubmissionInterrupted = 'submission-interrupted';
case AbortedBySystem = 'aborted-by-system';
case ServiceOffLine = 'service-off-line';
case QueuedInDevice = 'queued-in-device';
case ErrorsDetected = 'errors-detected';
case WarningsDetected = 'warnings-detected';
case Unknown = 'unknown';
}
12 changes: 12 additions & 0 deletions src/Enum/PrinterStateEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace DR\Ipp\Enum;

enum PrinterStateEnum: int
{
case Idle = 3;
case Processing = 4;
case Stopped = 5;
}
Loading