Failed Conditions
Push — master ( 123541...0f9c88 )
by Rafael
03:26 queued 11s
created

ModelSettingsTrait::setSettings()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.072

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 2
dl 0
loc 18
ccs 8
cts 10
cp 0.8
crap 3.072
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gewaer\Traits;
6
7
use Gewaer\Models\Users;
8
use Gewaer\Exception\ServerErrorHttpException;
9
use Gewaer\Exception\ModelException;
10
11
/**
12
 * Trait ResponseTrait
13
 *
14
 * @package Gewaer\Traits
15
 *
16
 * @property Users $user
17
 * @property Config $config
18
 * @property Request $request
19
 * @property Auth $auth
20
 * @property \Phalcon\Di $di
21
 *
22
 */
23
trait ModelSettingsTrait
24
{
25
    protected $settingsModel;
26
27
    /**
28
     * Set the setting model
29
     *
30
     * @return void
31
     */
32 1
    private function createSettingsModel(): void
33
    {
34 1
        $class = get_class($this) . 'Settings';
35
36 1
        $this->settingsModel = new $class();
37 1
    }
38
39
    /**
40
     * Get the primary key of this model, this will only work on model with just 1 primary key
41
     *
42
     * @return string
43
     */
44 1
    private function getPrimaryKey(): string
45
    {
46
        // Get the first matching primary key.
47
        // @TODO This will hurt on compound primary keys.
48 1
        $metaData = new \Phalcon\Mvc\Model\MetaData\Memory();
49
        // Get the first matching primary key.
50
        // @TODO This will hurt on compound primary keys.
51 1
        return $this->getSource() . '_' . $metaData->getPrimaryKeyAttributes($this)[0];
52
    }
53
54
    /**
55
     * Set the settings
56
     *
57
     * @param string $key
58
     * @param string $value
59
     * @return boolean
60
     */
61 1
    public function setSettings(string $key, $value) : bool
62
    {
63 1
        $this->createSettingsModel();
64
65 1
        if (!is_object($this->settingsModel)) {
66
            throw new ServerErrorHttpException('ModelSettingsTrait need to have a settings mdoel configure, check the model setting existe for this class' . get_class($this));
67
        }
68
69
        //setup the user notificatoin setting
70 1
        $this->settingsModel->{$this->getPrimaryKey()} = $this->getId();
71 1
        $this->settingsModel->name = $key;
72 1
        $this->settingsModel->value = $value;
73
74 1
        if (!$this->settingsModel->save()) {
75
            throw new ModelException((string)current($this->settingsModel->getMessages()));
76
        }
77
78 1
        return true;
79
    }
80
81
    /**
82
     * Get the settings base on the key
83
     *
84
     * @param string $key
85
     * @return void
86
     */
87
    public function getSettings(string $key): ?string
88
    {
89
        $this->createSettingsModel();
90
        $value = $this->settingsModel->findFirst([
91
            'conditions' => "{$this->getPrimaryKey()} = ?0 and name = ?1",
92
            'bind' => [$this->getId(), $key]
93
        ]);
94
95
        if (is_object($value)) {
96
            return $value->value;
97
        }
98
99
        return null;
100
    }
101
}
102