Completed
Branch develop (a31570)
by Mohamed
08:09 queued 04:45
created

Settings   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 78.57%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 84
ccs 11
cts 14
cp 0.7857
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A loadSettings() 0 15 3
A get() 0 12 4
A isPublicProjectsEnabled() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Model;
13
14
use Illuminate\Database\Eloquent;
15
use Illuminate\Database\Eloquent\Model;
16
17
/**
18
 * User is model class for users
19
 *
20
 * @author Mohamed Alsharaf <[email protected]>
21
 *
22
 * @property int $id
23
 * @property string $name
24
 * @property string $value
25
 * @property string $key
26
 */
27
class Settings extends Model
28
{
29
    const ENABLE = 1;
30
    const DISABLE = 0;
31
32
    /**
33
     * Indicates if the model should be timestamped.
34
     *
35
     * @var bool
36
     */
37
    public $timestamps = false;
38
39
    /**
40
     * The database table used by the model.
41
     *
42
     * @var string
43
     */
44
    protected $table = 'settings';
45
46
    /**
47
     * The attributes that are mass assignable.
48
     *
49
     * @var array
50
     */
51
    protected $fillable = ['key', 'name', 'value'];
52
53
    /**
54
     * Collection of all settings
55
     *
56
     * @var \Illuminate\Database\Eloquent\Collection
57
     */
58
    protected $settings = null;
59
60
    /**
61
     * Load all settings
62
     *
63
     * @return \Illuminate\Database\Eloquent\Collection|boolean
64
     */
65 53
    protected function loadSettings()
66
    {
67 53
        if (null === $this->settings) {
68
            // Skip exception if table does not exists
69
            // This method called from RouteServiceProvider, which is called within command line 'artisan migrate'
70
            // before the table exists.
71
            try {
72 53
                $this->settings = static::all();
73
            } catch (\Exception $e) {
74
                return false;
75
            }
76
        }
77
78 53
        return $this->settings;
79
    }
80
81
    /**
82
     * Returns a setting value
83
     *
84
     * @param $name
85
     * @param mixed|null $default
86
     * @return mixed
87
     */
88 53
    public function get($name, $default = null)
89
    {
90 53
        if ($this->loadSettings()) {
91 53
            foreach ($this->settings as $setting) {
92 53
                if ($setting->key === $name) {
93 53
                    return $setting->value;
94
                }
95
            }
96
        }
97
98
        return $default;
99
    }
100
101
    /**
102
     * Whether or not the public projects enabled
103
     *
104
     * @return bool
105
     */
106 53
    public function isPublicProjectsEnabled()
107
    {
108 53
        return (boolean)$this->get('enable_public_projects') === true;
109
    }
110
}
111