Completed
Push — master ( a0b016...728b96 )
by Stéphane
24:07
created

Translation::setTranslation()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 3
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
1
<?php namespace Rocket\Translation\Model;
2
3
use Carbon\Carbon;
4
use Eloquent;
5
6
/**
7
 * @property integer $id
8
 * @property integer $string_id
9
 * @property integer $language_id
10
 * @property DateTime $date_edition
11
 * @property string $text
12
 */
13
class Translation extends Eloquent
14
{
15
    /**
16
     * @var bool Indicates if the model should be timestamped.
17
     */
18
    public $timestamps = false;
19
20
    /**
21
     * @var string The table associated with the model.
22
     */
23
    protected $table = 'translations';
24
25
    /**
26
     * The attributes that should be mutated to dates.
27
     *
28
     * @var array
29
     */
30
    protected $dates = ['date_edition'];
31
32
    public static function setTranslation($stringId, $languageId, $text) {
33
        $translation = Translation::where('string_id', $stringId)->where('language_id', $languageId)->first();
34
35
        if (!$translation) {
36
            $translation = new Translation();
37
            $translation->string_id = $stringId;
38
            $translation->language_id = $languageId;
39
        }
40
41
        $translation->text = $text;
42
43
        if ($translation->isDirty()) {
44
            $translation->date_edition = Carbon::now();
45
        }
46
47
        $translation->save();
48
49
        return $translation;
50
    }
51
52
    protected static function getTranslation($stringId, $languageId)
53
    {
54
        return Translation::where('string_id', $stringId)->where('language_id', $languageId)->value('text');
55
    }
56
}
57