Observer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isDirty() 0 4 1
A saved() 0 10 3
1
<?php
2
3
namespace RichanFongdasen\I18n\Eloquent;
4
5
use RichanFongdasen\I18n\Contracts\TranslateableModel;
6
7
class Observer
8
{
9
    /**
10
     * Check if the translation model is dirty.
11
     *
12
     * @param \RichanFongdasen\I18n\Eloquent\TranslationModel $model
13
     *
14
     * @return bool
15
     */
16
    protected function isDirty(TranslationModel $model): bool
17
    {
18
        return $model->isDirty();
19
    }
20
21
    /**
22
     * Listening to any saved events.
23
     *
24
     * @param \RichanFongdasen\I18n\Contracts\TranslateableModel $model
25
     *
26
     * @return void
27
     */
28
    public function saved(TranslateableModel $model): void
29
    {
30
        foreach ($model->getAttribute('translations') as $translation) {
31
            if ($this->isDirty($translation)) {
32
                $translation->setAttribute($model->getForeignKey(), $model->getKey())
33
                    ->setTable($model->getTranslationTable())
34
                    ->save();
35
            }
36
        }
37
    }
38
}
39