JsonSchemaExceptionFakeHandler   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A deepArray() 0 9 3
A handle() 0 7 1
A fakeResponse() 0 10 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource;
6
7
use BEAR\Resource\Exception\JsonSchemaException;
8
use JSONSchemaFaker\Faker;
9
use LogicException;
10
use Override;
11
use SplFileInfo;
12
use stdClass;
13
14
use function class_exists;
15
use function is_object;
16
17
/** @psalm-import-type Body from Types */
18
final class JsonSchemaExceptionFakeHandler implements JsonSchemaExceptionHandlerInterface
19
{
20
    final public const X_FAKE_JSON = 'X-Fake-JSON';
21
    final public const X_JSON_SCHEMA_EXCEPTION = 'X-JSON-Schema-Exception';
22
23
    /**
24
     * {@inheritDoc}
25
     */
26
    #[Override]
27
    public function handle(ResourceObject $ro, JsonSchemaException $e, string $schemaFile)
28
    {
29
        $ro->headers[self::X_FAKE_JSON] = $schemaFile;
30
        $ro->headers[self::X_JSON_SCHEMA_EXCEPTION] = $e->getMessage();
31
        $ro->body = $this->fakeResponse($schemaFile);
32
        $ro->view = null;
33
    }
34
35
    /** @return Body */
0 ignored issues
show
Bug introduced by
The type BEAR\Resource\Body was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
    private function fakeResponse(string $schemaFile): array
37
    {
38
        if (! class_exists(Faker::class)) {
39
            throw new LogicException('"koriym/json-schema-faker" not installed. Please run "composer require koriym/json-schema-faker --dev". See more at https://github.com/bearsunday/BEAR.Resource/wiki/json_schema_faker_required'); // @codeCoverageIgnore
40
        }
41
42
        /** @var Body $fakeObject */
43
        $fakeObject = (new Faker())->generate(new SplFileInfo($schemaFile));
44
45
        return $this->deepArray($fakeObject);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->deepArray($fakeObject) returns the type array which is incompatible with the documented return type BEAR\Resource\Body.
Loading history...
46
    }
47
48
    /**
49
     * @param Body $values
50
     *
51
     * @return Body
52
     */
53
    private function deepArray(array|stdClass $values): array
54
    {
55
        $result = [];
56
        /** @psalm-suppress MixedAssignment */
57
        foreach ($values as $key => $value) { //@phpstan-ignore-line
58
            $result[$key] = is_object($value) ? $this->deepArray((array) $value) : $result[$key] = $value;
59
        }
60
61
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result returns the type array which is incompatible with the documented return type BEAR\Resource\Body.
Loading history...
62
    }
63
}
64