Test Failed
Push — master ( bfaa9c...b480ce )
by Terzi
11:11
created

Translatable::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
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
/**
12
 * Class Translatable
13
 *
14
 * @package Terranet\Administrator\Field
15
 * @method switchTo(string $className)
16
 * @method tinymce()
17
 * @method ckeditor()
18
 * @method markdown()
19
 * @method medium()
20
 * @method hideLabel()
21
 * @method sortable(\Closure $callback = null)
22
 * @method disableSorting()
23
 */
24
class Translatable
25
{
26
    /** @var Generic */
27
    protected $field;
28
29
    /**
30
     * Translatable constructor.
31
     *
32
     * @param Generic $field
33
     */
34
    protected function __construct(Generic $field)
35
    {
36
        $this->field = $field;
37
    }
38
39
    /**
40
     * @param Generic $field
41
     * @return Translatable
42
     */
43
    public static function make(Generic $field)
44
    {
45
        return new static($field);
46
    }
47
48
    /**
49
     * Proxy field methods calls.
50
     *
51
     * @param $method
52
     * @param $args
53
     */
54
    public function __call($method, $args)
55
    {
56
        if (method_exists($this->field, $method)) {
57
            return call_user_func_array([$this->field, $method], $args);
58
        }
59
    }
60
61
    /**
62
     * @param string $page
63
     * @return mixed
64
     */
65
    public function render(string $page = 'index')
66
    {
67
        if ($this->field->hasFormat()) {
68
            // Each Field can define its own data for custom formatter.
69
            $withData = method_exists($this, 'renderWith')
70
                ? $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

70
                ? $this->/** @scrutinizer ignore-call */ renderWith()
Loading history...
71
                : [$this->field->value(), $this->field->getModel()];
72
73
            return $this->field->callFormatter($withData);
74
        }
75
76
        $data = [
77
            'field' => $this->field,
78
            'model' => $this->field->getModel(),
79
        ];
80
81
        if (method_exists($this, $dataGetter = 'on'.title_case($page))) {
82
            $data += call_user_func([$this, $dataGetter]);
83
        }
84
85
        return View::make('administrator::fields.translatable.'.$page, $data);
86
    }
87
88
    /**
89
     * @return array
90
     */
91
    protected function onEdit(): array
92
    {
93
        return [
94
            'languages' => \localizer\locales(),
95
            'locale' => \localizer\locale(),
96
            'container' => $this,
97
            'translations' => app('scaffold.translations'),
98
        ];
99
    }
100
101
    /**
102
     * @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...
103
     */
104
    public function name(Locale $language)
105
    {
106
        return "translatable[{$language->id()}][{$this->field->id()}]";
107
    }
108
109
    /**
110
     * @param Locale $language
111
     */
112
    public function value(Locale $language)
113
    {
114
        $model = $this->field->getModel();
115
        $entity = $model->translate($language->id());
116
117
        return $entity ? $entity->getAttribute($this->field->id()) : null;
118
    }
119
}