Observer::isDirty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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