Test Failed
Pull Request — master (#53)
by Maximo
04:46
created

ModelSettingsTrait::getPrimaryKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 8
ccs 3
cts 3
cp 1
crap 1
rs 10
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
     * @param AbstractModel $model
0 ignored issues
show
Bug introduced by
The type Gewaer\Traits\AbstractModel was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
     * @return void
32
     */
33 1
    private function createSettingsModel(): void
34
    {
35 1
        $class = get_class($this) . 'Settings';
36
37 1
        $this->settingsModel = new $class();
38 1
    }
39
40
    /**
41
     * Get the primary key of this model, this will only work on model with just 1 primary key
42
     *
43
     * @return string
44
     */
45 1
    private function getPrimaryKey(): string
46
    {
47
        // Get the first matching primary key.
48
        // @TODO This will hurt on compound primary keys.
49 1
        $metaData = new \Phalcon\Mvc\Model\MetaData\Memory();
50
        // Get the first matching primary key.
51
        // @TODO This will hurt on compound primary keys.
52 1
        return $this->getSource() . '_' . $metaData->getPrimaryKeyAttributes($this)[0];
2 ignored issues
show
Bug introduced by
$this of type Gewaer\Traits\ModelSettingsTrait is incompatible with the type Phalcon\Mvc\ModelInterface expected by parameter $model of Phalcon\Mvc\Model\MetaDa...tPrimaryKeyAttributes(). ( Ignorable by Annotation )

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

52
        return $this->getSource() . '_' . $metaData->getPrimaryKeyAttributes(/** @scrutinizer ignore-type */ $this)[0];
Loading history...
Bug introduced by
It seems like getSource() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

52
        return $this->/** @scrutinizer ignore-call */ getSource() . '_' . $metaData->getPrimaryKeyAttributes($this)[0];
Loading history...
53
    }
54
55
    /**
56
     * Set the settings
57
     *
58
     * @param string $key
59
     * @param string $value
60
     * @return boolean
61
     */
62 1
    public function setSettings(string $key, $value) : bool
63
    {
64 1
        $this->createSettingsModel();
65
66 1
        if (!is_object($this->settingsModel)) {
67
            throw new ServerErrorHttpException('ModelSettingsTrait need to have a settings mdoel configure, check the model setting existe for this class' . get_class($this));
68
        }
69
70
        //setup the user notificatoin setting
71 1
        $this->settingsModel->{$this->getPrimaryKey()} = $this->getId();
72 1
        $this->settingsModel->name = $key;
73 1
        $this->settingsModel->value = $value;
74
75 1
        if (!$this->settingsModel->save()) {
76 1
            throw new ModelException((string)current($this->settingsModel->getMessages()));
77
        }
78
79 1
        return true;
80
    }
81
82
    /**
83
     * Get the settings base on the key
84
     *
85
     * @param string $key
86
     * @return void
87
     */
88
    public function getSettings(string $key): ?string
89
    {
90
        $this->createSettingsModel();
91
        $value = $this->settingsModel->findFirst([
92
            'conditions' => "{$this->getPrimaryKey()} = ?0 and name = ?1",
93
            'bind' => [$this->getId(), $key]
94
        ]);
95
96
        if (is_object($value)) {
97
            return $value->value;
98
        }
99
100
        return null;
101
    }
102
}
103