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

ErrorManager::createNotParsable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 3
dl 0
loc 10
ccs 0
cts 10
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
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