DefinitionService   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 112
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getValidationResult() 0 5 1
A getDefinition() 0 12 2
A getDefinitionArray() 0 12 2
A buildDefinitionObject() 0 15 3
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 CuyZ\Notiz\Core\Definition\Builder\DefinitionBuilder;
21
use CuyZ\Notiz\Core\Definition\Tree\Definition;
22
use CuyZ\Notiz\Core\Exception\InvalidDefinitionException;
23
use CuyZ\Notiz\Service\RuntimeService;
24
use CuyZ\Notiz\Service\Traits\ExtendedSelfInstantiateTrait;
25
use Romm\ConfigurationObject\ConfigurationObjectInstance;
26
use Throwable;
27
use TYPO3\CMS\Core\SingletonInterface;
28
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...
29
30
/**
31
 * Root definition service, that can be used to get the definition object used
32
 * everywhere in this extension.
33
 *
34
 * A validation process is done on the object to check if all its properties and
35
 * sub-properties are valid. Use the code below to check for errors:
36
 *
37
 * ```
38
 * $validationResult = DefinitionService::get()->getValidationResult();
39
 *
40
 * if ($validationResult->hasErrors()) {
41
 *     // ...
42
 * } else {
43
 *     // ...
44
 * }
45
 * ```
46
 *
47
 * To get the definition object, just use the code below:
48
 *
49
 * `DefinitionService::get()->getDefinition()`
50
 *
51
 * Please note that if the definition validation failed, an exception will be
52
 * thrown if you try to access the definition object: you must first check that
53
 * no errors exist (see above).
54
 */
55
class DefinitionService implements SingletonInterface
56
{
57
    use ExtendedSelfInstantiateTrait;
58
59
    /**
60
     * @var ConfigurationObjectInstance
61
     */
62
    protected $definitionObject;
63
64
    /**
65
     * @var Result
66
     */
67
    protected $validationResult;
68
69
    /**
70
     * If an exception/error was thrown during the creation of the definition
71
     * object, this property is filled with it.
72
     *
73
     * @var Throwable
74
     */
75
    protected $exception;
76
77
    /**
78
     * @var DefinitionBuilder
79
     */
80
    protected $builder;
81
82
    /**
83
     * @var RuntimeService
84
     */
85
    protected $runtimeService;
86
87
    /**
88
     * @param DefinitionBuilder $builder
89
     * @param RuntimeService $runtimeService
90
     */
91
    public function __construct(DefinitionBuilder $builder, RuntimeService $runtimeService)
92
    {
93
        $this->builder = $builder;
94
        $this->runtimeService = $runtimeService;
95
    }
96
97
    /**
98
     * @return Result
99
     */
100
    public function getValidationResult(): Result
101
    {
102
        $this->buildDefinitionObject();
103
104
        return $this->validationResult;
105
    }
106
107
    /**
108
     * @return Definition
109
     *
110
     * @throws InvalidDefinitionException
111
     */
112
    public function getDefinition(): Definition
113
    {
114
        $this->buildDefinitionObject();
115
116
        if ($this->validationResult->hasErrors()) {
117
            throw InvalidDefinitionException::definitionErrorNoAccess();
118
        }
119
120
        /** @var Definition $definition */
121
        $definition = $this->definitionObject->getObject();
122
123
        return $definition;
124
    }
125
126
    /**
127
     * Please note that the returned array can not be considered as a valid
128
     * definition array! You must still check the validation result.
129
     *
130
     * @return array
131
     */
132
    public function getDefinitionArray(): array
133
    {
134
        $this->buildDefinitionObject();
135
136
        if ($this->exception) {
137
            return [];
138
        }
139
140
        /** @var Definition $definition */
141
        $definition = $this->definitionObject->getObject(true);
142
143
        return $definition->toArray();
144
    }
145
146
    /**
147
     * Calls the builder to get the definition object instance.
148
     *
149
     * If an exception/error is thrown during the process, it is catch and added
150
     * to the runtime service.
151
     */
152
    protected function buildDefinitionObject()
153
    {
154
        if (null === $this->definitionObject) {
155
            $this->definitionObject = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type Romm\ConfigurationObject...igurationObjectInstance of property $definitionObject.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
156
157
            $exception = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $exception is dead and can be removed.
Loading history...
158
159
            try {
160
                $this->definitionObject = $this->builder->buildDefinition();
161
                $this->validationResult = $this->definitionObject->getValidationResult();
162
            } catch (Throwable $exception) {
163
                $this->exception = $exception;
164
                $this->validationResult = new Result;
165
166
                $this->runtimeService->setException($exception);
167
            }
168
        }
169
    }
170
}
171