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

UpdatesSeoValues   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B updateSeoValues() 0 32 1
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