Completed
Push — master ( d84b0e...706b03 )
by
unknown
05:32
created

TranslatableResource   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 92
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A translations() 0 23 4
A getResourceTranslations() 0 20 3
A indexQuery() 0 4 1
A scoutQuery() 0 4 1
A detailQuery() 0 4 1
A relatableQuery() 0 4 1
1
<?php
2
3
namespace BBSLab\NovaTranslation\Resources;
4
5
use BBSLab\NovaTranslation\Models\Locale;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, BBSLab\NovaTranslation\Resources\Locale.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use BBSLab\NovaTranslation\Models\Translation;
7
use Eminiarts\Tabs\Tabs;
8
use Illuminate\Database\Eloquent\Collection;
9
use Illuminate\Http\Request;
10
use Laravel\Nova\Fields\Text;
11
use Laravel\Nova\Http\Requests\NovaRequest;
12
use Laravel\Nova\Http\Requests\ResourceIndexRequest;
13
use Laravel\Nova\Panel;
14
use Laravel\Nova\Resource;
15
16
abstract class TranslatableResource extends Resource
17
{
18
    /**
19
     * Return tabs of translations for current resource.
20
     *
21
     * @param  \Illuminate\Http\Request  $request
22
     * @param  array  $fields
23
     * @return array
24
     */
25
    protected function translations(Request $request, array $fields)
26
    {
27
        if ($request instanceof ResourceIndexRequest) {
0 ignored issues
show
Bug introduced by
The class Laravel\Nova\Http\Requests\ResourceIndexRequest does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
28
            return $fields;
29
        }
30
31
        $locales = Locale::query()->select('id', 'iso', 'label')->get();
0 ignored issues
show
Bug introduced by
The method select() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean createSelectWithConstraint()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
32
        $translations = $this->getResourceTranslations($locales);
33
34
        $tabs = [];
35
        foreach ($locales as $locale) {
36
            /** @var \BBSLab\NovaTranslation\Models\Locale $locale */
37
            $localeResource = $translations->where('locale_id', '=', $locale->id)->first();
38
            if (! empty($localeResource)) {
39
                $this->resource = clone $localeResource;
40
                $tabs[] = new Panel($locale->iso, (new \ArrayObject($fields))->getArrayCopy());
41
            }
42
        }
43
44
        return [
45
            new Tabs('Translations', $tabs),
46
        ];
47
    }
48
49
    /**
50
     * Return resource translations.
51
     *
52
     * @param  \Illuminate\Database\Eloquent\Collection  $locales
53
     * @return \Illuminate\Database\Eloquent\Collection
54
     */
55
    protected function getResourceTranslations(Collection $locales)
56
    {
57
        $baseTranslation = Translation::query()
0 ignored issues
show
Bug introduced by
The method select() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean createSelectWithConstraint()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
58
            ->select('translation_id')
59
            ->where('translatable_id', '=', $this->resource->getKey())
60
            ->where('translatable_type', '=', get_class($this->resource))
61
            ->first();
62
63
        if (empty($baseTranslation)) {
64
            $translationId = $this->resource->freshTranslationId();
0 ignored issues
show
Unused Code introduced by
$translationId is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
65
            $translations = new Collection();
66
            foreach ($locales as $locale) {
67
                // @TODO... Instantiate new model (maybe with nonTranslatable() already filled in + "locale_id" and "translation_id")
68
            }
69
        } else {
70
            $translations = $this->resource->translations();
71
        }
72
73
        return $translations;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public static function indexQuery(NovaRequest $request, $query)
80
    {
81
        return $query->locale();
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public static function scoutQuery(NovaRequest $request, $query)
88
    {
89
        return $query;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public static function detailQuery(NovaRequest $request, $query)
96
    {
97
        return parent::detailQuery($request, $query);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public static function relatableQuery(NovaRequest $request, $query)
104
    {
105
        return parent::relatableQuery($request, $query);
106
    }
107
}
108