Passed
Branch ops-updates (277b44)
by Björn
05:09
created

ClientsTable   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
c 1
b 0
f 0
dl 0
loc 82
ccs 0
cts 46
cp 0
rs 10
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A fetchAll() 0 12 2
A fetchApplication() 0 3 1
A __construct() 0 3 1
A saveClients() 0 20 3
A deleteClients() 0 3 1
A fetchUser() 0 14 3
A getClients() 0 9 2
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\Clients;
20
use Zend\Db\Sql\Select;
21
22
class ClientsTable
23
{
24
    protected $tableGateway;
25
26
    public function __construct(TableGateway $tableGateway)
27
    {
28
        $this->tableGateway = $tableGateway;
29
    }
30
31
    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('name ASC');
39
                }
40
            }
41
        );
42
        return $resultSet;
43
    }
44
45
    public function fetchApplication()
46
    {
47
        return $this->fetchAll('application');
48
    }
49
50
    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 getClients($id)
67
    {
68
        $id  = (int) $id;
69
        $rowset = $this->tableGateway->select(array('clients_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 saveClients(Clients $clients)
78
    {
79
        $data = array(
80
        'name'            => $clients->name,
81
        'extraname'        => $clients->extraname,
82
        'homepage'        => $clients->homepage,
83
        'email'            => $clients->email,
84
        'contact'        => $clients->contact,
85
        'phone'            => $clients->phone,
86
        'statistics'    => $clients->statistics,
87
        );
88
89
        $id = (int)$clients->clients_id;
90
        if ($id == 0) {
91
            $this->tableGateway->insert($data);
92
        } else {
93
            if ($this->getClients($id)) {
94
                $this->tableGateway->update($data, array('clients_id' => $id));
95
            } else {
96
                throw new \Exception('Form id does not exist');
97
            }
98
        }
99
    }
100
101
    public function deleteClients($id)
102
    {
103
        $this->tableGateway->delete(array('clients_id' => $id));
104
    }
105
}