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

HP-1631: added into Customer entity state #73

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
96029e2
HP-1631: added into Customer entity state
VadymHrechukha Jul 30, 2024
0b6a488
HP-1631: Implemented state field in Customer model
VadymHrechukha Jul 30, 2024
302f89d
HP-1631: created CustomerState
VadymHrechukha Aug 1, 2024
d54f6d9
HP-1631: added all customer states into CustomerState
VadymHrechukha Aug 5, 2024
2b04c3a
HP-1631: fixed Customer
VadymHrechukha Aug 8, 2024
7c1611c
HP-1631: created ActionStateDeterminer class
VadymHrechukha Aug 11, 2024
a8886bc
HP-1631: created Unit test for ActionStateDeterminer class
VadymHrechukha Aug 11, 2024
ca0fe01
HP-1631: removed Analyzer classes because they don't need anymore
VadymHrechukha Aug 11, 2024
6a368f5
HP-1631: completed RestateActionsService class
VadymHrechukha Aug 11, 2024
ebdf697
HP-1631: renamed ActionState::isFinished() into ActionState::isNotAct…
VadymHrechukha Aug 12, 2024
9828d4c
HP-1631: created Behat test for check ActionStateDeterminer class
VadymHrechukha Aug 12, 2024
b34a233
HP-1631: added strict types to Factory because it is hard to understa…
VadymHrechukha Aug 14, 2024
1e0f18e
HP-1631: added annotation for Factory::get() method
VadymHrechukha Aug 14, 2024
41cacd7
HP-1631: added BuilderInterface::createSale() and BuilderInterface::b…
VadymHrechukha Aug 14, 2024
22c85c2
HP-1631: Renamed YiiActionRepository into ActionRepository class and …
VadymHrechukha Aug 19, 2024
37c07ba
HP-1631: modernized RestateActionsServiceTest Unit test
VadymHrechukha Aug 20, 2024
dfb7231
HP-1631: fixing RestateActionsServiceTest Unit test
VadymHrechukha Aug 20, 2024
c9a4c9b
HP-1631: fixed CustomerHydrator and created CustomerStateHydrator
VadymHrechukha Aug 20, 2024
2981ac3
HP-1631: added all possible states into CustomerState
VadymHrechukha Aug 20, 2024
183a7dc
HP-1631: fixed CustomerState::isDeleted() method
VadymHrechukha Aug 23, 2024
04a6a3c
HP-1631: ActionInterface must extend EntityInterface
VadymHrechukha Aug 24, 2024
7006e78
HP-1631: returned failed state to ActionState
VadymHrechukha Aug 24, 2024
db98bad
HP-1631: returned failed state to ActionState
VadymHrechukha Aug 24, 2024
dd9c18c
HP-1631: created CustomerStateException
VadymHrechukha Aug 24, 2024
a172483
HP-1631: tiny
VadymHrechukha Aug 24, 2024
18bd6e5
HP-1631: fixed cancelled action state name
VadymHrechukha Sep 18, 2024
2da650c
Merge branch 'master' into HP-1631_release_re-implemented_actions_charge
VadymHrechukha Oct 14, 2024
11e129f
HP-1631 Fixed "Class SimplePlanRepository contains 1 abstract method …
VadymHrechukha Oct 14, 2024
9b70aed
HP-1631 fixed ActionStateDeterminer Behat test
VadymHrechukha Oct 16, 2024
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
70 changes: 53 additions & 17 deletions src/action/ActionState.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,56 +17,92 @@
*/
class ActionState
{
const STATE_NEW = 'new';
const STATE_FINISHED = 'finished';
const STATE_FAILED = 'failed';
private const STATE_NEW = 'new';

/** @var string */
protected $state;
private const STATE_FINISHED = 'finished';

private function __construct(string $state = self::STATE_NEW)
private const STATE_PREMATURE = 'premature';

private const STATE_FUTURE = 'future';

private const STATE_CANCELED = 'canceled';

private const STATE_EXPIRED = 'expired';

private function __construct(protected string $state = self::STATE_NEW)
{
$this->state = $state;
}

public function getName()
public function getName(): string
{
return $this->state;
}

public function isNew()
public function isNew(): bool
{
return $this->state === self::STATE_NEW;
}

public function isFinished()
public function isFinished(): bool
{
return $this->state === self::STATE_FINISHED;
return !$this->isNew();
}

public static function new()
public static function new(): self
{
return new self(self::STATE_NEW);
}

public static function finished()
/**
* @deprecated use ActionState::expired()
* @return self
*/
public static function finished(): self
{
return new self(self::STATE_FINISHED);
}

public static function failed()
public static function premature(): self
{
return new self(self::STATE_PREMATURE);
}

public static function future(): self
{
return new self(self::STATE_FUTURE);
}

public static function canceled(): self
{
return new self(self::STATE_CANCELED);
}

public static function expired(): self
{
return new self(self::STATE_FAILED);
return new self(self::STATE_EXPIRED);
}

public static function fromString(string $name)
public static function fromString(string $name): self
{
foreach ([self::STATE_NEW, self::STATE_FINISHED, self::STATE_FAILED] as $state) {
$allowedStates = [
self::STATE_NEW,
self::STATE_FINISHED,
self::STATE_PREMATURE,
self::STATE_FUTURE,
self::STATE_CANCELED,
self::STATE_EXPIRED,
];
foreach ($allowedStates as $state) {
if ($state === $name) {
return new self($state);
}
}

throw new \Exception("wrong action state '$name'");
}

public function equals(ActionState $other): bool
{
return $this->state === $other->getName();
}
}
23 changes: 21 additions & 2 deletions src/customer/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,17 @@ class Customer implements CustomerInterface
*/
protected $sellers = [];

public function __construct($id, $login, CustomerInterface $seller = null)
/**
* @var string|null
*/
protected ?string $state = null;

public function __construct($id, $login, CustomerInterface $seller = null, ?string $state = null)
{
$this->id = $id;
$this->login = $login;
$this->seller = $seller;
$this->state = $state;
}

public function getId()
Expand Down Expand Up @@ -73,6 +79,19 @@ public function getSeller()
return $this->seller;
}

/**
* {@inheritdoc}
*/
public function getState(): ?string
{
return $this->state;
}

public function isDeleted(): bool
{
return CustomerState::isDeleted($this);
}

public static function fromArray(array $info)
{
if (!empty($info['seller_id']) && !empty($info['seller'])) {
Expand All @@ -81,7 +100,7 @@ public static function fromArray(array $info)
$seller = null;
}

return new static($info['id'], $info['login'], $seller);
return new static($info['id'], $info['login'], $seller, $info['state'] ?? null);
}

public function jsonSerialize(): array
Expand Down
8 changes: 8 additions & 0 deletions src/customer/CustomerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@ public function getLogin();
* @return static|null
*/
public function getSeller();

/**
* Get Customer state.
* @return null|string
*/
public function getState(): ?string;

public function isDeleted(): bool;
}
16 changes: 16 additions & 0 deletions src/customer/CustomerState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

namespace hiqdev\php\billing\customer;

enum CustomerState: string
{
case BLOCKED = 'blocked';
case DELETED = 'deleted';
case NEW = 'new';
case OK = 'ok';

public static function isDeleted(CustomerInterface $customer): bool
{
return self::tryFrom((string)$customer->getState()) === self::DELETED;
}
}
4 changes: 3 additions & 1 deletion src/sale/Sale.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function getCloseTime(): ?DateTimeImmutable
return $this->closeTime;
}

public function close(DateTimeImmutable $closeTime): void
public function close(DateTimeImmutable $closeTime): SaleInterface
{
if ($this->closeTime !== null) {
throw new InvariantException('Sale is already closed');
Expand All @@ -116,6 +116,8 @@ public function close(DateTimeImmutable $closeTime): void
}

$this->closeTime = $closeTime;

return $this;
}

public function setId($id)
Expand Down
2 changes: 1 addition & 1 deletion src/sale/SaleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function getData(): ?array;
* @throws InvariantException
* @throws ConstraintException
*/
public function close(DateTimeImmutable $time): void;
public function close(DateTimeImmutable $time): SaleInterface;

public function cancelClosing(): void;
}