ModelInstanceValidator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Xml\Impl\Validation;
4
5
use Xml\Impl\ModelInstanceImpl;
6
use Xml\Validation\ValidationResultsInterface;
7
8
class ModelInstanceValidator
9
{
10
    protected $modelInstanceImpl;
11
    private $validators;
12
13
    public function __construct(ModelInstanceImpl $modelInstanceImpl, array $validators)
14
    {
15
        $this->modelInstanceImpl = $modelInstanceImpl;
16
        $this->validators = $validators;
17
    }
18
19
    public function validate(): ValidationResultsInterface
20
    {
21
        $resultCollector = new ValidationResultsCollectorImpl();
22
23
        foreach ($this->validators as $validator) {
24
            $elementType = $validator->getElementType();
25
            $modelElementsByType = $modelInstanceImpl->getModelElementsByType($elementType);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $modelInstanceImpl seems to be never defined.
Loading history...
26
27
            foreach ($modelElementsByType as $element) {
28
                $resultCollector->setCurrentElement($element);
29
30
                try {
31
                    $validator->validate($element, $resultCollector);
32
                } catch (\Exception $e) {
33
                    throw new Exception("validator threw an exception while validating an element");
0 ignored issues
show
Bug introduced by
The type Xml\Impl\Validation\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
34
                }
35
            }
36
        }
37
38
        return $resultsCollector->getResults();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $resultsCollector does not exist. Did you maybe mean $resultCollector?
Loading history...
39
    }
40
}
41