Completed
Push — master ( 67a91e...657487 )
by Peter
06:52
created

ValidatorMiddlewareHandler::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 6
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\Handler;
12
13
use GpsLab\Component\Middleware\Handler\Exception\InvalidMessageException;
14
use Symfony\Component\Validator\Validator\ValidatorInterface;
15
16
class ValidatorMiddlewareHandler implements MiddlewareHandler
17
{
18
    /**
19
     * @var ValidatorInterface
20
     */
21
    protected $validator;
22
23
    /**
24
     * @param ValidatorInterface $validator
25
     */
26
    public function __construct(ValidatorInterface $validator)
27
    {
28
        $this->validator = $validator;
29
    }
30
31
    /**
32
     * @param mixed    $message
33
     * @param callable $next
34
     *
35
     * @return mixed
36
     */
37
    public function handle($message, callable $next)
38
    {
39
        $violations = $this->validator->validate($message);
40
41
        if (count($violations)) {
42
            throw new InvalidMessageException($violations);
43
        }
44
45
        return $next($message);
46
    }
47
}
48