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 12 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
4 changes: 2 additions & 2 deletions src/action/AbstractAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
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;
}
2 changes: 1 addition & 1 deletion src/action/ActionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
58 changes: 41 additions & 17 deletions src/action/ActionState.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,56 +17,80 @@
*/
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 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 premature(): self
{
return new self(self::STATE_PREMATURE);
}

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

public static function fromString(string $name)
public static function canceled(): self
{
foreach ([self::STATE_NEW, self::STATE_FINISHED, self::STATE_FAILED] as $state) {
return new self(self::STATE_CANCELED);
}

public static function fromString(string $name): self
{
$allowedStates = [
self::STATE_NEW,
self::STATE_FINISHED,
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::from($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;
}
21 changes: 21 additions & 0 deletions src/customer/CustomerState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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 $customer->getState() === self::DELETED;
}

public static function deleted(): CustomerState
{
return self::DELETED;
}
}
2 changes: 1 addition & 1 deletion src/order/Calculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function calculatePrice(PriceInterface $price, ActionInterface $action):
$charges = [$charge];
}

if ($action->isFinished()) {
if ($action->isNotActive()) {
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

⚠️ Potential issue

Inconsistent Method Usage Detected

The isFinished() method is still used in multiple classes (Bill, Charge, etc.), while Calculator.php now uses isNotActive(). This inconsistency could lead to confusion and potential bugs.

🔗 Analysis chain

Verify the semantic change and its implications

The condition for setting charges as finished has been changed from isFinished() to isNotActive(). This semantic change could have significant implications:

  1. The new condition might be broader, potentially setting charges as finished in more scenarios than before.
  2. Ensure that this change aligns with the business logic and doesn't introduce unintended side effects.
  3. Verify that the ActionInterface has been updated to include the isNotActive() method.
  4. Check for backwards compatibility issues if other parts of the codebase rely on the isFinished() method.
  5. Update the method's documentation to reflect this change in behavior.

To ensure consistency across the codebase, run the following script:

Consider adding a comment explaining the rationale behind this change to improve code maintainability.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for usage of isFinished() and isNotActive() methods

# Test 1: Check for remaining usages of isFinished()
echo "Checking for remaining usages of isFinished():"
rg --type php 'isFinished\(\)'

# Test 2: Check for new usages of isNotActive()
echo "Checking for new usages of isNotActive():"
rg --type php 'isNotActive\(\)'

# Test 3: Check if ActionInterface has been updated
echo "Checking if ActionInterface has been updated:"
rg --type php -p 'ActionInterface.php' 'function isNotActive\(\)'

Length of output: 1297


Script:

#!/bin/bash
# Description: Locate ActionInterface.php and verify the presence of isNotActive() method

# Test 1: Find the path to ActionInterface.php
echo "Locating ActionInterface.php in the repository:"
fd --type f 'ActionInterface\.php'

# Test 2: If found, check for the isNotActive() method within the file
# Note: This test will only run if the previous test finds the file
FILE_PATH=$(fd --type f 'ActionInterface\.php')
if [ -n "$FILE_PATH" ]; then
    echo "Checking for isNotActive() method in $FILE_PATH:"
    rg 'function\s+isNotActive\s*\(' "$FILE_PATH"
else
    echo "ActionInterface.php not found in the repository."
fi

Length of output: 867


Script:

#!/bin/bash
# Description: Locate the primary ActionInterface.php and verify the presence of isNotActive() method

# Test 1: Find the primary ActionInterface.php file, excluding temporary or backup files
echo "Locating the primary ActionInterface.php in the repository:"
fd --type f 'ActionInterface\.php' | grep -v 'TemporaryActionInterface\.php'

# Assign the found file to a variable
FILE_PATH=$(fd --type f 'ActionInterface\.php' | grep -v 'TemporaryActionInterface\.php')

# Test 2: If the primary ActionInterface.php is found, check for the isNotActive() method
if [ -n "$FILE_PATH" ]; then
    echo "Checking for isNotActive() method in $FILE_PATH:"
    rg 'function\s+isNotActive\s*\(' "$FILE_PATH"
else
    echo "Primary ActionInterface.php not found in the repository."
fi

Length of output: 661

foreach ($charges as $charge) {
$charge->setFinished();
}
Expand Down
8 changes: 2 additions & 6 deletions src/plan/PlanFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 1 addition & 5 deletions src/plan/PlanFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,5 @@
*/
interface PlanFactoryInterface
{
/**
* Creates plan object.
* @return Plan
*/
public function create(PlanCreationDto $dto);
public function create(PlanCreationDto $dto): PlanInterface;
}
3 changes: 0 additions & 3 deletions src/price/PriceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ public function __construct(array $types = [], $defaultClass = null)
}


/**
* Creates price object.
*/
public function create(PriceCreationDto $dto): PriceInterface
{
$class = $this->findClassForTypes([
Expand Down
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
Loading