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

StringModel::createString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 11
rs 9.9
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 DateTime $date_creation
9
 * @property string $context
10
 * @property string $string
11
 */
12
class StringModel extends Eloquent
13
{
14
    /**
15
     * @var bool Indicates if the model should be timestamped.
16
     */
17
    public $timestamps = false;
18
19
    /**
20
     * @var string The table associated with the model.
21
     */
22
    protected $table = 'strings';
23
24
    /**
25
     * The attributes that should be mutated to dates.
26
     *
27
     * @var array
28
     */
29
    protected $dates = ['date_creation'];
30
31
    public static function getStringId($context, $keyString) {
32
        return StringModel::where('string', $keyString)->where('context', $context)->value('id');
33
    }
34
35
    public static function getOrCreateTranslation($context, $keyString, $languageId, $defaultLanguageId) {
36
        $stringId = static::getStringId($context, $keyString);
37
        if ($stringId) {
38
            return Translation::getTranslation($stringId, $languageId);
39
        }
40
41
        static::createString($context, $keyString, $defaultLanguageId);
42
43
        return null;
44
    }
45
46
    protected static function createString($context, $text, $defaultLanguageId)
47
    {
48
        $string = new StringModel();
49
        $string->date_creation = Carbon::now();
50
        $string->context = $context;
51
        $string->string = $text;
52
        $string->save();
53
54
        // Insert the app's default language id as default translation;
55
        return Translation::setTranslation($string->id, $defaultLanguageId, $text);
56
    }
57
}
58