UserCompanyAppsActivities   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 142
ccs 0
cts 56
cp 0
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

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