ValidatorMiddleware::handle()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
3
/**
4
 * GpsLab component.
5
 *
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2011, Peter Gribanov
8
 * @license   http://opensource.org/licenses/MIT
9
 */
10
11
namespace GpsLab\Component\Middleware;
12
13
use GpsLab\Component\Middleware\Exception\InvalidMessageException;
14
use Symfony\Component\Validator\Validator\ValidatorInterface;
15
16
class ValidatorMiddleware implements Middleware
17
{
18
    /**
19
     * @var ValidatorInterface
20
     */
21
    protected $validator;
22
23
    /**
24
     * @param ValidatorInterface $validator
25
     */
26 2
    public function __construct(ValidatorInterface $validator)
27
    {
28 2
        $this->validator = $validator;
29 2
    }
30
31
    /**
32
     * @param mixed    $message
33
     * @param callable $next
34
     *
35
     * @return mixed
36
     */
37 2
    public function handle($message, callable $next)
38
    {
39 2
        $violations = $this->validator->validate($message);
40
41 2
        if (count($violations)) {
42 1
            throw new InvalidMessageException($violations);
43
        }
44
45 1
        return $next($message);
46
    }
47
}
48