TableSettingsManager   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 15
c 2
b 0
f 0
dl 0
loc 30
ccs 13
cts 13
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A apply() 0 22 4
1
<?php
2
3
namespace Glorand\Model\Settings\Managers;
4
5
use Glorand\Model\Settings\Contracts\SettingsManagerContract;
6
use Glorand\Model\Settings\Models\ModelSettings;
7
8
/**
9
 * Class TableSettingsManager
10
 * @package Glorand\Model\Settings\Managers
11
 * @property  \Illuminate\Database\Eloquent\Model|\Glorand\Model\Settings\Traits\HasSettingsTable $model
12
 */
13
class TableSettingsManager extends AbstractSettingsManager
14
{
15
    /**
16
     * @param  array  $settings
17
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
18
     * @throws \Exception
19
     * @SuppressWarnings(PHPMD.ElseExpression)
20 39
     */
21
    public function apply(array $settings = []): SettingsManagerContract
22 39
    {
23 39
        $this->validate($settings);
24 9
25 9
        $modelSettings = $this->model->modelSettings()->first();
26
        if (!count($settings)) {
27
            if ($modelSettings) {
28 39
                $modelSettings->delete();
29 39
            }
30 39
        } else {
31 39
            if (!$modelSettings) {
32
                $modelSettings = new ModelSettings();
33 39
                $modelSettings->setConnection($this->model->getConnectionName());
0 ignored issues
show
Bug introduced by
The method getConnectionName() does not exist on Glorand\Model\Settings\Traits\HasSettingsTable. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
                $modelSettings->setConnection($this->model->/** @scrutinizer ignore-call */ getConnectionName());
Loading history...
Bug introduced by
It seems like $this->model->getConnectionName() can also be of type Glorand\Model\Settings\C...SettingsManagerContract; however, parameter $name of Illuminate\Database\Eloq...\Model::setConnection() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
                $modelSettings->setConnection(/** @scrutinizer ignore-type */ $this->model->getConnectionName());
Loading history...
34 39
                $modelSettings->model()->associate($this->model);
35
            }
36
            $modelSettings->settings = $settings;
37 39
            $modelSettings->save();
38
        }
39 39
40
        cache()->forget($this->model->getSettingsCacheKey());
0 ignored issues
show
Bug introduced by
It seems like $this->model->getSettingsCacheKey() can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $key of Illuminate\Cache\Repository::forget() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
        cache()->forget(/** @scrutinizer ignore-type */ $this->model->getSettingsCacheKey());
Loading history...
41
42
        return $this;
43
    }
44
}
45