SettingsTable::setSettings()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 13
nc 3
nop 0
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');
0 ignored issues
show
Deprecated Code introduced by
The method Cake\ORM\Table::table() has been deprecated with message: 3.4.0 Use setTable()/getTable() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
24
        $this->displayField('name');
0 ignored issues
show
Deprecated Code introduced by
The method Cake\ORM\Table::displayField() has been deprecated with message: 3.4.0 Use setDisplayField()/getDisplayField() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
25
        $this->primaryKey('id');
0 ignored issues
show
Deprecated Code introduced by
The method Cake\ORM\Table::primaryKey() has been deprecated with message: 3.4.0 Use setPrimaryKey()/getPrimaryKey() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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