Factory   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 21
c 1
b 0
f 0
dl 0
loc 52
ccs 26
cts 26
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getRequestValidator() 0 6 2
A getResponseValidator() 0 6 2
A fromYamlFile() 0 10 2
A fromJsonFile() 0 10 2
1
<?php
2
3
namespace CHStudio\Raven\Bridge\LeagueOpenAPIValidation;
4
5
use CHStudio\Raven\Bridge\LeagueOpenAPIValidation\Exception\InvalidOpenApiDefinitionException;
6
use CHStudio\Raven\Bridge\LeagueOpenAPIValidation\Exception\ValidationExceptionMapper;
7
use InvalidArgumentException;
8
use League\OpenAPIValidation\PSR7\ValidatorBuilder;
9
use Throwable;
10
11
class Factory
12
{
13
    private readonly ValidationExceptionMapper $mapper;
14
15 5
    public function __construct(
16
        private readonly ValidatorBuilder $validator,
17
        ValidationExceptionMapper $mapper = null
18
    ) {
19 5
        $this->mapper = $mapper ?? new ValidationExceptionMapper();
0 ignored issues
show
Bug introduced by
The property mapper is declared read-only in CHStudio\Raven\Bridge\Le...enAPIValidation\Factory.
Loading history...
20
    }
21
22 2
    public static function fromYamlFile(string $path): self
23
    {
24 2
        if (!is_readable($path)) {
25 1
            throw new InvalidArgumentException(
26 1
                sprintf('Filename given isn\'t readable: %s', $path)
27 1
            );
28
        }
29
30 1
        return new self(
31 1
            (new ValidatorBuilder())->fromYamlFile($path)
32 1
        );
33
    }
34
35 2
    public static function fromJsonFile(string $path): self
36
    {
37 2
        if (!is_readable($path)) {
38 1
            throw new InvalidArgumentException(
39 1
                sprintf('Filename given isn\'t readable: %s', $path)
40 1
            );
41
        }
42
43 1
        return new self(
44 1
            (new ValidatorBuilder())->fromJsonFile($path)
45 1
        );
46
    }
47
48 4
    public function getRequestValidator(): RequestValidator
49
    {
50
        try {
51 4
            return new RequestValidator($this->validator->getRequestValidator(), $this->mapper);
52 1
        } catch (Throwable $error) {
53 1
            throw new InvalidOpenApiDefinitionException($error);
54
        }
55
    }
56
57 4
    public function getResponseValidator(): ResponseValidator
58
    {
59
        try {
60 4
            return new ResponseValidator($this->validator->getResponseValidator(), $this->mapper);
61 1
        } catch (Throwable $error) {
62 1
            throw new InvalidOpenApiDefinitionException($error);
63
        }
64
    }
65
}
66