Cancelled
Push — master ( a8b464...2fc3a5 )
by Billie
06:55
created

ValidationServiceImplementation::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
declare(strict_types = 1);
3
namespace PurpleBooth\GitGitHubLint;
4
5
use PurpleBooth\GitGitHubLint\Status\Status;
6
use PurpleBooth\GitGitHubLint\Validator\Validator;
7
8
/**
9
 * Validate against a number of validators against a message and return the most high priority status for that message
10
 *
11
 * @package PurpleBooth\GitGitHubLint
12
 */
13
class ValidationServiceImplementation implements ValidationService
14
{
15
    /**
16
     * @var array
17
     */
18
    private $validators;
19
20
    /**
21
     * ValidationServiceImplementation constructor.
22
     *
23
     * @param array $validators
24
     */
25
    public function __construct(array $validators)
26
    {
27
        $this->validators = $validators;
28
29
        if (count($validators) < 1) {
30
            new \LogicException("You need to provide the validation service with at least 1 validator");
31
        }
32
    }
33
34
    /**
35
     * Test a message against a number of validators
36
     *
37
     * @param Message $message
38
     *
39
     * @return Status
40
     */
41
    public function validate(Message $message) : Status
42
    {
43
        $statuses = [];
44
45
        /** @var Validator $validator */
46
        foreach ($this->validators as $validator) {
47
            $statuses[] = $validator->validate($message);
48
        }
49
50
        usort($statuses, function (Status $statusA, Status $statusB) {
51
            return $statusA->getWeight() <=> $statusB->getWeight();
52
        });
53
54
        return array_pop($statuses);
55
    }
56
}
57