Test Setup Failed
Push — v2 ( 74186a...c94b75 )
by Alexander
06:56
created

ErrorMessageResolver   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resolve() 0 8 2
1
<?php
2
3
namespace Flugg\Responder;
4
5
use Flugg\Responder\Contracts\ErrorMessageResolver as ErrorMessageResolverContract;
6
use Illuminate\Contracts\Translation\Translator;
7
8
/**
9
 * A resolver class responsible for resolving messages from error codes.
10
 *
11
 * @package flugger/laravel-responder
12
 * @author  Alexander Tømmerås <[email protected]>
13
 * @license The MIT License
14
 */
15
class ErrorMessageResolver implements ErrorMessageResolverContract
16
{
17
    /**
18
     * A serivce for resolving messages from language files.
19
     *
20
     * @var \Illuminate\Contracts\Translation\Translator
21
     */
22
    protected $translator;
23
24
    /**
25
     * Construct the resolver class.
26
     *
27
     * @param \Illuminate\Contracts\Translation\Translator $translator
28
     */
29
    public function __construct(Translator $translator)
30
    {
31
        $this->translator = $translator;
32
    }
33
34
    /**
35
     * Resolve a message from the given error code.
36
     *
37
     * @param  string $errorCode
38
     * @return string|null
39
     */
40
    public function resolve(string $errorCode)
41
    {
42
        if (! $this->translator->has($errorCode = "errors.$errorCode")) {
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $errorCode instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
43
            return null;
44
        }
45
46
        return $this->translator->trans($errorCode);
47
    }
48
}