TemplatableField::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 12
    public function __construct($value, $model, string $templateKey = '')
16
    {
17 12
        $this->templateKey = $templateKey;
18 12
        $this->translator = Container::getInstance()->make('translator');
19
20 12
        parent::__construct($value, $model);
21 12
    }
22
23 12
    protected function parseValue($value): string
24
    {
25 12
        $nesting_level = $this->templateKey ?: $this->getNestingLevel();
26
27 12
        $template_path = $this->getTemplatePath(
28 12
            get_class($this->model).
29 12
            ($nesting_level ? '.'.$nesting_level : '')
30
        );
31
32 12
        return $this->translator->has($template_path) ? $this->translator->get(
33 12
            $template_path,
34 12
            $this->parseAttributesWithKeys($value)
35 12
        ) : $this->model->getAttribute($value);
36
    }
37
38 12
    protected function getTemplatePath(string $templateKey): string
39
    {
40 12
        return config('seoable.templates_path').
41 12
            '.'.$templateKey.'.'.
42 12
            mb_strtolower(class_basename($this));
43
    }
44
45 6
    protected function getNestingLevel(): string
46
    {
47 6
        return '';
48
    }
49
}
50