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

ErrorMessageResolver::resolve()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
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
}