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

StringModel   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getStringId() 0 3 1
A getOrCreateTranslation() 0 10 2
A createString() 0 11 1
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