Passed
Pull Request — master (#21)
by Maximo
04:23
created

UserCompanyAppsActivities::getSource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Gewaer\Models;
5
6
use Gewaer\Exception\ServerErrorHttpException;
7
use Gewaer\Exception\ModelException;
8
use Phalcon\Di;
9
10
/**
11
 * Classs for UserCompanyAppsActivities
12
 * @property Users $userData
13
 * @property Request $request
14
 * @property Config $config
15
 * @property Apps $app
16
 * @property \Phalcon\DI $di
17
 *
18
 */
19
class UserCompanyAppsActivities extends AbstractModel
20
{
21
    /**
22
     *
23
     * @var integer
24
     */
25
    public $company_id;
26
27
    /**
28
     *
29
     * @var integer
30
     */
31
    public $company_branches_id;
32
33
    /**
34
     *
35
     * @var integer
36
     */
37
    public $apps_id;
38
39
    /**
40
     *
41
     * @var string
42
     */
43
    public $key;
44
45
    /**
46
     *
47
     * @var integer
48
     */
49
    public $value;
50
51
    /**
52
     *
53
     * @var string
54
     */
55
    public $created_at;
56
57
    /**
58
     *
59
     * @var string
60
     */
61
    public $updated_at;
62
63
    /**
64
     *
65
     * @var integer
66
     */
67
    public $is_deleted;
68
69
    /**
70
     * Initialize method for model.
71
     */
72 6
    public function initialize()
73
    {
74 6
        $this->belongsTo(
75 6
            'company_id',
76 6
            'Gewaer\Models\Companies',
77 6
            'id',
78 6
            ['alias' => 'company']
79
        );
80
81 6
        $this->belongsTo(
82 6
            'apps_id',
83 6
            'Gewaer\Models\Apps',
84 6
            'id',
85 6
            ['alias' => 'app']
86
        );
87
88 6
        $this->belongsTo(
89 6
            'company_branches_id',
90 6
            'Gewaer\Models\CompanyBranches',
91 6
            'id',
92 6
            ['alias' => 'companyBranch']
93
        );
94
95 6
        $this->setSource('user_company_apps_activities');
96 6
    }
97
98
    /**
99
     * Returns table name mapped in the model.
100
     *
101
     * @return string
102
     */
103 6
    public function getSource() : string
104
    {
105 6
        return 'user_company_apps_activities';
106
    }
107
108
    /**
109
     * Get the value of the settins by it key
110
     *
111
     * @param string $key
112
     * @param string $value
113
     */
114 3
    public static function get(string $key) : string
115
    {
116 3
        $setting = self::findFirst([
117 3
            'conditions' => 'company_id = ?0 and apps_id = ?1 and key = ?2',
118 3
            'bind' => [Di::getDefault()->getUserData()->default_company, Di::getDefault()->getApp()->getId(), $key]
119
        ]);
120
121 3
        if (is_object($setting)) {
122 3
            return $setting->value;
123
        }
124
125
        throw new ServerErrorHttpException(_('No settings found with this ' . $key));
126
    }
127
128
    /**
129
     * Set a setting for the given app
130
     *
131
     * @param string $key
132
     * @param string $value
133
     */
134 2
    public static function set(string $key, $value) : bool
135
    {
136 2
        $activity = self::findFirst([
137 2
            'conditions' => 'company_id = ?0 and apps_id = ?1 and key = ?2',
138 2
            'bind' => [Di::getDefault()->getUserData()->default_company, Di::getDefault()->getApp()->getId(), $key]
139
        ]);
140
141 2
        if (!is_object($activity)) {
142 2
            $activity = new self();
143 2
            $activity->company_id = Di::getDefault()->getUserData()->default_company;
144 2
            $activity->company_branches_id = Di::getDefault()->getUserData()->default_company_branch;
145 2
            $activity->apps_id = Di::getDefault()->getApp()->getId();
146 2
            $activity->key = $key;
147
        }
148
149 2
        $activity->value = $value;
0 ignored issues
show
Documentation Bug introduced by
The property $value was declared of type integer, but $value is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
150
151 2
        if (!$activity->save()) {
152
            throw new ModelException((string)current($activity->getMessages()));
153
        }
154
155 2
        return true;
156
    }
157
}
158