Completed
Pull Request — master (#56)
by Sebastian
06:12
created

UpdatesSeoValues::updateSeoValues()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 19
nc 1
nop 0
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace App\Foundation\Models\Updaters;
4
5
use Illuminate\Support\Collection;
6
use Spatie\Regex\MatchResult;
7
use Spatie\Regex\Regex;
8
9
trait UpdatesSeoValues
10
{
11
    protected function updateSeoValues()
12
    {
13
        collect($this->request->all())
0 ignored issues
show
Bug introduced by
The property request does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
14
            ->filter(function ($value, $fieldName) {
15
                // Filter out everything that starts with 'translated_<locale>_seo_'
16
                return Regex::match('/^translated_([a-z][a-z])_seo_/', $fieldName)->hasMatch();
17
            })
18
            ->map(function ($value, $fieldName) {
19
20
                // Replace 'translated_<locale>_seo_<attribute>' with '<locale>_<attribute>'
21
                $localeAndAttribute = Regex::replace('/translated_([a-z][a-z])_seo_/', function (MatchResult $matchResult) {
22
                    return $matchResult->group(1) . '_';
23
                }, $fieldName)->result();
24
25
                $localeAndAttribute = explode('_', $localeAndAttribute, 2);
26
27
                return [
28
                    'locale' => $localeAndAttribute[0],
29
                    'attribute' => $localeAndAttribute[1],
30
                    'value' => $value,
31
                ];
32
            })
33
            ->groupBy('locale')
34
            ->map(function (Collection $valuesInLocale) {
35
                return $valuesInLocale->mapToAssoc(function ($values) {
36
                    return [$values['attribute'], $values['value']];
37
                });
38
            })
39
            ->each(function ($values, $locale) {
40
                $this->model->setTranslation('seo_values', $locale, $values);
0 ignored issues
show
Bug introduced by
The property model does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
41
            });
42
    }
43
}
44