Test Failed
Push — master ( a72aaf...813e38 )
by Dominik
03:05
created

ErrorManager   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 72
ccs 0
cts 37
cp 0
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createByMissingModel() 0 10 1
A createByValidationErrors() 0 14 1
A createNotParsable() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\ApiSkeleton\Error;
6
7
use Chubbyphp\Translation\TranslatorInterface;
8
use Chubbyphp\Validation\Error\NestedErrorMessages;
9
10
class ErrorManager
11
{
12
    /**
13
     * @var TranslatorInterface
14
     */
15
    private $translator;
16
17
    /**
18
     * @param TranslatorInterface $translator
19
     */
20
    public function __construct(TranslatorInterface $translator)
21
    {
22
        $this->translator = $translator;
23
    }
24
25
    /**
26
     * @param string $type
27
     * @param array  $arguments
28
     *
29
     * @return Error
30
     */
31
    public function createByMissingModel(string $type, array $arguments): Error
32
    {
33
        return new Error(
34
            Error::SCOPE_RESOURCE,
35
            'missing.model',
36
            'the wished model does not exist',
37
            $type,
38
            $arguments
39
        );
40
    }
41
42
    /**
43
     * @param array  $errors
44
     * @param string $locale
45
     * @param string $scope
46
     * @param string $type
47
     *
48
     * @return Error
49
     */
50
    public function createByValidationErrors(array $errors, string $locale, string $scope, string $type): Error
51
    {
52
        $nestedErrorMessage = new NestedErrorMessages($errors, function (string $key, array $arguments) use ($locale) {
53
            return $this->translator->translate($locale, $key, $arguments);
54
        });
55
56
        return new Error(
57
            $scope,
58
            'validation.errors',
59
            'there are validation errors while validating the model',
60
            $type,
61
            $nestedErrorMessage->getMessages()
62
        );
63
    }
64
65
    /**
66
     * @param string $type
67
     * @param string $contentType
68
     * @param string $body
69
     * @return Error
70
     */
71
    public function createNotParsable(string $type, string $contentType, string $body): Error
72
    {
73
         return new Error(
74
            Error::SCOPE_BODY,
75
            'notparsable',
76
            'request body not parsable',
77
            $type,
78
            ['body' => $body, 'contentType' => $contentType]
79
         );
80
    }
81
}
82