StringModel   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getStringId() 0 4 1
A getOrCreateTranslation() 0 11 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
    {
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