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 = '') |
|
|
|
|
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 ) |
|
|
|
|
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
|
|
|
} |
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.