StringModel::getStringId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
rs 10
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
    {
33
        return StringModel::where('string', $keyString)->where('context', $context)->value('id');
34
    }
35
36
    public static function getOrCreateTranslation($context, $keyString, $languageId, $defaultLanguageId)
37
    {
38
        $stringId = static::getStringId($context, $keyString);
39
        if ($stringId) {
40
            return Translation::getTranslation($stringId, $languageId);
41
        }
42
43
        static::createString($context, $keyString, $defaultLanguageId);
44
45
        return null;
46
    }
47
48
    protected static function createString($context, $text, $defaultLanguageId)
49
    {
50
        $string = new StringModel();
51
        $string->date_creation = Carbon::now();
52
        $string->context = $context;
53
        $string->string = $text;
54
        $string->save();
55
56
        // Insert the app's default language id as default translation;
57
        return Translation::setTranslation($string->id, $defaultLanguageId, $text);
58
    }
59
}
60