Translation::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
namespace BBSLab\NovaTranslation\Fields;
4
5
use BBSLab\NovaTranslation\Models\Contracts\IsTranslatable;
6
use BBSLab\NovaTranslation\Models\Locale;
7
use BBSLab\NovaTranslation\Models\Translation as TranslationModel;
8
use BBSLab\NovaTranslation\NovaTranslation;
9
use BBSLab\NovaTranslation\NovaTranslationServiceProvider;
10
use Laravel\Nova\Fields\Field;
11
12
class Translation extends Field
13
{
14
    /**
15
     * The field's component.
16
     *
17
     * @var string
18
     */
19
    public $component = 'nova-translation-field';
20
21
    public function __construct($name, $attribute = null, callable $resolveCallback = null)
22
    {
23
        $name = trans(NovaTranslationServiceProvider::PACKAGE_ID.'::lang.field');
24
        $attribute = 'translation';
25
26
        parent::__construct($name, $attribute, $resolveCallback);
27
28
        $this->withMeta([
29
            'locales' => $this->locales(),
30
        ]);
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function resolve($resource, $attribute = null)
37
    {
38
        $this->withMeta([
39
            'translations' => $resource instanceof IsTranslatable ? $this->translations($resource) : [],
40
        ]);
41
42
        parent::resolve($resource, $attribute);
43
    }
44
45
    /**
46
     * Return all indexed locales.
47
     *
48
     * @return array
49
     * @throws \Exception
50
     */
51
    protected function locales()
52
    {
53
        return NovaTranslation::locales()->mapWithKeys(function (Locale $locale) {
54
            return [$locale->getKey() => $locale->toArray()];
55
        })->toArray();
56
    }
57
58
    /**
59
     * Return translations entries for given translatable model.
60
     *
61
     * @param  \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable  $resource
62
     * @return array
63
     */
64
    protected function translations(IsTranslatable $resource)
65
    {
66
        return $resource->translations->mapWithKeys(function (TranslationModel $translation) {
0 ignored issues
show
Bug introduced by
Accessing translations on the interface BBSLab\NovaTranslation\M...ontracts\IsTranslatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
67
            return [
68
                $translation->locale_id => $translation->toArray(),
69
            ];
70
        })->toArray();
71
    }
72
}
73