Skip to content

Commit 5f4ed75

Browse files
committed
feat: replace InvalidArgumentException with BizException and SysException in CategoryStatus and CategoryRecord
1 parent b031f6d commit 5f4ed75

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

contexts/CategoryManagement/Domain/Models/CategoryStatus.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Contexts\CategoryManagement\Domain\Models;
66

7-
use InvalidArgumentException;
7+
use App\Exceptions\BizException;
88

99
class CategoryStatus
1010
{
@@ -17,7 +17,8 @@ class CategoryStatus
1717
public function __construct(private string $value)
1818
{
1919
if (! in_array($value, [self::SUBSPENDED, self::ACTIVE, self::DELETED])) {
20-
throw new InvalidArgumentException('Invalid category status');
20+
throw BizException::make('Invalid status: :status')
21+
->with('status', $value);
2122
}
2223
}
2324

@@ -45,19 +46,16 @@ public function transitionTo(CategoryStatus $target): self
4546
};
4647

4748
if (! in_array($target->value, $validTransitions)) {
48-
// TODO: Create a custom exception
49-
throw new InvalidArgumentException("Cannot transition from {$this->value} to {$target->value}");
49+
throw BizException::make('Cannot transition from :from to :to')
50+
->with('from', $this->value)
51+
->with('to', $target->value);
5052
}
5153

5254
return $target;
5355
}
5456

5557
public static function fromString(string $status): self
5658
{
57-
if (! in_array($status, [self::SUBSPENDED, self::ACTIVE, self::DELETED])) {
58-
throw new InvalidArgumentException('Invalid category status');
59-
}
60-
6159
return new self($status);
6260
}
6361

contexts/CategoryManagement/Infrastructure/Records/CategoryRecord.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Contexts\CategoryManagement\Infrastructure\Records;
66

7+
use App\Exceptions\SysException;
78
use App\Http\Models\BaseModel;
89
use Contexts\CategoryManagement\Domain\Models\Category;
910
use Contexts\CategoryManagement\Domain\Models\CategoryId;
@@ -34,7 +35,7 @@ class CategoryRecord extends BaseModel
3435
public static function mapStatusToDomain(int $status): CategoryStatus
3536
{
3637
if (! isset(self::STATUS_MAPPING[$status])) {
37-
throw new \InvalidArgumentException('Invalid status value');
38+
throw SysException::make('Invalid status value: '.$status);
3839
}
3940

4041
return new CategoryStatus(self::STATUS_MAPPING[$status]);
@@ -43,7 +44,7 @@ public static function mapStatusToDomain(int $status): CategoryStatus
4344
public static function mapStatusToRecord(CategoryStatus $status): int
4445
{
4546
if (! in_array($status->getValue(), self::STATUS_MAPPING)) {
46-
throw new \InvalidArgumentException('Invalid status value');
47+
throw SysException::make('Invalid status value: '.$status->getValue());
4748
}
4849

4950
return array_search($status->getValue(), self::STATUS_MAPPING);

contexts/CategoryManagement/Tests/Unit/Domain/Models/CategoryStatusTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
declare(strict_types=1);
44

5+
use App\Exceptions\BizException;
56
use Contexts\CategoryManagement\Domain\Models\CategoryStatus;
67

78
it('can be created', function (string $validValue) {
@@ -11,7 +12,7 @@
1112
})->with(['subspended', 'active', 'subspended', 'deleted']);
1213

1314
it('throws an exception when the status is invalid', function (string $invalidValue) {
14-
$this->expectException(\InvalidArgumentException::class);
15+
$this->expectException(BizException::class);
1516

1617
new CategoryStatus($invalidValue);
1718
})->with(['invalid', 'status']);
@@ -28,7 +29,7 @@
2829
it('throws an exception when transitioning to active from active', function () {
2930
$categoryStatus = new CategoryStatus('active');
3031

31-
$this->expectException(\InvalidArgumentException::class);
32+
$this->expectException(BizException::class);
3233

3334
$categoryStatus->transitionTo(CategoryStatus::active());
3435
});
@@ -40,7 +41,7 @@
4041
})->with(['subspended', 'active', 'subspended', 'deleted']);
4142

4243
it('throws an exception when creating from invalid string status', function (string $invalidValue) {
43-
$this->expectException(\InvalidArgumentException::class);
44+
$this->expectException(BizException::class);
4445

4546
CategoryStatus::fromString($invalidValue);
4647
})->with(['invalid', 'status']);

0 commit comments

Comments
 (0)