UseCase   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 10
c 2
b 0
f 0
dl 0
loc 40
rs 10
ccs 14
cts 14
cp 1
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A setRepository() 0 3 1
A getRepository() 0 7 2
A throwExceptionIfRequestNotValid() 0 4 2
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