Passed
Push — master ( 868a85...47fe80 )
by Morten Poul
03:20
created

Translation   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 10
c 3
b 0
f 0
dl 0
loc 42
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A scopeForLang() 0 3 1
A translatable() 0 3 1
A __construct() 0 7 2
1
<?php
2
3
namespace Signifly\Translator\Models;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Relations\MorphTo;
8
use Signifly\Translator\Contracts\Translation as TranslationContract;
9
10
class Translation extends Model implements TranslationContract
11
{
12
    /**
13
     * The attributes that are mass assignable.
14
     *
15
     * @var array
16
     */
17
    protected $fillable = [
18
        'language_code',
19
        'key',
20
        'value',
21
    ];
22
23
    public function __construct(array $attributes = [])
24
    {
25
        if (! isset($this->table)) {
26
            $this->setTable(config('translator.table_name'));
27
        }
28
29
        parent::__construct($attributes);
30
    }
31
32
    /**
33
     * The associated translatable relation.
34
     *
35
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
36
     */
37
    public function translatable(): MorphTo
38
    {
39
        return $this->morphTo();
40
    }
41
42
    /**
43
     * Scope a query to only include translations for a given language.
44
     *
45
     * @param  \Illuminate\Database\Eloquent\Builder $query
46
     * @param  string  $langCode
47
     * @return \Illuminate\Database\Eloquent\Builder
48
     */
49
    public function scopeForLang(Builder $query, string $langCode): Builder
50
    {
51
        return $query->where('language_code', $langCode);
52
    }
53
}
54