Completed
Push — master ( b18aac...90de4e )
by Sebastian
03:53
created

TranslationLoader::fragmentsAreAvailable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace App\Services\Locale;
4
5
use App\Models\Fragment;
6
use Cache;
7
use Illuminate\Translation\FileLoader;
8
use Schema;
9
10
class TranslationLoader extends FileLoader
11
{
12
    /**
13
     * Load the messages for the given locale.
14
     *
15
     * @param  string $locale
16
     * @param  string $group
17
     * @param  string $namespace
18
     *
19
     * @return array
20
     */
21
    public function load($locale, $group, $namespace = null) : array
22
    {
23
        if (!is_null($namespace) && $namespace !== '*') {
24
            return $this->loadNamespaced($locale, $group, $namespace);
25
        }
26
27
        if (!$this->fragmentsAreAvailable()) {
28
            return [];
29
        }
30
31
        return Cache::rememberForever("locale.fragments.{$locale}.{$group}", function () use ($group, $locale) {
32
            return $this->fetchFragments($group, $locale);
33
        });
34
    }
35
36
    protected function fragmentsAreAvailable() : bool
37
    {
38
        try {
39
            return Schema::hasTable('fragments');
40
        } catch (\Exception $e) {
41
            return false;
42
        }
43
    }
44
45
    protected function fetchFragments(string $group, string $locale) : array
46
    {
47
        return Fragment::query()
48
            ->where('name', 'LIKE', "{$group}.%")
49
            ->get()
50
            ->map(function (Fragment $fragment) use ($locale, $group) {
51
                return [
52
                    'key' => str_replace("{$group}.", '', $fragment->name),
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<App\Models\Fragment>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
53
                    'text' => $fragment->translate($locale)->text,
54
                ];
55
            })
56
            ->pluck('text', 'key')
57
            ->toArray();
58
    }
59
}
60