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

ApplicationsTable   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
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 91
ccs 0
cts 44
cp 0
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A fetchAllFull() 0 15 1
A saveApplication() 0 19 3
A getApplication() 0 9 2
A fetchAll() 0 12 2
A __construct() 0 3 1
A deleteApplication() 0 3 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\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
    public function fetchAll($scope = '')
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. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

65
    public function fetchAllFull(/** @scrutinizer ignore-unused */ $scope = '')

This check looks for 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()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

108
            if ($this->/** @scrutinizer ignore-call */ getApplications($id)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

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
}