Test Failed
Push — master ( d27946...98f74e )
by Terzi
08:07
created

RendersTranslatableElement::translatableName()   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\Traits\Form;
4
5
use Terranet\Administrator\Scaffolding;
6
use Terranet\Translatable\Translatable;
7
8
trait RendersTranslatableElement
9
{
10
    /**
11
     * @return mixed
12
     */
13
    public function render($page = Scaffolding::PAGE_INDEX)
0 ignored issues
show
Unused Code introduced by
The parameter $page is not used and could be removed. ( Ignorable by Annotation )

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

13
    public function render(/** @scrutinizer ignore-unused */ $page = Scaffolding::PAGE_INDEX)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
14
    {
15
        /**
16
         * to be able using translations we have to get element's Eloquent model.
17
         *
18
         * @var \Terranet\Translatable\Translatable;
19
         */
20
        $repository = app('scaffold.model') ?: app('scaffold.module')->model();
0 ignored issues
show
Bug introduced by
The method model() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

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

20
        $repository = app('scaffold.model') ?: app('scaffold.module')->/** @scrutinizer ignore-call */ model();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
21
22
        $cycle = 0;
23
        $current = \localizer\locale();
24
        $translations = app('scaffold.translations');
25
26
        $inputs = array_build(
27
            \localizer\locales(),
0 ignored issues
show
Bug introduced by
locales() of type Illuminate\Support\Collection is incompatible with the type array expected by parameter $array of array_build(). ( Ignorable by Annotation )

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

27
            /** @scrutinizer ignore-type */ \localizer\locales(),
Loading history...
28
            function ($key, $locale) use ($repository, $current, &$cycle, $translations) {
29
                $element = $this->selfClone($locale, $repository);
30
31
                if ($translations->readonly($locale)) {
32
                    $element->setAttributes(['disabled' => true]);
33
                }
34
35
                $input = $element->render();//.$element->errors();
36
37
                $input = view(
38
                    'administrator::partials.forms.translatable.element',
39
                    [
40
                        'element' => $element,
41
                        'locale' => $locale,
42
                        'current' => $current,
43
                        'input' => $input,
44
                    ]
45
                );
46
47
                return [$cycle++, $input];
48
            }
49
        );
50
51
        return view('administrator::partials.forms.translatable.container')
52
            ->with([
53
                'element' => $this,
54
                'inputs' => $inputs,
55
                'current' => \localizer\locale(),
56
            ]);
57
    }
58
59
    /**
60
     * @param $locale
61
     * @param $repository
62
     *
63
     * @return RendersTranslatableElement
64
     */
65
    protected function selfClone($locale, $repository)
66
    {
67
        $element = clone $this->field;
68
69
        $element->setName(
70
            $this->translatableName($locale)
71
        );
72
73
        // set translated value
74
        if ($repository instanceof Translatable && $repository->hasTranslation($locale->id())) {
75
            $element->setValue(
76
                $repository->translate($locale->id())->{$this->id()}
0 ignored issues
show
Bug introduced by
It seems like id() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

76
                $repository->translate($locale->id())->{$this->/** @scrutinizer ignore-call */ id()}
Loading history...
77
            );
78
        }
79
80
        return $element;
81
    }
82
83
    /**
84
     * @param $locale
85
     *
86
     * @return string
87
     */
88
    protected function translatableName($locale)
89
    {
90
        return "translatable[{$locale->id()}][{$this->id()}]";
91
    }
92
}
93