DefinitionTransformer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 * Copyright (C)
6
 * Nathan Boiron <[email protected]>
7
 * Romain Canon <[email protected]>
8
 *
9
 * This file is part of the TYPO3 NotiZ project.
10
 * It is free software; you can redistribute it and/or modify it
11
 * under the terms of the GNU General Public License, either
12
 * version 3 of the License, or any later version.
13
 *
14
 * For the full copyright and license information, see:
15
 * http://www.gnu.org/licenses/gpl-3.0.html
16
 */
17
18
namespace CuyZ\Notiz\Core\Definition;
19
20
use TYPO3\CMS\Core\SingletonInterface;
21
use TYPO3\CMS\Extbase\Error\Result;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Error\Result 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...
22
23
class DefinitionTransformer implements SingletonInterface
24
{
25
    /**
26
     * @var DefinitionService
27
     */
28
    protected $definitionService;
29
30
    /**
31
     * @param DefinitionService $definitionService
32
     */
33
    public function __construct(DefinitionService $definitionService)
34
    {
35
        $this->definitionService = $definitionService;
36
    }
37
38
    /**
39
     * Returns a custom definition array, where every last part of the array
40
     * is not the real value, but an array that contains the following keys:
41
     *
42
     * - `value` - the actual value
43
     * - `path` - the full path to this value
44
     * - `errors` - errors found at this path during the validation
45
     * - `warnings` - warnings found at this path during the validation
46
     *
47
     * @return array
48
     */
49
    public function getDefinitionArray(): array
50
    {
51
        return $this->transformDefinition(
52
            $this->definitionService->getDefinitionArray(),
53
            $this->definitionService->getValidationResult()
54
        );
55
    }
56
57
    /**
58
     * @param array $definition
59
     * @param Result $result
60
     * @param array $path
61
     * @return array
62
     */
63
    protected function transformDefinition(array $definition, Result $result, array $path = []): array
64
    {
65
        $newDefinition = [];
66
67
        foreach ($definition as $key => $value) {
68
            $data = [];
69
            $newPath = array_merge($path, [$key]);
70
            $readablePath = implode('.', $newPath);
71
72
            if (is_array($value)) {
73
                $data['sub'] = $this->transformDefinition($value, $result, $newPath);
74
            } else {
75
                $data['value'] = $value;
76
                $data['path'] = $readablePath;
77
            }
78
79
            $propertyResult = $result->forProperty($readablePath);
80
81
            $data['errors'] = $propertyResult->getErrors();
82
            $data['warnings'] = $propertyResult->getWarnings();
83
84
            $newDefinition[$key] = $data;
85
        }
86
87
        if (empty($path)) {
88
            $newDefinition = [
89
                'sub' => $newDefinition,
90
            ];
91
92
            $newDefinition['errors'] = $result->getErrors();
93
            $newDefinition['warnings'] = $result->getWarnings();
94
        }
95
96
        return $newDefinition;
97
    }
98
}
99