From 96029e2cfd3f5717726c86c6f70978e1a64967ba Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Tue, 30 Jul 2024 15:21:58 +0300 Subject: [PATCH 01/28] HP-1631: added into Customer entity state --- src/customer/Customer.php | 21 ++++++++++++++++++++- src/customer/CustomerInterface.php | 8 ++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/customer/Customer.php b/src/customer/Customer.php index 4b02c668..3630e46d 100755 --- a/src/customer/Customer.php +++ b/src/customer/Customer.php @@ -37,11 +37,17 @@ class Customer implements CustomerInterface */ protected $sellers = []; - public function __construct($id, $login, CustomerInterface $seller = null) + /** + * @var string|null + */ + protected ?string $state; + + 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() @@ -73,6 +79,19 @@ public function getSeller() return $this->seller; } + /** + * {@inheritdoc} + */ + public function getState(): ?string + { + return $this->state; + } + + public function isDeleted(): bool + { + return $this->state === 'deleted'; + } + public static function fromArray(array $info) { if (!empty($info['seller_id']) && !empty($info['seller'])) { diff --git a/src/customer/CustomerInterface.php b/src/customer/CustomerInterface.php index d121d8c4..46b91c9f 100644 --- a/src/customer/CustomerInterface.php +++ b/src/customer/CustomerInterface.php @@ -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; } From 0b6a488c9c2d3226e762e0248c60b1d27459914e Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Tue, 30 Jul 2024 20:48:24 +0300 Subject: [PATCH 02/28] HP-1631: Implemented state field in Customer model --- src/customer/Customer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/customer/Customer.php b/src/customer/Customer.php index 3630e46d..c20c1f9b 100755 --- a/src/customer/Customer.php +++ b/src/customer/Customer.php @@ -100,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 From 302f89dbc5b177698863f54891a699e459b4623e Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Fri, 2 Aug 2024 01:21:23 +0300 Subject: [PATCH 03/28] HP-1631: created CustomerState --- src/customer/Customer.php | 2 +- src/customer/CustomerState.php | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 src/customer/CustomerState.php diff --git a/src/customer/Customer.php b/src/customer/Customer.php index c20c1f9b..7149252b 100755 --- a/src/customer/Customer.php +++ b/src/customer/Customer.php @@ -89,7 +89,7 @@ public function getState(): ?string public function isDeleted(): bool { - return $this->state === 'deleted'; + return CustomerState::isDeleted($this); } public static function fromArray(array $info) diff --git a/src/customer/CustomerState.php b/src/customer/CustomerState.php new file mode 100644 index 00000000..05cae12f --- /dev/null +++ b/src/customer/CustomerState.php @@ -0,0 +1,13 @@ +getState()) === self::DELETED; + } +} From d54f6d9c820607d863dabc8a7865bf75ac014f72 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Mon, 5 Aug 2024 17:52:21 +0300 Subject: [PATCH 04/28] HP-1631: added all customer states into CustomerState --- src/customer/CustomerState.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/customer/CustomerState.php b/src/customer/CustomerState.php index 05cae12f..a34f0d31 100644 --- a/src/customer/CustomerState.php +++ b/src/customer/CustomerState.php @@ -4,7 +4,10 @@ enum CustomerState: string { + case BLOCKED = 'blocked'; case DELETED = 'deleted'; + case NEW = 'new'; + case OK = 'ok'; public static function isDeleted(CustomerInterface $customer): bool { From 2b04c3ae9835f7e6b74f031d65a413bd72488a4f Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Thu, 8 Aug 2024 18:07:50 +0300 Subject: [PATCH 05/28] HP-1631: fixed Customer --- src/customer/Customer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/customer/Customer.php b/src/customer/Customer.php index 7149252b..e0522f9d 100755 --- a/src/customer/Customer.php +++ b/src/customer/Customer.php @@ -40,7 +40,7 @@ class Customer implements CustomerInterface /** * @var string|null */ - protected ?string $state; + protected ?string $state = null; public function __construct($id, $login, CustomerInterface $seller = null, ?string $state = null) { From 7c1611cd8ab6260b88ad22682328e9e275dec7d0 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Sun, 11 Aug 2024 21:13:10 +0300 Subject: [PATCH 06/28] HP-1631: created ActionStateDeterminer class --- src/action/ActionState.php | 65 ++++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 17 deletions(-) diff --git a/src/action/ActionState.php b/src/action/ActionState.php index 5dfbbb49..d3ca66b2 100644 --- a/src/action/ActionState.php +++ b/src/action/ActionState.php @@ -17,51 +17,82 @@ */ 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); } From a8886bcb78afb06a54a74b9a2646cfabfcfcff00 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Sun, 11 Aug 2024 23:04:44 +0300 Subject: [PATCH 07/28] HP-1631: created Unit test for ActionStateDeterminer class --- src/sale/Sale.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/sale/Sale.php b/src/sale/Sale.php index 538f0afb..02f0d9fc 100755 --- a/src/sale/Sale.php +++ b/src/sale/Sale.php @@ -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'); @@ -116,6 +116,8 @@ public function close(DateTimeImmutable $closeTime): void } $this->closeTime = $closeTime; + + return $this; } public function setId($id) From ca0fe0133f55d1c359cc6e4ce0923a262294bc49 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Sun, 11 Aug 2024 23:17:04 +0300 Subject: [PATCH 08/28] HP-1631: removed Analyzer classes because they don't need anymore --- src/sale/SaleInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sale/SaleInterface.php b/src/sale/SaleInterface.php index 843104db..c18782ca 100644 --- a/src/sale/SaleInterface.php +++ b/src/sale/SaleInterface.php @@ -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; } From 6a368f58618a66226c72e572f21ea03648be6315 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Mon, 12 Aug 2024 01:41:18 +0300 Subject: [PATCH 09/28] HP-1631: completed RestateActionsService class --- src/action/ActionState.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/action/ActionState.php b/src/action/ActionState.php index d3ca66b2..6b583a93 100644 --- a/src/action/ActionState.php +++ b/src/action/ActionState.php @@ -100,4 +100,9 @@ public static function fromString(string $name): self throw new \Exception("wrong action state '$name'"); } + + public function equals(ActionState $other): bool + { + return $this->state === $other->getName(); + } } From ebdf697c4f8480735779924f8e6d9f420f8c3c9f Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Mon, 12 Aug 2024 10:35:56 +0300 Subject: [PATCH 10/28] HP-1631: renamed ActionState::isFinished() into ActionState::isNotActive() and removed ActionState::expired() because it is the same as ActionState::finished() --- src/action/AbstractAction.php | 4 ++-- src/action/ActionInterface.php | 2 +- src/action/ActionState.php | 14 +------------- src/order/Calculator.php | 2 +- 4 files changed, 5 insertions(+), 17 deletions(-) diff --git a/src/action/AbstractAction.php b/src/action/AbstractAction.php index b184ea3c..601b3db6 100755 --- a/src/action/AbstractAction.php +++ b/src/action/AbstractAction.php @@ -169,9 +169,9 @@ public function setFinished(): void $this->state = ActionState::finished(); } - public function isFinished(): ?bool + public function isNotActive(): ?bool { - return $this->state === null ? null : $this->state->isFinished(); + return $this->state === null ? null : $this->state->isNotActive(); } /** diff --git a/src/action/ActionInterface.php b/src/action/ActionInterface.php index 5ad95bf9..a0924e67 100644 --- a/src/action/ActionInterface.php +++ b/src/action/ActionInterface.php @@ -70,7 +70,7 @@ public function getSale(): ?SaleInterface; /** * Returns null if the action state is not set. */ - public function isFinished(): ?bool; + public function isNotActive(): ?bool; public function getParent(): ?ActionInterface; diff --git a/src/action/ActionState.php b/src/action/ActionState.php index 6b583a93..15b93b39 100644 --- a/src/action/ActionState.php +++ b/src/action/ActionState.php @@ -27,8 +27,6 @@ class ActionState private const STATE_CANCELED = 'canceled'; - private const STATE_EXPIRED = 'expired'; - private function __construct(protected string $state = self::STATE_NEW) { } @@ -43,7 +41,7 @@ public function isNew(): bool return $this->state === self::STATE_NEW; } - public function isFinished(): bool + public function isNotActive(): bool { return !$this->isNew(); } @@ -53,10 +51,6 @@ public static function new(): self return new self(self::STATE_NEW); } - /** - * @deprecated use ActionState::expired() - * @return self - */ public static function finished(): self { return new self(self::STATE_FINISHED); @@ -77,11 +71,6 @@ public static function canceled(): self return new self(self::STATE_CANCELED); } - public static function expired(): self - { - return new self(self::STATE_EXPIRED); - } - public static function fromString(string $name): self { $allowedStates = [ @@ -90,7 +79,6 @@ public static function fromString(string $name): self self::STATE_PREMATURE, self::STATE_FUTURE, self::STATE_CANCELED, - self::STATE_EXPIRED, ]; foreach ($allowedStates as $state) { if ($state === $name) { diff --git a/src/order/Calculator.php b/src/order/Calculator.php index 64b59976..cd216838 100644 --- a/src/order/Calculator.php +++ b/src/order/Calculator.php @@ -106,7 +106,7 @@ public function calculatePrice(PriceInterface $price, ActionInterface $action): $charges = [$charge]; } - if ($action->isFinished()) { + if ($action->isNotActive()) { foreach ($charges as $charge) { $charge->setFinished(); } From 9828d4c8d4eeae0e883961c9ff69c479f33fe2e1 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Tue, 13 Aug 2024 01:06:17 +0300 Subject: [PATCH 11/28] HP-1631: created Behat test for check ActionStateDeterminer class --- src/customer/Customer.php | 23 ++++++++++++++++------- src/customer/CustomerInterface.php | 6 ++++-- src/customer/CustomerState.php | 7 ++++++- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/customer/Customer.php b/src/customer/Customer.php index e0522f9d..290afb6a 100755 --- a/src/customer/Customer.php +++ b/src/customer/Customer.php @@ -37,12 +37,9 @@ class Customer implements CustomerInterface */ protected $sellers = []; - /** - * @var string|null - */ - protected ?string $state = null; + protected ?CustomerState $state = null; - public function __construct($id, $login, CustomerInterface $seller = null, ?string $state = null) + public function __construct($id, $login, CustomerInterface $seller = null, ?CustomerState $state = null) { $this->id = $id; $this->login = $login; @@ -82,11 +79,18 @@ public function getSeller() /** * {@inheritdoc} */ - public function getState(): ?string + public function getState(): ?CustomerState { return $this->state; } + public function setState(CustomerState $state): self + { + $this->state = $state; + + return $this; + } + public function isDeleted(): bool { return CustomerState::isDeleted($this); @@ -100,7 +104,12 @@ public static function fromArray(array $info) $seller = null; } - return new static($info['id'], $info['login'], $seller, $info['state'] ?? null); + return new static( + $info['id'], + $info['login'], + $seller, + isset($info['state']) ? CustomerState::from($info['state']) : null, + ); } public function jsonSerialize(): array diff --git a/src/customer/CustomerInterface.php b/src/customer/CustomerInterface.php index 46b91c9f..11bc3805 100644 --- a/src/customer/CustomerInterface.php +++ b/src/customer/CustomerInterface.php @@ -38,9 +38,11 @@ public function getSeller(); /** * Get Customer state. - * @return null|string + * @return null|CustomerState */ - public function getState(): ?string; + public function getState(): ?CustomerState; + + public function setState(CustomerState $state): self; public function isDeleted(): bool; } diff --git a/src/customer/CustomerState.php b/src/customer/CustomerState.php index a34f0d31..d542662d 100644 --- a/src/customer/CustomerState.php +++ b/src/customer/CustomerState.php @@ -11,6 +11,11 @@ enum CustomerState: string public static function isDeleted(CustomerInterface $customer): bool { - return self::tryFrom((string)$customer->getState()) === self::DELETED; + return $customer->getState() === self::DELETED; + } + + public static function deleted(): CustomerState + { + return self::DELETED; } } From b34a233cd7ba5a1f34063b6e919626c749a15137 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Wed, 14 Aug 2024 10:50:56 +0300 Subject: [PATCH 12/28] HP-1631: added strict types to Factory because it is hard to understand with what entity you are working --- src/action/ActionFactory.php | 18 +++-- src/action/ActionFactoryInterface.php | 6 +- src/bill/BillFactory.php | 6 +- src/bill/BillFactoryInterface.php | 6 +- src/charge/ChargeFactory.php | 6 +- src/charge/ChargeFactoryInterface.php | 6 +- src/customer/CustomerFactory.php | 6 +- src/customer/CustomerFactoryInterface.php | 6 +- src/plan/PlanFactory.php | 8 +-- src/plan/PlanFactoryInterface.php | 6 +- src/price/PriceFactory.php | 3 - src/sale/SaleFactory.php | 6 +- src/sale/SaleFactoryInterface.php | 6 +- src/target/TargetFactory.php | 5 +- src/target/TargetFactoryInterface.php | 5 +- src/tools/Factory.php | 71 +++++++++++-------- src/type/TypeFactory.php | 5 +- src/type/TypeFactoryInterface.php | 5 +- tests/behat/bootstrap/BuilderInterface.php | 2 +- tests/behat/bootstrap/FactoryBasedBuilder.php | 8 ++- tests/support/tools/SimpleFactory.php | 2 +- 21 files changed, 77 insertions(+), 115 deletions(-) diff --git a/src/action/ActionFactory.php b/src/action/ActionFactory.php index 6162725f..0b6cf681 100644 --- a/src/action/ActionFactory.php +++ b/src/action/ActionFactory.php @@ -17,12 +17,18 @@ */ class ActionFactory implements ActionFactoryInterface { - /** - * Creates action object. - * @return Action - */ - public function create(ActionCreationDto $dto) + public function create(ActionCreationDto $dto): AbstractAction { - return new Action($dto->id, $dto->type, $dto->target, $dto->quantity, $dto->customer, $dto->time, $dto->sale, $dto->state, $dto->parent); + return new Action( + $dto->id, + $dto->type, + $dto->target, + $dto->quantity, + $dto->customer, + $dto->time, + $dto->sale, + $dto->state, + $dto->parent, + ); } } diff --git a/src/action/ActionFactoryInterface.php b/src/action/ActionFactoryInterface.php index 93650e4c..ab63d669 100644 --- a/src/action/ActionFactoryInterface.php +++ b/src/action/ActionFactoryInterface.php @@ -17,9 +17,5 @@ */ interface ActionFactoryInterface { - /** - * Creates action object. - * @return Action - */ - public function create(ActionCreationDto $dto); + public function create(ActionCreationDto $dto): AbstractAction; } diff --git a/src/bill/BillFactory.php b/src/bill/BillFactory.php index aa89afd3..725a44ce 100644 --- a/src/bill/BillFactory.php +++ b/src/bill/BillFactory.php @@ -17,11 +17,7 @@ */ class BillFactory implements BillFactoryInterface { - /** - * Creates bill object. - * @return Bill - */ - public function create(BillCreationDto $dto) + public function create(BillCreationDto $dto): BillInterface { return new Bill( $dto->id, diff --git a/src/bill/BillFactoryInterface.php b/src/bill/BillFactoryInterface.php index f5598ebb..7061081e 100644 --- a/src/bill/BillFactoryInterface.php +++ b/src/bill/BillFactoryInterface.php @@ -17,9 +17,5 @@ */ interface BillFactoryInterface { - /** - * Creates bill object. - * @return Bill - */ - public function create(BillCreationDto $dto); + public function create(BillCreationDto $dto): BillInterface; } diff --git a/src/charge/ChargeFactory.php b/src/charge/ChargeFactory.php index 70a9b9f8..1bb01148 100644 --- a/src/charge/ChargeFactory.php +++ b/src/charge/ChargeFactory.php @@ -17,11 +17,7 @@ */ class ChargeFactory implements ChargeFactoryInterface { - /** - * Creates charge object. - * @return Charge - */ - public function create(ChargeCreationDto $dto) + public function create(ChargeCreationDto $dto): ChargeInterface { return new Charge( $dto->id, diff --git a/src/charge/ChargeFactoryInterface.php b/src/charge/ChargeFactoryInterface.php index c9aef9e0..e49a75f5 100644 --- a/src/charge/ChargeFactoryInterface.php +++ b/src/charge/ChargeFactoryInterface.php @@ -17,9 +17,5 @@ */ interface ChargeFactoryInterface { - /** - * Creates charge object. - * @return Charge - */ - public function create(ChargeCreationDto $dto); + public function create(ChargeCreationDto $dto): ChargeInterface; } diff --git a/src/customer/CustomerFactory.php b/src/customer/CustomerFactory.php index 1333a530..d11df89f 100644 --- a/src/customer/CustomerFactory.php +++ b/src/customer/CustomerFactory.php @@ -17,11 +17,7 @@ */ class CustomerFactory implements CustomerFactoryInterface { - /** - * Creates customer object. - * @return Customer - */ - public function create(CustomerCreationDto $dto) + public function create(CustomerCreationDto $dto): CustomerInterface { return new Customer($dto->id, $dto->login, $dto->seller); } diff --git a/src/customer/CustomerFactoryInterface.php b/src/customer/CustomerFactoryInterface.php index da92a7c2..dba621bf 100644 --- a/src/customer/CustomerFactoryInterface.php +++ b/src/customer/CustomerFactoryInterface.php @@ -17,9 +17,5 @@ */ interface CustomerFactoryInterface { - /** - * Creates customer object. - * @return Customer - */ - public function create(CustomerCreationDto $dto); + public function create(CustomerCreationDto $dto): CustomerInterface; } diff --git a/src/plan/PlanFactory.php b/src/plan/PlanFactory.php index 3fd5dc89..3270790b 100644 --- a/src/plan/PlanFactory.php +++ b/src/plan/PlanFactory.php @@ -17,16 +17,12 @@ */ class PlanFactory implements PlanFactoryInterface { - /** - * Creates plan object. - * @return Plan - */ - public function create(PlanCreationDto $dto) + public function create(PlanCreationDto $dto): PlanInterface { return $this->createAnyPlan($dto); } - protected function createAnyPlan(PlanCreationDto $dto, string $class = null) + protected function createAnyPlan(PlanCreationDto $dto, string $class = null): PlanInterface { $class = $class ?? Plan::class; diff --git a/src/plan/PlanFactoryInterface.php b/src/plan/PlanFactoryInterface.php index 32e26ac1..4f697b9a 100644 --- a/src/plan/PlanFactoryInterface.php +++ b/src/plan/PlanFactoryInterface.php @@ -17,9 +17,5 @@ */ interface PlanFactoryInterface { - /** - * Creates plan object. - * @return Plan - */ - public function create(PlanCreationDto $dto); + public function create(PlanCreationDto $dto): PlanInterface; } diff --git a/src/price/PriceFactory.php b/src/price/PriceFactory.php index a771f685..55379688 100644 --- a/src/price/PriceFactory.php +++ b/src/price/PriceFactory.php @@ -42,9 +42,6 @@ public function __construct(array $types = [], $defaultClass = null) } - /** - * Creates price object. - */ public function create(PriceCreationDto $dto): PriceInterface { $class = $this->findClassForTypes([ diff --git a/src/sale/SaleFactory.php b/src/sale/SaleFactory.php index fcd725f2..8c368796 100644 --- a/src/sale/SaleFactory.php +++ b/src/sale/SaleFactory.php @@ -17,11 +17,7 @@ */ class SaleFactory implements SaleFactoryInterface { - /** - * Creates sale object. - * @return Sale - */ - public function create(SaleCreationDto $dto) + public function create(SaleCreationDto $dto): SaleInterface { $sale = new Sale($dto->id, $dto->target, $dto->customer, $dto->plan, $dto->time, $dto->data); diff --git a/src/sale/SaleFactoryInterface.php b/src/sale/SaleFactoryInterface.php index 25c777ed..8b357c39 100644 --- a/src/sale/SaleFactoryInterface.php +++ b/src/sale/SaleFactoryInterface.php @@ -17,9 +17,5 @@ */ interface SaleFactoryInterface { - /** - * Creates sale object. - * @return Sale - */ - public function create(SaleCreationDto $dto); + public function create(SaleCreationDto $dto): SaleInterface; } diff --git a/src/target/TargetFactory.php b/src/target/TargetFactory.php index 652ab7b8..906c989f 100644 --- a/src/target/TargetFactory.php +++ b/src/target/TargetFactory.php @@ -17,10 +17,7 @@ */ class TargetFactory implements TargetFactoryInterface { - /** - * @return Target - */ - public function create(TargetCreationDto $dto) + public function create(TargetCreationDto $dto): TargetInterface { return new Target($dto->id, $dto->type, $dto->name); } diff --git a/src/target/TargetFactoryInterface.php b/src/target/TargetFactoryInterface.php index 5bb16169..1e2744e8 100644 --- a/src/target/TargetFactoryInterface.php +++ b/src/target/TargetFactoryInterface.php @@ -17,10 +17,7 @@ */ interface TargetFactoryInterface { - /** - * @return Target - */ - public function create(TargetCreationDto $dto); + public function create(TargetCreationDto $dto): TargetInterface; /** * Returns class that represents target with $type. diff --git a/src/tools/Factory.php b/src/tools/Factory.php index 95be85ad..dc604bb3 100644 --- a/src/tools/Factory.php +++ b/src/tools/Factory.php @@ -11,10 +11,21 @@ namespace hiqdev\php\billing\tools; use DateTimeImmutable; +use hiqdev\php\billing\action\ActionInterface; +use hiqdev\php\billing\bill\BillInterface; +use hiqdev\php\billing\charge\ChargeInterface; +use hiqdev\php\billing\customer\CustomerInterface; use hiqdev\php\billing\Exception\UnknownEntityException; +use hiqdev\php\billing\plan\PlanInterface; +use hiqdev\php\billing\price\PriceInterface; +use hiqdev\php\billing\sale\SaleInterface; use hiqdev\php\billing\target\TargetCollection; +use hiqdev\php\billing\target\TargetInterface; +use hiqdev\php\billing\type\TypeInterface; use hiqdev\php\units\Quantity; +use hiqdev\php\units\QuantityInterface; use hiqdev\php\units\Unit; +use hiqdev\php\units\UnitInterface; use Money\Currency; use Money\Money; @@ -25,21 +36,21 @@ */ class Factory implements FactoryInterface { - private $entities = []; + private array $entities = []; - private $factories = []; + private array $factories; public function __construct(array $factories) { $this->factories = $factories; } - public function getMoney($data) + public function getMoney($data): Money { return $this->get('money', $data); } - public function getSums($data) + public function getSums($data): array { $res = []; foreach ($data as $key => $value) { @@ -49,7 +60,7 @@ public function getSums($data) return $res; } - public function parseMoney($str) + public function parseMoney($str): array { [$amount, $currency] = explode(' ', $str); @@ -59,22 +70,22 @@ public function parseMoney($str) ]; } - public function createMoney($data) + public function createMoney($data): Money { return new Money($data['amount'], new Currency(strtoupper($data['currency']))); } - public function getCurrency($data) + public function getCurrency($data): Currency { return new Currency($data); } - public function getQuantity($data) + public function getQuantity($data): QuantityInterface { return $this->get('quantity', $data); } - public function parseQuantity($str) + public function parseQuantity(string$str): array { [$quantity, $unit] = explode(' ', $str); @@ -84,32 +95,32 @@ public function parseQuantity($str) ]; } - public function createQuantity($data) + public function createQuantity($data): QuantityInterface { return Quantity::create($data['unit'], $data['quantity']); } - public function getUnit($data) + public function getUnit($data): UnitInterface { return $this->get('unit', $data); } - public function createUnit($data) + public function createUnit($data): UnitInterface { return Unit::create($data['name']); } - public function getType($data) + public function getType($data): TypeInterface { return $this->get('type', $data); } - public function getTime($data) + public function getTime($data): DateTimeImmutable { return $this->get('time', $data); } - public function createTime($data) + public function createTime($data): DateTimeImmutable { if (empty($data['date'])) { return new DateTimeImmutable(); @@ -123,12 +134,12 @@ public function createTime($data) return new DateTimeImmutable($str); } - public function getTargets($data) + public function getTargets($data): TargetCollection { return $this->get('targets', $data); } - public function createTargets($data) + public function createTargets(array $data): TargetCollection { $targets = []; foreach ($data as $one) { @@ -138,17 +149,21 @@ public function createTargets($data) return new TargetCollection($targets); } - public function getTarget($data) + public function getTarget($data): TargetInterface { return $this->get('target', $data); } - public function getAction($data) + public function getAction($data): ActionInterface { return $this->get('action', $data); } - public function getCharges($data) + /** + * @param array $data + * @return ChargeInterface[] + */ + public function getCharges(array $data): array { $res = []; foreach ($data as $key => $row) { @@ -158,32 +173,32 @@ public function getCharges($data) return $res; } - public function getCharge($data) + public function getCharge($data): ChargeInterface { return $this->get('charge', $data); } - public function getPrice($data) + public function getPrice($data): PriceInterface { return $this->get('price', $data); } - public function getBill($data) + public function getBill($data): BillInterface { return $this->get('bill', $data); } - public function getPlan($data) + public function getPlan($data): PlanInterface { return $this->get('plan', $data); } - public function getSale($data) + public function getSale($data): SaleInterface { return $this->get('sale', $data); } - public function getCustomer($data) + public function getCustomer($data): CustomerInterface { return $this->get('customer', $data); } @@ -270,9 +285,7 @@ public function create(string $entity, $data) throw new FactoryNotFoundException($entity); } - $factory = $this->factories[$entity]; - - return $factory->create($this->createDto($entity, $data)); + return $this->factories[$entity]->create($this->createDto($entity, $data)); } public function createDto(string $entity, array $data) diff --git a/src/type/TypeFactory.php b/src/type/TypeFactory.php index 5184356e..d3ae7470 100644 --- a/src/type/TypeFactory.php +++ b/src/type/TypeFactory.php @@ -15,10 +15,7 @@ */ class TypeFactory implements TypeFactoryInterface { - /** - * @return Type - */ - public function create(TypeCreationDto $dto) + public function create(TypeCreationDto $dto): TypeInterface { return new Type($dto->id, $dto->name); } diff --git a/src/type/TypeFactoryInterface.php b/src/type/TypeFactoryInterface.php index 55710965..2c91a6da 100644 --- a/src/type/TypeFactoryInterface.php +++ b/src/type/TypeFactoryInterface.php @@ -17,8 +17,5 @@ */ interface TypeFactoryInterface { - /** - * @return Type - */ - public function create(TypeCreationDto $dto); + public function create(TypeCreationDto $dto): TypeInterface; } diff --git a/tests/behat/bootstrap/BuilderInterface.php b/tests/behat/bootstrap/BuilderInterface.php index 0c97de6c..cbaae794 100644 --- a/tests/behat/bootstrap/BuilderInterface.php +++ b/tests/behat/bootstrap/BuilderInterface.php @@ -28,7 +28,7 @@ public function buildTarget(string $name); public function recreatePlan(string $name); - public function buildSale(string $target, string $plan, string $time); + public function buildSale(string $target, string $plan, string $time, ?string $closeTime = null); public function buildPurchase(string $target, string $plan, string $time, ?array $uses = []); diff --git a/tests/behat/bootstrap/FactoryBasedBuilder.php b/tests/behat/bootstrap/FactoryBasedBuilder.php index f9fa76be..37d7f667 100644 --- a/tests/behat/bootstrap/FactoryBasedBuilder.php +++ b/tests/behat/bootstrap/FactoryBasedBuilder.php @@ -13,6 +13,7 @@ use hiqdev\billing\hiapi\plan\PlanFactory; use hiqdev\billing\hiapi\tests\support\order\SimpleCalculator; +use hiqdev\php\billing\action\ActionInterface; use hiqdev\php\billing\price\EnumPrice; use hiqdev\php\billing\price\PriceFactory; use hiqdev\php\billing\price\RatePrice; @@ -121,14 +122,15 @@ public function recreatePlan(string $name) $plan->setPrices($this->prices); } - public function buildSale(string $target, $plan, string $time = null) + public function buildSale(string $target, $plan, string $time = null, ?string $closeTime = null) { $this->time = $time; - $this->sale = $this->factory->get('sale', array_filter([ + $this->sale = $this->factory->getSale(array_filter([ 'customer' => $this->customer, 'target' => $target, 'plan' => $plan, 'time' => $time, + 'closeTime' => $closeTime, ])); return $this->sale; @@ -191,7 +193,7 @@ public function performAction(array $data) $this->getBilling()->perform($action); } - public function buildAction(array $data) + public function buildAction(array $data): ActionInterface { $data['time'] = $data['time'] ?? $this->time; $data['customer'] = $data['customer'] ?? $this->customer; diff --git a/tests/support/tools/SimpleFactory.php b/tests/support/tools/SimpleFactory.php index 19f63ef6..d3b09dcc 100644 --- a/tests/support/tools/SimpleFactory.php +++ b/tests/support/tools/SimpleFactory.php @@ -26,7 +26,7 @@ class SimpleFactory extends Factory { public function __construct(array $factories = []) { - return parent::__construct(array_merge(self::simpleFactories(), $factories)); + parent::__construct(array_merge(self::simpleFactories(), $factories)); } public static function simpleFactories(): array From 1e0f18e47604e187e1bd9fad99a23f2ff2c39c80 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Wed, 14 Aug 2024 11:37:09 +0300 Subject: [PATCH 13/28] HP-1631: added annotation for Factory::get() method --- src/tools/Factory.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/tools/Factory.php b/src/tools/Factory.php index dc604bb3..55b39c9b 100644 --- a/src/tools/Factory.php +++ b/src/tools/Factory.php @@ -203,6 +203,13 @@ public function getCustomer($data): CustomerInterface return $this->get('customer', $data); } + /** + * @deprecated In the future the function will be private. + * Please use a specific method instead (e.g., getSale) + * @param string $entity + * @param $data + * @return mixed + */ public function get(string $entity, $data) { if (is_scalar($data)) { From 41cacd7fa998ddc67374e8a126639385cd0e071c Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Thu, 15 Aug 2024 00:36:47 +0300 Subject: [PATCH 14/28] HP-1631: added BuilderInterface::createSale() and BuilderInterface::buildAction() methods --- tests/behat/bootstrap/BuilderInterface.php | 6 +++++- tests/behat/bootstrap/FactoryBasedBuilder.php | 19 +++++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/tests/behat/bootstrap/BuilderInterface.php b/tests/behat/bootstrap/BuilderInterface.php index cbaae794..ea9ff9fa 100644 --- a/tests/behat/bootstrap/BuilderInterface.php +++ b/tests/behat/bootstrap/BuilderInterface.php @@ -28,7 +28,9 @@ public function buildTarget(string $name); public function recreatePlan(string $name); - public function buildSale(string $target, string $plan, string $time, ?string $closeTime = null); + public function buildSale(string $target, string $planName, string $time); + + public function createSale(string $target, string $planName, string $time, ?string $closeTime = null); public function buildPurchase(string $target, string $plan, string $time, ?array $uses = []); @@ -38,6 +40,8 @@ public function performBilling(string $time): void; public function setAction(string $type, int $amount, string $unit, string $target, string $time): void; + public function buildAction(array $data); + public function performCalculation(string $time): array; public function targetChangePlan(string $target, string $planName, string $date, string $wallTime = null); diff --git a/tests/behat/bootstrap/FactoryBasedBuilder.php b/tests/behat/bootstrap/FactoryBasedBuilder.php index 37d7f667..be49492e 100644 --- a/tests/behat/bootstrap/FactoryBasedBuilder.php +++ b/tests/behat/bootstrap/FactoryBasedBuilder.php @@ -18,6 +18,7 @@ use hiqdev\php\billing\price\PriceFactory; use hiqdev\php\billing\price\RatePrice; use hiqdev\php\billing\price\SinglePrice; +use hiqdev\php\billing\sale\SaleInterface; use hiqdev\php\billing\target\Target; use hiqdev\php\billing\tests\support\order\SimpleBilling; use hiqdev\php\billing\tests\support\tools\SimpleFactory; @@ -33,11 +34,12 @@ class FactoryBasedBuilder implements BuilderInterface private $plan; - private $sale; + private SaleInterface $sale; private $prices = []; - private $actions = []; + /** @var ActionInterface[] */ + private array $actions = []; private $factory; @@ -122,13 +124,18 @@ public function recreatePlan(string $name) $plan->setPrices($this->prices); } - public function buildSale(string $target, $plan, string $time = null, ?string $closeTime = null) + public function buildSale(string $target, $planName, string $time = null, ?string $closeTime = null) + { + return $this->createSale($target, $planName, $time, $closeTime); + } + + public function createSale(string $target, $planName, string $time = null, ?string $closeTime = null) { $this->time = $time; $this->sale = $this->factory->getSale(array_filter([ 'customer' => $this->customer, 'target' => $target, - 'plan' => $plan, + 'plan' => $planName, 'time' => $time, 'closeTime' => $closeTime, ])); @@ -198,10 +205,10 @@ public function buildAction(array $data): ActionInterface $data['time'] = $data['time'] ?? $this->time; $data['customer'] = $data['customer'] ?? $this->customer; if (!empty($data['targets'])) { - $data['target'] = $this->factory->get('targets', $data['targets']); + $data['target'] = $this->factory->getTargets($data['targets']); } - return $this->factory->get('action', $data); + return $this->factory->getAction($data); } public function findBills(array $data): array From 22c85c23ecfc2922eeb642b56464c87704da16d9 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Mon, 19 Aug 2024 08:04:31 +0300 Subject: [PATCH 15/28] HP-1631: Renamed YiiActionRepository into ActionRepository class and added $closeTime into signature of ApiBasedBuilder::buildSale() method --- tests/behat/bootstrap/BillingContext.php | 15 ++++++++++++++- tests/behat/bootstrap/BuilderInterface.php | 2 +- tests/behat/bootstrap/FactoryBasedBuilder.php | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/tests/behat/bootstrap/BillingContext.php b/tests/behat/bootstrap/BillingContext.php index 38711a9a..d8793c73 100644 --- a/tests/behat/bootstrap/BillingContext.php +++ b/tests/behat/bootstrap/BillingContext.php @@ -27,6 +27,8 @@ class BillingContext extends BaseContext protected $saleTime; + protected $saleCloseTime; + protected $bill; protected $charges = []; @@ -164,8 +166,19 @@ public function recreatePlan($plan) public function sale($target, $plan, $time): void { $this->saleTime = $this->prepareTime($time); - $this->builder->buildSale($target, $plan, $this->saleTime); + $this->sale = $this->builder->buildSale($target, $plan, $this->saleTime); + } + + /** + * @Given /sale target (\S+) by plan (\S+) at (\S+) and close at (\S+)/ + */ + public function saleWithCloseTime($target, $plan, $time, $closeTime): void + { + $this->saleTime = $this->prepareTime($time); + $this->saleCloseTime = $this->prepareTime($closeTime); + $this->builder->buildSale($target, $plan, $this->saleTime, $this->saleCloseTime); } + /** * @When /^sale close is requested for target "([^"]*)" at "([^"]*)", assuming current time is "([^"]*)"$/ */ diff --git a/tests/behat/bootstrap/BuilderInterface.php b/tests/behat/bootstrap/BuilderInterface.php index ea9ff9fa..a9ecd334 100644 --- a/tests/behat/bootstrap/BuilderInterface.php +++ b/tests/behat/bootstrap/BuilderInterface.php @@ -28,7 +28,7 @@ public function buildTarget(string $name); public function recreatePlan(string $name); - public function buildSale(string $target, string $planName, string $time); + public function buildSale(string $target, string $planName, string $time, ?string $closeTime = null); public function createSale(string $target, string $planName, string $time, ?string $closeTime = null); diff --git a/tests/behat/bootstrap/FactoryBasedBuilder.php b/tests/behat/bootstrap/FactoryBasedBuilder.php index be49492e..923bac82 100644 --- a/tests/behat/bootstrap/FactoryBasedBuilder.php +++ b/tests/behat/bootstrap/FactoryBasedBuilder.php @@ -171,7 +171,7 @@ public function buildPurchase(string $target, string $plan, string $time, ?array 'type' => 'monthly,cdn_traf95_max', 'quantity' => '1 items', 'target' => $target, - 'initial_uses' => $uses + 'initial_uses' => $uses, ]); } From 37c07ba8495d42fd8114cc1fb5b668840343f8c4 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Tue, 20 Aug 2024 20:25:14 +0300 Subject: [PATCH 16/28] HP-1631: modernized RestateActionsServiceTest Unit test --- src/plan/Plan.php | 5 +++++ src/plan/PlanInterface.php | 7 +++++++ src/plan/PlanRepositoryInterface.php | 2 ++ src/tools/FactoryInterface.php | 10 ++++++++++ 4 files changed, 24 insertions(+) diff --git a/src/plan/Plan.php b/src/plan/Plan.php index a582efa1..6de79254 100755 --- a/src/plan/Plan.php +++ b/src/plan/Plan.php @@ -85,6 +85,11 @@ public function getId() return $this->id; } + public function setId(int $id): void + { + $this->id = $id; + } + /** * @return string */ diff --git a/src/plan/PlanInterface.php b/src/plan/PlanInterface.php index 4b30b08a..1b4ca92d 100644 --- a/src/plan/PlanInterface.php +++ b/src/plan/PlanInterface.php @@ -28,6 +28,8 @@ interface PlanInterface extends EntityInterface */ public function getId(); + public function setId(int $id): void; + /** * Globally unique ID. * @return int|string @@ -38,6 +40,7 @@ public function getUniqueId(); * @return PriceInterface[] */ public function getPrices(): array; + public function hasPrices(): bool; /** @@ -45,8 +48,12 @@ public function hasPrices(): bool; * @throws CannotReassignException when prices are already set */ public function setPrices(array $prices): void; + public function getSeller(): ?CustomerInterface; + public function getName(): string; + public function setName(string $name): void; + public function getType(): ?TypeInterface; } diff --git a/src/plan/PlanRepositoryInterface.php b/src/plan/PlanRepositoryInterface.php index 735a2236..e1ca90d7 100644 --- a/src/plan/PlanRepositoryInterface.php +++ b/src/plan/PlanRepositoryInterface.php @@ -42,4 +42,6 @@ public function findByIds(array $ids); * @throws EntityNotFoundException */ public function getById(int $id): PlanInterface; + + public function save(PlanInterface $action): void; } diff --git a/src/tools/FactoryInterface.php b/src/tools/FactoryInterface.php index f39f0546..da4cf726 100644 --- a/src/tools/FactoryInterface.php +++ b/src/tools/FactoryInterface.php @@ -11,6 +11,10 @@ namespace hiqdev\php\billing\tools; +use hiqdev\php\billing\action\ActionInterface; +use hiqdev\php\billing\plan\PlanInterface; +use hiqdev\php\billing\sale\SaleInterface; + /** * @author Andrii Vasyliev */ @@ -20,4 +24,10 @@ interface FactoryInterface * Create billing object by entity name and data. */ public function create(string $entity, $data); + + public function getSale($data): SaleInterface; + + public function getAction($data): ActionInterface; + + public function getPlan($data): PlanInterface; } From dfb7231d0bc5fda197f5a222aa8818fe4bbe10be Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Wed, 21 Aug 2024 01:27:57 +0300 Subject: [PATCH 17/28] HP-1631: fixing RestateActionsServiceTest Unit test --- src/tools/Factory.php | 4 +-- src/tools/FactoryInterface.php | 49 ++++++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/tools/Factory.php b/src/tools/Factory.php index 55b39c9b..aeef2b15 100644 --- a/src/tools/Factory.php +++ b/src/tools/Factory.php @@ -60,7 +60,7 @@ public function getSums($data): array return $res; } - public function parseMoney($str): array + public function parseMoney(string $str): array { [$amount, $currency] = explode(' ', $str); @@ -85,7 +85,7 @@ public function getQuantity($data): QuantityInterface return $this->get('quantity', $data); } - public function parseQuantity(string$str): array + public function parseQuantity(string $str): array { [$quantity, $unit] = explode(' ', $str); diff --git a/src/tools/FactoryInterface.php b/src/tools/FactoryInterface.php index da4cf726..7e7c31de 100644 --- a/src/tools/FactoryInterface.php +++ b/src/tools/FactoryInterface.php @@ -1,5 +1,5 @@ - @@ -30,4 +41,38 @@ public function getSale($data): SaleInterface; public function getAction($data): ActionInterface; public function getPlan($data): PlanInterface; + + public function getCustomer($data): CustomerInterface; + + public function getCharge($data): ChargeInterface; + + public function getPrice($data): PriceInterface; + + public function getBill($data): BillInterface; + + public function getTarget($data): TargetInterface; + + public function getType($data): TypeInterface; + + public function getUnit($data): UnitInterface; + + public function createUnit($data): UnitInterface; + + public function getQuantity($data): QuantityInterface; + + public function createQuantity($data): QuantityInterface; + + public function parseQuantity(string $str): array; + + public function getMoney($data): Money; + + public function createMoney($data): Money; + + public function parseMoney(string $str): array; + + public function getCurrency($data): Currency; + + public function createTime($data): DateTimeImmutable; + + public function getTime($data): DateTimeImmutable; } From c9a4c9b3a893a0629e12a13035f06382e46626e0 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Wed, 21 Aug 2024 01:50:18 +0300 Subject: [PATCH 18/28] HP-1631: fixed CustomerHydrator and created CustomerStateHydrator --- src/customer/CustomerState.php | 41 +++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/src/customer/CustomerState.php b/src/customer/CustomerState.php index d542662d..a516ef21 100644 --- a/src/customer/CustomerState.php +++ b/src/customer/CustomerState.php @@ -2,12 +2,24 @@ namespace hiqdev\php\billing\customer; -enum CustomerState: string +class CustomerState { - case BLOCKED = 'blocked'; - case DELETED = 'deleted'; - case NEW = 'new'; - case OK = 'ok'; + public const BLOCKED = 'blocked'; + + public const DELETED = 'deleted'; + + public const NEW = 'new'; + + public const OK = 'ok'; + + private function __construct(protected string $state = self::NEW) + { + } + + public function getName(): string + { + return $this->state; + } public static function isDeleted(CustomerInterface $customer): bool { @@ -16,6 +28,23 @@ public static function isDeleted(CustomerInterface $customer): bool public static function deleted(): CustomerState { - return self::DELETED; + return new self(self::DELETED); + } + + public static function fromString(string $name): self + { + $allowedStates = [ + self::BLOCKED, + self::DELETED, + self::NEW, + self::OK, + ]; + foreach ($allowedStates as $state) { + if ($state === $name) { + return new self($state); + } + } + + throw new \Exception("wrong customer state '$name'"); } } From 2981ac3ddf75928e98c421d164e4281275c1c943 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Wed, 21 Aug 2024 01:54:58 +0300 Subject: [PATCH 19/28] HP-1631: added all possible states into CustomerState --- src/customer/Customer.php | 2 +- src/customer/CustomerState.php | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/customer/Customer.php b/src/customer/Customer.php index 290afb6a..96abaf97 100755 --- a/src/customer/Customer.php +++ b/src/customer/Customer.php @@ -108,7 +108,7 @@ public static function fromArray(array $info) $info['id'], $info['login'], $seller, - isset($info['state']) ? CustomerState::from($info['state']) : null, + isset($info['state']) ? CustomerState::fromString($info['state']) : null, ); } diff --git a/src/customer/CustomerState.php b/src/customer/CustomerState.php index a516ef21..0c4eb960 100644 --- a/src/customer/CustomerState.php +++ b/src/customer/CustomerState.php @@ -31,6 +31,21 @@ public static function deleted(): CustomerState return new self(self::DELETED); } + public static function blocked(): CustomerState + { + return new self(self::BLOCKED); + } + + public static function new(): CustomerState + { + return new self(self::NEW); + } + + public static function ok(): CustomerState + { + return new self(self::OK); + } + public static function fromString(string $name): self { $allowedStates = [ From 183a7dc21b32617a6cd3e09d0626980b9bef9e96 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Fri, 23 Aug 2024 16:30:46 +0300 Subject: [PATCH 20/28] HP-1631: fixed CustomerState::isDeleted() method --- src/customer/CustomerState.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/customer/CustomerState.php b/src/customer/CustomerState.php index 0c4eb960..08857a9c 100644 --- a/src/customer/CustomerState.php +++ b/src/customer/CustomerState.php @@ -23,7 +23,7 @@ public function getName(): string public static function isDeleted(CustomerInterface $customer): bool { - return $customer->getState() === self::DELETED; + return $customer->getState()?->getName() === self::DELETED; } public static function deleted(): CustomerState From 04a6a3c82dbb25550bece8c3925739e6ae3950ce Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Sat, 24 Aug 2024 15:08:10 +0300 Subject: [PATCH 21/28] HP-1631: ActionInterface must extend EntityInterface --- src/action/AbstractAction.php | 4 +--- src/action/ActionInterface.php | 3 ++- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/action/AbstractAction.php b/src/action/AbstractAction.php index 601b3db6..f3f6f627 100755 --- a/src/action/AbstractAction.php +++ b/src/action/AbstractAction.php @@ -10,10 +10,8 @@ namespace hiqdev\php\billing\action; -use DateInterval; use DateTimeImmutable; use hiqdev\php\billing\customer\CustomerInterface; -use hiqdev\php\billing\EntityInterface; use hiqdev\php\billing\Exception\CannotReassignException; use hiqdev\php\billing\sale\SaleInterface; use hiqdev\php\billing\target\TargetInterface; @@ -27,7 +25,7 @@ * * @author Andrii Vasyliev */ -abstract class AbstractAction implements ActionInterface, EntityInterface +abstract class AbstractAction implements ActionInterface { /** @var int */ protected $id; diff --git a/src/action/ActionInterface.php b/src/action/ActionInterface.php index a0924e67..1cf08807 100644 --- a/src/action/ActionInterface.php +++ b/src/action/ActionInterface.php @@ -12,6 +12,7 @@ use DateTimeImmutable; use hiqdev\php\billing\customer\CustomerInterface; +use hiqdev\php\billing\EntityInterface; use hiqdev\php\billing\price\PriceInterface; use hiqdev\php\billing\sale\SaleInterface; use hiqdev\php\billing\target\TargetInterface; @@ -30,7 +31,7 @@ * * @author Andrii Vasyliev */ -interface ActionInterface extends \JsonSerializable +interface ActionInterface extends EntityInterface { /** * Returns if the given price applicable to this action. From 7006e78f70657ce0fea02c141b57564d33f1a585 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Sat, 24 Aug 2024 15:49:26 +0300 Subject: [PATCH 22/28] HP-1631: returned failed state to ActionState --- src/action/ActionState.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/action/ActionState.php b/src/action/ActionState.php index 15b93b39..2278f32e 100644 --- a/src/action/ActionState.php +++ b/src/action/ActionState.php @@ -21,6 +21,8 @@ class ActionState private const STATE_FINISHED = 'finished'; + private const STATE_FAILED = 'failed'; + private const STATE_PREMATURE = 'premature'; private const STATE_FUTURE = 'future'; @@ -56,6 +58,11 @@ public static function finished(): self return new self(self::STATE_FINISHED); } + public static function failed(): self + { + return new self(self::STATE_FAILED); + } + public static function premature(): self { return new self(self::STATE_PREMATURE); From db98badb67e43e9db2dda9c79d2c6ba1a6e02bb4 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Sat, 24 Aug 2024 15:50:06 +0300 Subject: [PATCH 23/28] HP-1631: returned failed state to ActionState --- src/action/ActionState.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/action/ActionState.php b/src/action/ActionState.php index 2278f32e..5f982e9f 100644 --- a/src/action/ActionState.php +++ b/src/action/ActionState.php @@ -83,6 +83,7 @@ public static function fromString(string $name): self $allowedStates = [ self::STATE_NEW, self::STATE_FINISHED, + self::STATE_FAILED, self::STATE_PREMATURE, self::STATE_FUTURE, self::STATE_CANCELED, From dd9c18c608380798dcaf68225c6152e357ebd669 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Sat, 24 Aug 2024 15:58:57 +0300 Subject: [PATCH 24/28] HP-1631: created CustomerStateException --- src/Exception/CustomerStateException.php | 10 ++++++++++ src/customer/CustomerState.php | 4 +++- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 src/Exception/CustomerStateException.php diff --git a/src/Exception/CustomerStateException.php b/src/Exception/CustomerStateException.php new file mode 100644 index 00000000..5ea81cfe --- /dev/null +++ b/src/Exception/CustomerStateException.php @@ -0,0 +1,10 @@ + Date: Sat, 24 Aug 2024 16:03:09 +0300 Subject: [PATCH 25/28] HP-1631: tiny --- src/Exception/CustomerStateException.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Exception/CustomerStateException.php b/src/Exception/CustomerStateException.php index 5ea81cfe..861e62ef 100644 --- a/src/Exception/CustomerStateException.php +++ b/src/Exception/CustomerStateException.php @@ -6,5 +6,4 @@ class CustomerStateException extends RuntimeException implements ExceptionInterface { - } From 18bd6e5e0b85b23325ecdb85e16efd35c0074e9d Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Thu, 19 Sep 2024 02:02:23 +0300 Subject: [PATCH 26/28] HP-1631: fixed cancelled action state name --- src/action/ActionState.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/action/ActionState.php b/src/action/ActionState.php index 5f982e9f..4d2bb38b 100644 --- a/src/action/ActionState.php +++ b/src/action/ActionState.php @@ -27,7 +27,7 @@ class ActionState private const STATE_FUTURE = 'future'; - private const STATE_CANCELED = 'canceled'; + private const STATE_CANCELED = 'cancelled'; private function __construct(protected string $state = self::STATE_NEW) { From 11e129f1f97442bd12f9d6f805fd8d4e33cf34ce Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Tue, 15 Oct 2024 02:10:00 +0300 Subject: [PATCH 27/28] HP-1631 Fixed "Class SimplePlanRepository contains 1 abstract method and must therefore be declared abstract or implement the remaining methods" fatal error --- tests/support/plan/SimplePlanRepository.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/support/plan/SimplePlanRepository.php b/tests/support/plan/SimplePlanRepository.php index 09255d07..55b6f2f4 100644 --- a/tests/support/plan/SimplePlanRepository.php +++ b/tests/support/plan/SimplePlanRepository.php @@ -51,4 +51,9 @@ public function getById(int $id): PlanInterface { throw new \Exception('not implemented'); } + + public function save(PlanInterface $action): void + { + throw new \Exception('not implemented'); + } } From 9b70aed04bb538ec531e9236c4da809c150f269c Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Wed, 16 Oct 2024 03:20:50 +0300 Subject: [PATCH 28/28] HP-1631 fixed ActionStateDeterminer Behat test --- tests/behat/bootstrap/BuilderInterface.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/behat/bootstrap/BuilderInterface.php b/tests/behat/bootstrap/BuilderInterface.php index a9ecd334..8f561ba6 100644 --- a/tests/behat/bootstrap/BuilderInterface.php +++ b/tests/behat/bootstrap/BuilderInterface.php @@ -40,8 +40,6 @@ public function performBilling(string $time): void; public function setAction(string $type, int $amount, string $unit, string $target, string $time): void; - public function buildAction(array $data); - public function performCalculation(string $time): array; public function targetChangePlan(string $target, string $planName, string $date, string $wallTime = null);