Passed
Push — master ( 700b0f...aafb0a )
by Björn
18:25 queued 10s
created

SettingsTable::fetchAll()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 13

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 13
loc 13
ccs 0
cts 8
cp 0
rs 9.8333
c 0
b 0
f 0
cc 2
nc 1
nop 1
crap 6
1
<?php
2
/**
3
 * BB's Zend Framework 2 Components
4
 * 
5
 * AdminModule
6
 *
7
 * @package   [MyApplication]
8
 * @package   BB's Zend Framework 2 Components
9
 * @package   AdminModule
10
 * @author    Björn Bartels <[email protected]>
11
 * @link      https://gitlab.bjoernbartels.earth/groups/zf2
12
 * @license   http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
13
 * @copyright copyright (c) 2016 Björn Bartels <[email protected]>
14
 */
15
16
namespace Admin\Model;
17
18
use Zend\Db\TableGateway\TableGateway;
19
use Admin\Model\Settings;
20
use Zend\Db\Sql\Select;
21
22
class SettingsTable
23
{
24
    protected $tableGateway;
25
26
    public function __construct(TableGateway $tableGateway)
27
    {
28
        $this->tableGateway = $tableGateway;
29
    }
30
31 View Code Duplication
    public function fetchAll($scope = '')
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...
32
    {
33
        $resultSet = $this->tableGateway->select(
34
            function (Select $select) use ($scope) {
35
                if (!empty($scope)) {
36
                    $select->where('scope = \''.$scope.'\'')->order('type, name ASC');
37
                } else {
38
                    $select->order('type, name ASC');
39
                }
40
            }
41
        );
42
        return $resultSet;
43
    }
44
45
    public function fetchApplication()
46
    {
47
        return $this->fetchAll('application');
48
    }
49
50 View Code Duplication
    public function fetchUser( $id )
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...
51
    {
52
        if (!$id) { return array(); 
53
        }
54
        $resultSet = $this->tableGateway->select(
55
            function (Select $select) use ($id) {
56
                if (!empty($id)) {
57
                    $select->where(array( '(scope = \'user\') AND (ref_id = \''.((int)$id).'\')' ))->order('type, name ASC');
58
                } else {
59
                    $select->order('type, name ASC');
60
                }
61
            }
62
        );
63
        return $resultSet;
64
    }
65
66
    public function getSettings($id)
67
    {
68
        $id  = (int) $id;
69
        $rowset = $this->tableGateway->select(array('settings_id' => $id));
70
        $row = $rowset->current();
71
        if (!$row) {
72
            throw new \Exception("Could not find row $id");
73
        }
74
        return $row;
75
    }
76
77
    public function saveSettings(Settings $settings)
78
    {
79
        $data = array(
80
        'scope'        => $settings->scope,
81
        'ref_id'    => $settings->ref_id,
82
        'type'        => $settings->type,
83
        'name'        => $settings->name,
84
        'value'        => $settings->value,
85
        );
86
87
        $id = (int)$settings->settings_id;
88
        if ($id == 0) {
89
            $this->tableGateway->insert($data);
90
        } else {
91
            if ($this->getSettings($id)) {
92
                $this->tableGateway->update($data, array('settings_id' => $id));
93
            } else {
94
                throw new \Exception('Form id does not exist');
95
            }
96
        }
97
    }
98
99
    public function deleteSettings($id)
100
    {
101
        $this->tableGateway->delete(array('settings_id' => $id));
102
    }
103
}