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 26 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
9 changes: 9 additions & 0 deletions src/Exception/CustomerStateException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);

namespace hiqdev\php\billing\Exception;

use hiqdev\php\billing\ExceptionInterface;

class CustomerStateException extends RuntimeException implements ExceptionInterface
{
}
8 changes: 3 additions & 5 deletions src/action/AbstractAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +25,7 @@
*
* @author Andrii Vasyliev <[email protected]>
*/
abstract class AbstractAction implements ActionInterface, EntityInterface
abstract class AbstractAction implements ActionInterface
{
/** @var int */
protected $id;
Expand Down Expand Up @@ -169,9 +167,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();
Comment on lines +170 to +172
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Update Remaining Usages of isFinished() to isNotActive()

The method isFinished() is still used in the following files:

  • src/bill/Bill.php
  • src/charge/Charge.php

Please update these method calls to isNotActive() to maintain consistency and prevent potential errors.

🔗 Analysis chain

Approve renaming isFinished() to isNotActive() with suggestions

The renaming of isFinished() to isNotActive() improves clarity and aligns better with the method's functionality. The internal logic update is consistent with this change.

Consider adding a PHPDoc comment to explain the purpose and behavior of the isNotActive() method, especially if it differs from the previous isFinished() method.

Please run the following script to check for any remaining usage of the isFinished() method in the codebase:

Ensure that all occurrences of isFinished() are updated to isNotActive() if they refer to this class.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for usage of isFinished() method

# Test: Search for isFinished() method calls
rg --type php -e "\->isFinished\(\)"

Length of output: 226

}

/**
Expand Down
18 changes: 12 additions & 6 deletions src/action/ActionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
}
6 changes: 1 addition & 5 deletions src/action/ActionFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,5 @@
*/
interface ActionFactoryInterface
{
/**
* Creates action object.
* @return Action
*/
public function create(ActionCreationDto $dto);
public function create(ActionCreationDto $dto): AbstractAction;
}
5 changes: 3 additions & 2 deletions src/action/ActionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,7 +31,7 @@
*
* @author Andrii Vasyliev <[email protected]>
*/
interface ActionInterface extends \JsonSerializable
interface ActionInterface extends EntityInterface
{
/**
* Returns if the given price applicable to this action.
Expand Down Expand Up @@ -70,7 +71,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;

Expand Down
64 changes: 48 additions & 16 deletions src/action/ActionState.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,56 +17,88 @@
*/
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_FAILED = 'failed';

private const STATE_PREMATURE = 'premature';

private const STATE_FUTURE = 'future';

private const STATE_CANCELED = 'cancelled';

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 isNotActive(): 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()
public static function finished(): self
{
return new self(self::STATE_FINISHED);
}

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

public static function fromString(string $name)
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 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_FAILED,
self::STATE_PREMATURE,
self::STATE_FUTURE,
self::STATE_CANCELED,
];
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();
}
}
6 changes: 1 addition & 5 deletions src/bill/BillFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 1 addition & 5 deletions src/bill/BillFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,5 @@
*/
interface BillFactoryInterface
{
/**
* Creates bill object.
* @return Bill
*/
public function create(BillCreationDto $dto);
public function create(BillCreationDto $dto): BillInterface;
}
6 changes: 1 addition & 5 deletions src/charge/ChargeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 1 addition & 5 deletions src/charge/ChargeFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,5 @@
*/
interface ChargeFactoryInterface
{
/**
* Creates charge object.
* @return Charge
*/
public function create(ChargeCreationDto $dto);
public function create(ChargeCreationDto $dto): ChargeInterface;
}
32 changes: 30 additions & 2 deletions src/customer/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ class Customer implements CustomerInterface
*/
protected $sellers = [];

public function __construct($id, $login, CustomerInterface $seller = null)
protected ?CustomerState $state = null;

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

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

/**
* {@inheritdoc}
*/
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);
}

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

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

public function jsonSerialize(): array
Expand Down
6 changes: 1 addition & 5 deletions src/customer/CustomerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 1 addition & 5 deletions src/customer/CustomerFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,5 @@
*/
interface CustomerFactoryInterface
{
/**
* Creates customer object.
* @return Customer
*/
public function create(CustomerCreationDto $dto);
public function create(CustomerCreationDto $dto): CustomerInterface;
}
10 changes: 10 additions & 0 deletions src/customer/CustomerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,14 @@ public function getLogin();
* @return static|null
*/
public function getSeller();

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

public function setState(CustomerState $state): self;

public function isDeleted(): bool;
}
Loading
Loading