SettingRepository::getValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace App\Repositories;
4
5
use App\Exceptions\RepositoryException;
6
7
class SettingRepository extends Repository {
8
9
    const STOCK_EMPTY_ALERT = 'stock_empty_alert';
10
11
    public function getModelName()
12
    {
13
        return 'App\Models\Settings';
14
    }
15
16 View Code Duplication
    public function get($setting_name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
    {
18
        $this->validateName($setting_name);
19
20
        try
21
        {
22
            $settingRecord = $this->model->where('setting_name', '=', $setting_name)->firstOrFail();
23
        }
24
        catch(\Exception $e)
25
        {
26
            throw new RepositoryException('Could not retrieve setting '.$setting_name, RepositoryException::DATABASE_ERROR);
27
        }
28
29
        return $settingRecord;
30
    }
31
32
    public function getValue($setting_name)
33
    {
34
        return $this->get($setting_name)->setting_value;
35
    }
36
37
    private function validateName($setting_name)
38
    {
39
        if( !preg_match('/^[a-z0-9_]+$/', $setting_name) )
40
        {
41
            throw new RepositoryException('Setting name is invalid', RepositoryException::VALIDATION_FAILED);
42
        }
43
    }
44
45
}