Completed
Push — master ( e600ef...7ac924 )
by Basenko
07:00
created

TemplatableField   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 43
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getTemplatePath() 0 6 1
A getNestingLevel() 0 4 1
A parseValue() 0 14 4
1
<?php
2
3
namespace MadWeb\Seoable\Fields;
4
5
use Illuminate\Container\Container;
6
7
abstract class TemplatableField extends Field
8
{
9
    /** @var string */
10
    protected $templateKey;
11
12
    /** @var \Illuminate\Contracts\Translation\Translator */
13
    protected $translator;
14
15 9
    public function __construct($value, $model, string $templateKey = '')
16
    {
17 9
        $this->templateKey = $templateKey;
18 9
        $this->translator = Container::getInstance()->make('translator');
19
20 9
        parent::__construct($value, $model);
21 9
    }
22
23 9
    protected function parseValue($value): string
24
    {
25 9
        $nesting_level = $this->templateKey ?: $this->getNestingLevel();
26
27 9
        $template_path = $this->getTemplatePath(
28 9
            get_class($this->model).
29 9
            ($nesting_level ? '.'.$nesting_level : '')
30
        );
31
32 9
        return $this->translator->has($template_path) ? $this->translator->trans(
33 9
            $template_path,
34 9
            $this->parseAttributesWithKeys($value)
35 9
        ) : $this->model->getAttribute($value);
36
    }
37
38 9
    protected function getTemplatePath(string $templateKey): string
39
    {
40 9
        return config('seoable.templates_path').
41 9
            '.'.$templateKey.'.'.
42 9
            mb_strtolower(class_basename($this));
43
    }
44
45 3
    protected function getNestingLevel(): string
46
    {
47 3
        return '';
48
    }
49
}
50