Completed
Push — master ( 27c584...7678fd )
by Andrii
13:30
created

SaleCreateAction   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 13
c 1
b 0
f 0
dl 0
loc 31
rs 10

3 Methods

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