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

UserTable   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 89
ccs 0
cts 49
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fetchAll() 0 5 1
A getUserByEmailOrUsername() 0 22 3
A getUser() 0 10 2
A saveUser() 0 22 3
A deleteUser() 0 4 1
A getTableGateway() 0 4 1
A setTableGateway() 0 4 1
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\User;
20
use Zend\Db\Sql\Select;
21
22
class UserTable
23
{
24
    protected $tableGateway;
25
26
    public function __construct(TableGateway $tableGateway)
27
    {
28
        $this->setTableGateway($tableGateway);
29
    }
30
31
    public function fetchAll()
32
    {
33
        $resultSet = $this->tableGateway->select();
34
        return $resultSet;
35
    }
36
37
    public function getUserByEmailOrUsername( $id )
38
    {
39
        if (empty($id) ) {
40
            return false;
41
        }
42
        $resultSet = $this->tableGateway->select(
43
            function (Select $select) use ($id) {
44
                $select->where(
45
                    array(
46
                    "username = '".$id."'",
47
                    "email = '".$id."'"
48
                    ),
49
                    'OR'
50
                );
51
            }
52
        );
53
        $user = $resultSet->current();
54
        if (!$user) {
55
            throw new \Exception("Could not find user with email or username '$id'");
56
        }
57
        return $user;
58
    }
59
60
    public function getUser($id)
61
    {
62
        $id  = (int) $id;
63
        $rowset = $this->tableGateway->select(array('user_id' => $id));
64
        $row = $rowset->current();
65
        if (!$row) {
66
            throw new \Exception("Could not find row $id");
67
        }
68
        return $row;
69
    }
70
71
    public function saveUser(User $user)
72
    {
73
        $data = array(
74
        'display_name'    => $user->display_name,
75
        'username'        => $user->username,
76
        'email'            => $user->email,
77
        'password'        => $user->password,
78
        'state'            => $user->state,
79
        'aclrole'        => $user->aclrole,
80
        );
81
82
        $id = (int)$user->user_id;
83
        if ($id == 0) {
84
            $this->tableGateway->insert($data);
85
        } else {
86
            if ($this->getUser($id)) {
87
                $this->tableGateway->update($data, array('user_id' => $id));
88
            } else {
89
                throw new \Exception('Form id does not exist');
90
            }
91
        }
92
    }
93
94
    public function deleteUser($id)
95
    {
96
        $this->tableGateway->delete(array('user_id' => $id));
97
    }
98
    
99
    public function getTableGateway()
100
    {
101
        return $this->tableGateway;
102
    }
103
    
104
    public function setTableGateway(TableGateway $tableGateway)
105
    {
106
        $this->tableGateway = $tableGateway;
107
    }
108
    
109
    
110
}