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

SaleCloseAction::checkRequiredInput()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 3
nc 3
nop 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