Test Failed
Push — master ( 98f74e...fae1a9 )
by Terzi
04:51
created

Translatable::value()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Terranet\Administrator\Field;
4
5
use Illuminate\Support\Facades\View;
6
use League\Flysystem\Adapter\Local;
7
use function localizer\locale;
8
use Terranet\Administrator\Exception;
9
use Terranet\Localizer\Locale;
10
11
class Translatable
12
{
13
    protected $field;
14
15
    /**
16
     * Translatable constructor.
17
     *
18
     * @param Generic $field
19
     */
20
    public function __construct(Generic $field)
21
    {
22
        $this->field = $field;
23
    }
24
25
    /**
26
     * Proxy field methods calls.
27
     *
28
     * @param $method
29
     * @param $args
30
     */
31
    public function __call($method, $args)
32
    {
33
        if (method_exists($this->field, $method)) {
34
            return call_user_func_array([$this->field, $method], $args);
35
        }
36
37
        throw new Exception(sprintf('Unknown method [%s]', $method));
38
    }
39
40
    /**
41
     * @param string $page
42
     * @return mixed
43
     */
44
    public function render(string $page = 'index')
45
    {
46
        if ($this->field->hasFormat()) {
47
            // Each Field can define its own data for custom formatter.
48
            $withData = method_exists($this, 'renderWith')
49
                ? $this->renderWith()
0 ignored issues
show
Bug introduced by
The method renderWith() does not exist on Terranet\Administrator\Field\Translatable. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
                ? $this->/** @scrutinizer ignore-call */ renderWith()
Loading history...
50
                : [$this->field->value(), $this->field->getModel()];
51
52
            return $this->field->callFormatter($withData);
53
        }
54
55
        $data = [
56
            'field' => $this->field,
57
            'model' => $this->field->getModel(),
58
        ];
59
60
        if (method_exists($this, $dataGetter = 'on'.title_case($page))) {
61
            $data += call_user_func([$this, $dataGetter]);
62
        }
63
64
        return View::make('administrator::fields.translatable.'.$page, $data);
65
    }
66
67
    /**
68
     * @return array
69
     */
70
    protected function onEdit(): array
71
    {
72
        return [
73
            'languages' => \localizer\locales(),
74
            'locale' => \localizer\locale(),
75
            'container' => $this,
76
            'translations' => app('scaffold.translations'),
77
        ];
78
    }
79
80
    /**
81
     * @param Language $language
0 ignored issues
show
Bug introduced by
The type Terranet\Administrator\Field\Language was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
82
     */
83
    public function name(Locale $language)
84
    {
85
        return "translatable[{$language->id()}][{$this->field->id()}]";
86
    }
87
88
    /**
89
     * @param Locale $language
90
     */
91
    public function value(Locale $language)
92
    {
93
        $model = $this->field->getModel();
94
95
        return $model->translate($language->id())->{$this->field->id()};
96
    }
97
}