Completed
Pull Request — master (#49)
by John
08:22
created

VndValidationErrorFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 3
c 3
b 0
f 1
lcom 0
cbo 4
dl 0
loc 29
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A create() 0 12 2
1
<?php
2
/*
3
 * This file is part of the KleijnWeb\SwaggerBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\SwaggerBundle\Response;
10
11
use KleijnWeb\SwaggerBundle\Exception\InvalidParametersException;
12
use Ramsey\VndError\VndError;
13
use Symfony\Component\HttpFoundation\Request;
14
15
/**
16
 * @author John Kleijn <[email protected]>
17
 */
18
class VndValidationErrorFactory
19
{
20
    const DEFAULT_MESSAGE = 'Input Validation Failure';
21
22
23
    public function __construct(){
24
25
    }
26
27
    /**
28
     * @param Request                    $request
29
     * @param InvalidParametersException $exception
30
     * @param string|null                $logRef
31
     *
32
     * @return VndError
33
     */
34
    public function create(Request $request, InvalidParametersException $exception, $logRef = null)
35
    {
36
        $vndError = new VndError(self::DEFAULT_MESSAGE, $logRef);
37
        $vndError->addLink('help', $request->attributes->get('_resource'), ['title' => 'Error Information']);
38
        $vndError->addLink('about', $request->getUri(), ['title' => 'Error Information']);
39
40
        foreach ($exception->getValidationErrors() as $errorSpec) {
41
            $vndError->addResource($errorSpec['property'], new VndError($errorSpec['message']));
42
        }
43
44
        return $vndError;
45
    }
46
}
47