Completed
Push — develop ( 9071eb...a28223 )
by Freddie
02:39
created

UseCase::throwExceptionIfRequestNotValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace FlexPHP\UseCases;
4
5
use FlexPHP\Repositories\RepositoryInterface;
6
use FlexPHP\UseCases\Exception\UndefinedRepositoryUseCaseException;
7
use FlexPHP\UseCases\Exception\NotValidRequestException;
8
9
/**
10
 * Class UseCase
11
 * @package FlexPHP\UseCases
12
 */
13
abstract class UseCase implements UseCaseInterface
14
{
15
    /**
16
     * @var RepositoryInterface|null
17
     */
18
    private $repository = null;
19
20
    public function __construct(RepositoryInterface $repository = null)
21
    {
22
        if (!\is_null($repository)) {
23
            $this->setRepository($repository);
24
        }
25
    }
26
27
    public function setRepository(RepositoryInterface $repository): UseCaseInterface
28
    {
29
        $this->repository = $repository;
30
31
        return $this;
32
    }
33
34
    /**
35
     * @throws UndefinedRepositoryUseCaseException
36
     */
37
    public function getRepository(): RepositoryInterface
38
    {
39
        if (\is_null($this->repository)) {
40
            throw new UndefinedRepositoryUseCaseException();
41
        }
42
43
        return $this->repository;
44
    }
45
46
    /**
47
     * @throws NotValidRequestException
48
     */
49
    public function throwExceptionIfRequestNotValid(string $function, string $requestExpected, $requestUsed): void
50
    {
51
        if (!$requestUsed instanceof $requestExpected) {
52
            throw new NotValidRequestException($function, $requestExpected, $requestUsed);
53
        }
54
    }
55
}
56