|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of FlexPHP. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Freddie Gar <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace FlexPHP\UseCases; |
|
11
|
|
|
|
|
12
|
|
|
use FlexPHP\Repositories\RepositoryInterface; |
|
13
|
|
|
use FlexPHP\UseCases\Exception\NotValidRequestException; |
|
14
|
|
|
use FlexPHP\UseCases\Exception\UndefinedRepositoryUseCaseException; |
|
15
|
|
|
|
|
16
|
|
|
abstract class UseCase implements UseCaseInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var null|RepositoryInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
private $repository; |
|
22
|
|
|
|
|
23
|
7 |
|
public function __construct(RepositoryInterface $repository = null) |
|
24
|
|
|
{ |
|
25
|
7 |
|
if (!\is_null($repository)) { |
|
26
|
2 |
|
$this->setRepository($repository); |
|
27
|
|
|
} |
|
28
|
7 |
|
} |
|
29
|
|
|
|
|
30
|
3 |
|
public function setRepository(RepositoryInterface $repository): void |
|
31
|
|
|
{ |
|
32
|
3 |
|
$this->repository = $repository; |
|
33
|
3 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @throws UndefinedRepositoryUseCaseException |
|
37
|
|
|
*/ |
|
38
|
4 |
|
public function getRepository(): RepositoryInterface |
|
39
|
|
|
{ |
|
40
|
4 |
|
if (\is_null($this->repository)) { |
|
41
|
1 |
|
throw new UndefinedRepositoryUseCaseException(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
3 |
|
return $this->repository; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param mixed $requestUsed |
|
49
|
|
|
* |
|
50
|
|
|
* @throws NotValidRequestException |
|
51
|
|
|
*/ |
|
52
|
5 |
|
public function throwExceptionIfRequestNotValid(string $function, string $requestExpected, $requestUsed): void |
|
53
|
|
|
{ |
|
54
|
5 |
|
if (!$requestUsed instanceof $requestExpected) { |
|
55
|
2 |
|
throw new NotValidRequestException($function, $requestExpected, $requestUsed); |
|
56
|
|
|
} |
|
57
|
3 |
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|