ProjectRepository::findBranch()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
dl 17
loc 17
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 1
1
<?php
2
3
namespace PRReviewWatcher\Entity;
4
5
class ProjectRepository extends Repository
6
{
7
    /**
8
     * @return array
9
     */
10 View Code Duplication
    public function findAll()
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...
11
    {
12
        $sql     = 'SELECT * FROM project AS p LEFT JOIN credential AS c ON p.credential = c.idCred ORDER BY p.id DESC';
13
        $results = $this->db->prepare($sql);
14
        $results->execute();
15
        $results  = $results->fetchAll();
16
        $projects = array();
17
18
        foreach ($results as $row) {
19
            $projectId            = $row['id'];
20
            $projects[$projectId] = $this->buildDomainObject($row);
21
        }
22
23
        return $projects;
24
    }
25
26
    /**
27
     * @param $id
28
     *
29
     * @return Project
30
     */
31 View Code Duplication
    public function find($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...
32
    {
33
        $sql = 'SELECT p.*,c.idCred FROM project AS p, credential AS c WHERE p.id= :id';
34
        $row = $this->db->prepare($sql);
35
        $row->bindValue(':id', $id, SQLITE3_TEXT);
36
        $row->execute();
37
        $row = $this->db->fetchAssoc($sql, array($id));
38
39
        return $this->buildDomainObject($row);
40
    }
41
42
    /**
43
     * @param Project $project
44
     */
45
    public function save(Project $project)
46
    {
47
        $projectData = array(
48
            'name'       => $project->getName(),
49
            'branch'     => $project->getBranch(),
50
            'credential' => $project->getCredential(),
51
            'comment'    => $project->getComment(),
52
            'alive'      => $project->getAlive(),
53
        );
54
55
        if ($project->getId()) {
56
            $projectData['numberTaskList'] = $project->getNumberTaskList();
57
            $this->getDb()->update('project', $projectData, array('id' => $project->getId()));
58
        } else {
59
            $projectData['numberTaskList'] = 0;
60
            $this->getDb()->insert('project', $projectData);
61
        }
62
    }
63
64
    /**
65
     * @param $id
66
     */
67
    public function delete($id)
68
    {
69
        $this->getDb()->delete('project', array('id' => $id));
70
    }
71
72
    /**
73
     * @param $repoHook
74
     *
75
     * @return array
76
     */
77 View Code Duplication
    public function findBranch($repoHook)
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...
78
    {
79
        $sql    = 'SELECT branch FROM project WHERE name = :name AND alive = 1;';
80
        $result = $this->db->prepare($sql);
81
        $result->bindValue(':name', $repoHook);
82
        $result->execute();
83
        $result   = $result->fetchAll();
84
85
        $branches = array();
86
87
        foreach ($result as $row) {
88
            $branch     = $row['branch'];
89
            $branches[] = $branch;
90
        }
91
92
        return $branches;
93
    }
94
95
    /**
96
     * @param $repoHook
97
     * @param $branchHook
98
     *
99
     * @return \Doctrine\DBAL\Driver\Statement|mixed
100
     */
101 View Code Duplication
    public function findComment($repoHook, $branchHook)
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...
102
    {
103
        if ($branchHook == null) {
104
            $sql    = 'SELECT comment FROM project WHERE name = :name AND alive = 1;';
105
            $result = $this->db->prepare($sql);
106
            $result->bindValue(':name', $repoHook);
107
        } else {
108
            $sql    = 'SELECT comment FROM project WHERE name = :name AND branch = :branch AND alive = 1';
109
            $result = $this->db->prepare($sql);
110
            $result->bindValue(':name', $repoHook);
111
            $result->bindValue(':branch', $branchHook);
112
        }
113
114
        $result->execute();
115
        $result = $result->fetch();
116
        $result = $result['comment'];
117
118
        return $result;
119
    }
120
121
    /**
122
     * @param $repoHook
123
     * @param $branchHook
124
     *
125
     * @return \Doctrine\DBAL\Driver\Statement|mixed
126
     */
127 View Code Duplication
    public function findId($repoHook, $branchHook)
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...
128
    {
129
        if ($branchHook == null) {
130
            $sql    = "SELECT id FROM project AS p , credential AS c WHERE name = :name AND branch = 'all';";
131
            $result = $this->db->prepare($sql);
132
            $result->bindValue(':name', $repoHook);
133
        } else {
134
            $sql    = "SELECT id FROM project AS p , credential AS c WHERE name = :name AND branch = :branch;";
135
            $result = $this->db->prepare($sql);
136
            $result->bindValue(':name', $repoHook);
137
            $result->bindValue(':branch', $branchHook);
138
        }
139
140
        $result->execute();
141
        $result = $result->fetch();
142
        $result = $result['id'];
143
144
        return $result;
145
    }
146
147
    /**
148
     * @param $id
149
     */
150
    public function incrementNumber($id)
151
    {
152
        $sql    = 'SELECT numberTaskList FROM project WHERE id = :id';
153
        $result = $this->db->prepare($sql);
154
        $result->bindValue(':id', $id);
155
        $result->execute();
156
        $result = $result->fetch();
157
        $result = $result['numberTaskList'];
158
        $result = $result + 1;
159
160
        $sql    = 'UPDATE project SET numberTaskList = :numberTaskList where id = :id;';
161
        $update = $this->db->prepare($sql);
162
        $update->bindValue(':numberTaskList', $result);
163
        $update->bindValue(':id', $id);
164
        $update->execute();
165
    }
166
167
    /**
168
     * @param $row
169
     * @return Project
170
     */
171
    protected function buildDomainObject($row)
172
    {
173
        $credential = new Credential();
174
175
        $project = new Project();
176
        $project->setId($row['id']);
177
        $project->setName($row['name']);
178
        $project->setBranch($row['branch']);
179
        if (array_key_exists('nameCred', $row)) {
180
            $credential->setNameCred($row['nameCred']);
181
            $project->setCredential($credential->getNameCred());
182
        } else {
183
            $project->setCredential($row['credential']);
184
        }
185
        $project->setComment($row['comment']);
186
        $project->setAlive($row['alive']);
187
        $project->setNumberTaskList($row['numberTaskList']);
188
189
        return $project;
190
    }
191
}
192