Completed
Push — master ( bf8c2d...965adb )
by Fèvre
10s
created

SettingsTable   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 91
Duplicated Lines 16.48 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 15
loc 91
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 15 15 1
A setSettings() 0 20 3
A beforeSave() 0 4 1
B _setValue() 0 18 9

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace App\Model\Table;
3
4
use Cake\Core\Configure;
5
use Cake\Event\Event;
6
use Cake\ORM\Entity;
7
use Cake\ORM\Table;
8
9
class SettingsTable extends Table
10
{
11
12
    /**
13
     * Initialize method.
14
     *
15
     * @param array $config The configuration for the Table.
16
     *
17
     * @return void
18
     */
19 View Code Duplication
    public function initialize(array $config)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
    {
21
        parent::initialize($config);
22
23
        $this->table('settings');
24
        $this->displayField('name');
25
        $this->primaryKey('id');
26
27
        $this->addBehavior('Timestamp');
28
29
        $this->belongsTo('LastUpdatedUser', [
30
            'className' => 'Users',
31
            'foreignKey' => 'last_updated_user_id'
32
        ]);
33
    }
34
35
    /**
36
     * Set the settings from the database.
37
     *
38
     * @return void
39
     */
40
    public function setSettings()
41
    {
42
        $settings = $this->find()
43
            ->select([
44
                'id',
45
                'name',
46
                'value_int',
47
                'value_str',
48
                'value_bool'
49
            ])
50
            ->cache('settings', 'database');
51
52
        if (empty($settings)) {
53
            return;
54
        }
55
56
        foreach ($settings as $setting) {
57
            Configure::write($setting->name, $setting->value);
58
        }
59
    }
60
61
    /**
62
     * Handle the beforeSave event.
63
     *
64
     * @param \Cake\Event\Event $event The beforeSave event that was fired.
65
     * @param \Cake\ORM\Entity $entity The entity that is going to be saved.
66
     *
67
     * @return void
68
     */
69
    public function beforeSave(Event $event, Entity $entity)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
70
    {
71
        $this->_setValue($entity);
72
    }
73
74
    /**
75
     * Assign the right value for each `value_*` fields regarding to the value.
76
     *
77
     * @param \Cake\ORM\Entity $entity The entity that is going to be saved.
78
     *
79
     * @return \Cake\ORM\Entity
80
     */
81
    protected function _setValue(Entity $entity)
82
    {
83
        if (empty($entity->value_str)) {
84
            $entity->value_str = null;
85
        }
86
87
        if (!is_numeric($entity->value_int)) {
88
            $entity->value_int = null;
89
        }
90
91
        if (is_null($entity->value_str) && is_null($entity->value_int) && $entity->value_bool == false) {
92
            $entity->value_bool = 0;
93
        } elseif ((!is_null($entity->value_str) || !is_null($entity->value_int)) && $entity->value_bool == false) {
94
            $entity->value_bool = null;
95
        }
96
97
        return $entity;
98
    }
99
}
100