Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.74% covered (success)
94.74%
18 / 19
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
DiscountApplicationService
94.44% covered (success)
94.44%
17 / 18
83.33% covered (warning)
83.33%
5 / 6
8.01
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDiscounts
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDiscount
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 saveDiscount
92.31% covered (success)
92.31%
12 / 13
0.00% covered (danger)
0.00%
0 / 1
3.00
 deleteDiscount
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 findDefault
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5/** @license For full copyright and license information, please see the LICENSE.md file. */
6
7namespace App\Modules\Discount\Application\Service;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11use App\Modules\Discount\Application\DTO\DiscountRateDto;
12use App\Modules\Discount\Domain\Model\DiscountRate;
13use App\Modules\Discount\Domain\Repository\DiscountRepositoryInterface;
14use App\Modules\Discount\Domain\ValueObject\DiscountType;
15use InvalidArgumentException;
16
17/**
18 * Application service orchestrating discount management use cases.
19 */
20final readonly class DiscountApplicationService
21{
22    /**
23     * DiscountApplicationService constructor.
24     */
25    public function __construct(
26        private DiscountRepositoryInterface $discountRepository
27    ) {
28    }
29
30    /**
31     * List all discount rates.
32     *
33     * @param array<string, mixed> $filters Optional filter parameters.
34     * @return DiscountRate[]
35     */
36    public function getDiscounts(array $filters = []): array
37    {
38        return $this->discountRepository->findAll($filters);
39    }
40
41    /**
42     * Find single discount rate by ID.
43     */
44    public function getDiscount(int $id): ?DiscountRate
45    {
46        return $this->discountRepository->findById($id);
47    }
48
49    /**
50     * Save discount rate from DTO.
51     */
52    public function saveDiscount(DiscountRateDto $dto, int $owner = 1): DiscountRate
53    {
54        if ($dto->discountCode === '' || $dto->discountName === '') {
55            throw new InvalidArgumentException('Discount code and name are required.');
56        }
57
58        $discountRate = new DiscountRate(
59            id: $dto->id,
60            discountCode: $dto->discountCode,
61            discountName: $dto->discountName,
62            discountType: DiscountType::from($dto->discountType),
63            discountValue: $dto->discountValue,
64            isDefault: $dto->isDefault,
65            isActive: $dto->isActive,
66            owner: $owner,
67        );
68
69        return $this->discountRepository->save($discountRate);
70    }
71
72    /**
73     * Delete discount rate by ID.
74     */
75    public function deleteDiscount(int $id): bool
76    {
77        return $this->discountRepository->delete($id);
78    }
79
80    /**
81     * Find default discount rate.
82     */
83    public function findDefault(): ?DiscountRate
84    {
85        return $this->discountRepository->findDefault();
86    }
87}