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

ApplicationsTable   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 13.98 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 13
loc 93
ccs 0
cts 45
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fetchAll() 13 13 2
A fetchAllFull() 0 16 1
A getApplication() 0 10 2
A saveApplication() 0 22 3
A deleteApplication() 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\Applications;
20
use Zend\Db\Sql\Select;
21
22
/**
23
 * class Admin\Model\ApplicationsTable
24
 * 
25
 * @author  bba
26
 * @package ApplicationsTable
27
 */
28
class ApplicationsTable
29
{
30
    protected $tableGateway;
31
32
    /**
33
     * 
34
     * @param TableGateway $tableGateway
35
     */
36
    public function __construct(TableGateway $tableGateway)
37
    {
38
        $this->tableGateway = $tableGateway;
39
    }
40
41
    /**
42
     * 
43
     * @param string $scope
44
     * @return \Zend\Db\ResultSet\ResultSet
45
     */
46 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...
47
    {
48
        $resultSet = $this->tableGateway->select(
49
            function (Select $select) use ($scope) {
50
                if (!empty($scope)) {
51
                    $select->where('scope = \''.$scope.'\'')->order('type, name ASC');
52
                } else {
53
                    $select->order('name ASC');
54
                }
55
            }
56
        );
57
        return $resultSet;
58
    }
59
60
    /**
61
     * 
62
     * @param string $scope
63
     * @return \Zend\Db\Adapter\Driver\ResultInterface
64
     */
65
    public function fetchAllFull($scope = '')
0 ignored issues
show
Unused Code introduced by
The parameter $scope is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
66
    {
67
        $sqlSelect = $this->tableGateway->getSql()->select();
68
        $sqlSelect->join(
69
            'clients',
70
            'applications.client_id = clients.clients_id',
71
            array(
72
            'clientname' => 'name',
73
            ),
74
            Select::JOIN_LEFT
75
        );
76
        $statement = $this->tableGateway->getSql()->prepareStatementForSqlObject($sqlSelect);
77
        $resultSet = $statement->execute();
78
        
79
        return $resultSet;
80
    }
81
82
    public function getApplication($id)
83
    {
84
        $id  = (int) $id;
85
        $rowset = $this->tableGateway->select(array('application_id' => $id));
86
        $row = $rowset->current();
87
        if (!$row) {
88
            throw new \Exception("Could not find row $id");
89
        }
90
        return $row;
91
    }
92
93
    public function saveApplication(Applications $applications)
94
    {
95
        $data = array(
96
        'name'        => $applications->name,
97
        'shortname'    => $applications->shortname,
98
        'path'        => $applications->path,
99
        'url'        => $applications->url,
100
        'email'        => $applications->email,
101
        'client_id'    => $applications->client_id,
102
        );
103
104
        $id = (int)$applications->application_id;
105
        if ($id == 0) {
106
            $this->tableGateway->insert($data);
107
        } else {
108
            if ($this->getApplications($id)) {
0 ignored issues
show
Bug introduced by
The method getApplications() does not exist on Admin\Model\ApplicationsTable. Did you maybe mean getApplication()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
109
                $this->tableGateway->update($data, array('application_id' => $id));
110
            } else {
111
                throw new \Exception('Form id does not exist');
112
            }
113
        }
114
    }
115
116
    public function deleteApplication($id)
117
    {
118
        $this->tableGateway->delete(array('application_id' => $id));
119
    }
120
}