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

ClientsTable   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 84
Duplicated Lines 33.33 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fetchAll() 13 13 2
A fetchApplication() 0 4 1
A fetchUser() 15 15 3
A getClients() 0 10 2
A saveClients() 0 23 3
A deleteClients() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 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('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 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
}