Skip to content

Commit

Permalink
[!!!][TASK] Use Priority enum in Incident class
Browse files Browse the repository at this point in the history
Related: #2
  • Loading branch information
brotkrueml committed Dec 14, 2022
1 parent 17f32d7 commit f1f7725
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 87 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Changed
- Incident class accepts as a priority only a Priority enum

### Removed
- Compatibility with PHP < 8.1
- Compatibility with JobRouter® < 2022.1
Expand Down
21 changes: 4 additions & 17 deletions docs/api/incident.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@ Model\\Incident
Incident model class which collects the fields for a process instance start.

.. php:const:: const PRIORITY_HIGH = 3
The value of the high priority.

.. php:const:: const PRIORITY_LOW = 1
The value of the low priority.

.. php:const:: const PRIORITY_NORMAL = 2
The value of the default priority.

.. php:method:: addRowToSubTable($subTableName, $row)
Add a row to a sub table.
Expand Down Expand Up @@ -56,9 +44,9 @@ Model\\Incident

.. php:method:: getPriority()
Retrieve the priority, or :php:`null` if not defined.
Retrieve the priority, see :ref:`api-priority`.

:returns ?int: The priority.
:returns \\Brotkrueml\\JobRouterClient\\Enumerations\\Priority: The priority.

.. php:method:: getProcessTableField($name)
Expand Down Expand Up @@ -134,10 +122,9 @@ Model\\Incident

.. php:method:: setPriority($priority)
Sets the priority (values 1-3 are allowed, you can use the PRIORITY
constants defined in this class).
Sets the priority, see :ref:`api-priority`

:param int $priority: The priority.
:param \\Brotkrueml\\JobRouterClient\\Enumerations\\Priority $priority: The priority.
:returns self: An instance of itself.

.. php:method:: setProcessTableField($name, $value)
Expand Down
6 changes: 6 additions & 0 deletions docs/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ API
incident
file
filestorage

.. toctree::
:maxdepth: 1
:caption: Enumerations

priority
23 changes: 23 additions & 0 deletions docs/api/priority.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.. include:: /_includes.rst.txt

.. _api-priority:

======================
Enumerations\\Priority
======================

.. php:class:: enum Brotkrueml\JobRouterClient\Enumerations\Priority
Priority enumeration to be used in :ref:`Incident class <api-incident>`.

.. php:const:: case PRIORITY_HIGH = 3
The value of the high priority.

.. php:const:: case PRIORITY_LOW = 1
The value of the low priority.

.. php:const:: case PRIORITY_NORMAL = 2
The value of the default priority.
6 changes: 6 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ to `Semantic Versioning <https://semver.org/spec/v2.0.0.html>`_.
`Unreleased <https://github.com/brotkrueml/jobrouter-client/compare/v1.4.0...HEAD>`_
----------------------------------------------------------------------------------------

Changed
^^^^^^^


* Incident class accepts as a priority only a Priority enum

Removed
^^^^^^^

Expand Down
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ license <https://creativecommons.org/licenses/by-nc-sa/4.0/>`_.
installation
usage
api/index
upgrade
changelog

.. toctree::
Expand Down
26 changes: 26 additions & 0 deletions docs/upgrade.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
.. include:: /_includes.rst.txt

.. _upgrade:

=======
Upgrade
=======

From version 1.x to 2.0
=======================

With JobRouter Client 2.0 the minimum requirements have changed, supported are
now:

- PHP ≥ 8.1
- JobRouter® ≥ 2022.1

API changes
-----------

- :ref:`Incident <api-incident>` class:

- The :php:`->setPriority()` method accepts only a :ref:`Priority
<api-priority>` enum, previously it was an integer or null.
- The :php:`->getPriority()` method returns a :ref:`Priority
<api-priority>` enum, previously it was an integer or null.
5 changes: 3 additions & 2 deletions src/Client/IncidentsClientDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace Brotkrueml\JobRouterClient\Client;

use Brotkrueml\JobRouterClient\Enumerations\Priority;
use Brotkrueml\JobRouterClient\Exception\HttpException;
use Brotkrueml\JobRouterClient\Model\Incident;
use Brotkrueml\JobRouterClient\Resource\FileInterface;
Expand Down Expand Up @@ -66,8 +67,8 @@ private function buildMultipart(Incident $incident): array
$multipart['summary'] = $incident->getSummary();
}

if (\is_int($incident->getPriority())) {
$multipart['priority'] = (string)$incident->getPriority();
if ($incident->getPriority() !== Priority::Normal) {
$multipart['priority'] = (string)$incident->getPriority()->value;
}

if (\is_int($incident->getPool())) {
Expand Down
22 changes: 22 additions & 0 deletions src/Enumerations/Priority.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

/*
* This file is part of the JobRouter Client.
* https://github.com/brotkrueml/jobrouter-client
*
* Copyright (c) 2019-2022 Chris Müller
*
* For the full copyright and license information, please view the
* LICENSE.txt file that was distributed with this source code.
*/

namespace Brotkrueml\JobRouterClient\Enumerations;

enum Priority: int
{
case Low = 1;
case Normal = 2;
case High = 3;
}
28 changes: 4 additions & 24 deletions src/Model/Incident.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@

namespace Brotkrueml\JobRouterClient\Model;

use Brotkrueml\JobRouterClient\Enumerations\Priority;
use Brotkrueml\JobRouterClient\Resource\FileInterface;

final class Incident
{
public const PRIORITY_LOW = 1;
public const PRIORITY_NORMAL = 2;
public const PRIORITY_HIGH = 3;

/**
* @var positive-int|null
*/
Expand All @@ -30,10 +27,7 @@ final class Incident
private string $username = '';
private string $jobFunction = '';
private string $summary = '';
/**
* @var int<1,3>|null
*/
private ?int $priority = null;
private Priority $priority = Priority::Normal;
/**
* @var positive-int|null
*/
Expand Down Expand Up @@ -113,27 +107,13 @@ public function setSummary(string $summary): self
return $this;
}

public function getPriority(): ?int
public function getPriority(): Priority
{
return $this->priority;
}

/**
* @param int<1,3> $priority
* @throws \InvalidArgumentException
*/
public function setPriority(int $priority): self
public function setPriority(Priority $priority): self
{
if ($priority < self::PRIORITY_LOW || $priority > self::PRIORITY_HIGH) { // @phpstan-ignore-line
throw new \InvalidArgumentException(
\sprintf(
'proprity has to be an integer between 1 and 3, "%d" given',
$priority
),
1578225130
);
}

$this->priority = $priority;

return $this;
Expand Down
5 changes: 3 additions & 2 deletions tests/Unit/Client/IncidentsClientDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use Brotkrueml\JobRouterClient\Client\ClientInterface;
use Brotkrueml\JobRouterClient\Client\IncidentsClientDecorator;
use Brotkrueml\JobRouterClient\Enumerations\Priority;
use Brotkrueml\JobRouterClient\Model\Incident;
use Brotkrueml\JobRouterClient\Resource\FileInterface;
use PHPUnit\Framework\MockObject\MockObject;
Expand Down Expand Up @@ -111,9 +112,9 @@ public function dataProvider(): iterable
];

yield 'Given priority' => [
(new Incident())->setPriority(2),
(new Incident())->setPriority(Priority::High),
[
'priority' => '2',
'priority' => '3',
],
];

Expand Down
47 changes: 5 additions & 42 deletions tests/Unit/Model/IncidentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace Brotkrueml\JobRouterClient\Tests\Unit\Model;

use Brotkrueml\JobRouterClient\Enumerations\Priority;
use Brotkrueml\JobRouterClient\Model\Incident;
use Brotkrueml\JobRouterClient\Resource\FileInterface;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -70,9 +71,9 @@ public function summaryIsAnEmptyStringWhenNotSet(): void
/**
* @test
*/
public function priorityIsNullWhenNotSet(): void
public function priorityIsNormalWhenNotExplicitlySet(): void
{
self::assertNull($this->subject->getPriority());
self::assertSame(Priority::Normal, $this->subject->getPriority());
}

/**
Expand Down Expand Up @@ -183,48 +184,10 @@ public function setAndGetSummaryAreCorrectImplemented(): void
*/
public function setAndGetPriorityAreCorrectImplemented(): void
{
$actual = $this->subject->setPriority(2);
$actual = $this->subject->setPriority(Priority::High);

self::assertSame($this->subject, $actual);
self::assertSame(2, $this->subject->getPriority());
}

/**
* @test
*/
public function setPriorityAcceptsOnlyAllowedRange(): void
{
$exception = null;
try {
$this->subject->setPriority(1);
$this->subject->setPriority(2);
$this->subject->setPriority(3);
} catch (\InvalidArgumentException $exception) {
}

self::assertNull($exception, 'Unexpected InvalidArgumentException');
}

/**
* @test
*/
public function setPriorityThrowsExceptionWhenTooLow(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode(1578225130);

$this->subject->setPriority(0);
}

/**
* @test
*/
public function setPriorityThrowsExceptionWhenTooHigh(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode(1578225130);

$this->subject->setPriority(4);
self::assertSame(Priority::High, $this->subject->getPriority());
}

/**
Expand Down

0 comments on commit f1f7725

Please sign in to comment.