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

UserProfileTable::fetchAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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\User;
20
use Zend\Db\Sql\Select;
21
22
class UserProfileTable
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 getUserProfile($id)
38
    {
39
        $id  = (int) $id;
40
        $rowset = $this->tableGateway->select(array('user_id' => $id));
41
        $row = $rowset->current();
42
        if (!$row) {
43
            throw new \Exception("Could not find row $id");
44
        }
45
        return $row;
46
    }
47
48
    public function saveUserProfile(UserProfile $user)
49
    {
50
        
51
        $data = array(
52
        'user_id'        => $user->user_id,
53
                
54
        'street'        => $user->street,
55
        'city'            => $user->city,
56
        'country'        => $user->country,
57
        'phone'            => $user->phone,
58
        'cell'            => $user->cell,
59
                
60
        'twitter'        => $user->twitter,
61
        'facebook'        => $user->facebook,
62
        'skype'            => $user->skype,
63
        'icq'            => $user->icq,
64
        );
65
66
        $id = (int)$user->user_id;
67
        if ($id == 0) {
68
            throw new \Exception('invalid user');
69
        } else {
70
            try {
71
                if ($this->getUserProfile($id)) {
72
                    $this->tableGateway->update($data, array('user_id' => $id));
73
                }
74
            } catch (\Exception $e) {
75
                $this->tableGateway->insert($data);
76
            }
77
        }
78
        return $this;
79
    }
80
81
    public function deleteUserProfile($id)
82
    {
83
        $this->tableGateway->delete(array('user_id' => $id));
84
    }
85
    
86
    public function getTableGateway()
87
    {
88
        return $this->tableGateway;
89
    }
90
    
91
    public function setTableGateway(TableGateway $tableGateway)
92
    {
93
        $this->tableGateway = $tableGateway;
94
    }
95
    
96
    
97
}