Passed
Push — master ( ebb653...69cfe7 )
by Andrii
12:58
created

SaleCloseAction   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 12
c 1
b 0
f 0
dl 0
loc 29
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A checkRequiredInput() 0 7 3
A __construct() 0 3 1
A __invoke() 0 8 1
1
<?php
2
3
namespace hiqdev\billing\hiapi\sale\Close;
4
5
use hiqdev\billing\hiapi\sale\SaleRepository;
6
use hiqdev\php\billing\plan\Plan;
7
use hiqdev\php\billing\sale\Sale;
8
use hiapi\exceptions\domain\RequiredInputException;
9
10
class SaleCloseAction
11
{
12
    /**
13
     * @var SaleRepository
14
     */
15
    private $repo;
16
17
    public function __construct(SaleRepository $repo)
18
    {
19
        $this->repo = $repo;
20
    }
21
22
    public function __invoke(SaleCloseCommand $command): Sale
23
    {
24
        $this->checkRequiredInput($command);
25
        $plan = new Plan($command->plan_id, null);
26
        $sale = new Sale(null, $command->target, $command->customer, $plan, $command->time);
27
        $this->repo->delete($sale);
28
29
        return $sale;
30
    }
31
32
    protected function checkRequiredInput(SaleCloseCommand $command)
33
    {
34
        if (empty($command->customer)) {
35
            throw new RequiredInputException('customer');
36
        }
37
        if (empty($command->target)) {
38
            throw new RequiredInputException('target');
39
        }
40
    }
41
}
42