GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

APIController::addRetention()   B
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 34
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 34
ccs 23
cts 23
cp 1
rs 8.439
cc 5
eloc 24
nc 3
nop 3
crap 5
1
<?php
2
/**
3
 *
4
 * @author Roeland Jago Douma <[email protected]>
5
 *
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
namespace OCA\Files_Retention\Controller;
23
24
use OCP\AppFramework\Controller;
25
use OCP\AppFramework\Http;
26
use OCP\AppFramework\Http\JSONResponse;
27
use OCP\AppFramework\Http\Response;
28
use OCP\BackgroundJob\IJobList;
29
use OCP\IDBConnection;
30
use OCP\IRequest;
31
use OCP\SystemTag\ISystemTagManager;
32
33
class APIController extends Controller {
34
35
	/** @var IDBConnection */
36
	private $db;
37
38
	/** @var ISystemTagManager */
39
	private $tagManager;
40
41
	/** @var IJobList */
42
	private $joblist;
43
44
	/**
45
	 * APIController constructor.
46
	 *
47
	 * @param string $appName
48
	 * @param IRequest $request
49
	 * @param IDBConnection $db
50
	 * @param ISystemTagManager $tagManager
51
	 * @param IJobList $jobList
52
	 */
53 108
	public function __construct($appName,
54
								IRequest $request,
55
								IDBConnection $db,
56
								ISystemTagManager $tagManager,
57
								IJobList $jobList) {
58 108
		parent::__construct($appName, $request);
59
60 108
		$this->db = $db;
61 108
		$this->tagManager = $tagManager;
62 108
		$this->joblist = $jobList;
63 108
	}
64
65
	/**
66
	 * @return JSONResponse
67
	 */
68 18
	public function getRetentions() {
69 18
		$qb = $this->db->getQueryBuilder();
70
71 18
		$qb->select('*')
72 18
			->from('retention')
73 18
			->orderBy('id');
74
75 18
		$cursor = $qb->execute();
76
77 18
		$result = [];
78
79 18
		while($data = $cursor->fetch()) {
80 12
			$result[] = [
81 12
				'id' => (int)$data['id'],
82 12
				'tagid' => (int)$data['tag_id'],
83 12
				'timeunit' => (int)$data['time_unit'],
84 12
				'timeamount' => (int)$data['time_amount'],
85
			];
86 10
		}
87
88 18
		$cursor->closeCursor();
89
90 18
		return new JSONResponse($result);
91
	}
92
93
	/**
94
	 * @param int $tagid
95
	 * @param int $timeunit
96
	 * @param int $timeamount
97
	 *
98
	 * @return Response
99
	 */
100 18
	public function addRetention($tagid, $timeunit, $timeamount) {
101 18
		$response = new Response();
102
103
		try {
104 18
			$this->tagManager->getTagsByIds($tagid);
105 16
		} catch (\InvalidArgumentException $e) {
106 6
			$response->setStatus(Http::STATUS_BAD_REQUEST);
107 6
			return $response;
108
		}
109
110 12
		if ($timeunit < 0 || $timeunit > 3 || $timeamount < 1) {
111 6
			$response->setStatus(Http::STATUS_BAD_REQUEST);
112 6
			return $response;
113
		}
114
115 6
		$qb = $this->db->getQueryBuilder();
116 6
		$qb->insert('retention')
117 6
			->setValue('tag_id', $qb->createNamedParameter($tagid))
118 6
			->setValue('time_unit', $qb->createNamedParameter($timeunit))
119 6
			->setValue('time_amount', $qb->createNamedParameter($timeamount));
120
121 6
		$qb->execute();
122 6
		$id = $qb->getLastInsertId();
123
124
		//Insert cronjob
125 6
		$this->joblist->add('OCA\Files_Retention\BackgroundJob\RetentionJob', ['tag' => $tagid]);
126
127 6
		return new JSONResponse([
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \OCP\AppFrame...\Http::STATUS_CREATED); (OCP\AppFramework\Http\JSONResponse) is incompatible with the return type documented by OCA\Files_Retention\Cont...ontroller::addRetention of type OCP\AppFramework\Http\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
128 6
			'id' => $id,
129 6
			'tagid' => $tagid,
130 6
			'timeunit' => $timeunit,
131 6
			'timeamount' => $timeamount,
132 6
		], Http::STATUS_CREATED);
133
	}
134
135
	/**
136
	 * @param int $id
137
	 *
138
	 * @return Response
139
	 */
140 12
	public function deleteRetention($id) {
141 12
		$qb = $this->db->getQueryBuilder();
142
143
		// Fetch tag_id
144 12
		$qb->select('tag_id')
145 12
			->from('retention')
146 12
			->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
147 12
			->setMaxResults(1);
148 12
		$cursor = $qb->execute();
149 12
		$data = $cursor->fetch();
150 12
		$cursor->closeCursor();
151
152 12
		if ($data === false) {
153 6
			return new Http\NotFoundResponse();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \OCP\AppFrame...ttp\NotFoundResponse(); (OCP\AppFramework\Http\NotFoundResponse) is incompatible with the return type documented by OCA\Files_Retention\Cont...roller::deleteRetention of type OCP\AppFramework\Http\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
154
		}
155
156
		// Remove from retention db
157 6
		$qb = $this->db->getQueryBuilder();
158 6
		$qb->delete('retention')
159 6
			->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
160 6
		$qb->execute();
161
162
		// Remove cronjob
163 6
		$this->joblist->remove('OCA\Files_Retention\BackgroundJob\RetentionJob', ['tag' => $data['tag_id']]);
164
165 6
		$response = new Response();
166 6
		$response->setStatus(Http::STATUS_NO_CONTENT);
167 6
		return $response;
168
	}
169
170
	/**
171
	 * @param int $id
172
	 * @param int|null $timeunit
173
	 * @param int|null $timeamount
174
	 *
175
	 * @return Response
176
	 */
177 60
	public function editRetention($id, $timeunit = null, $timeamount = null) {
178 60
		if (($timeunit === null && $timeamount === null) ||
179 54
			($timeunit !== null && ($timeunit < 0 || $timeunit > 3)) ||
180 60
			($timeamount !== null && $timeamount < 1)) {
181 36
			$response = new Response();
182 36
			$response->setStatus(Http::STATUS_BAD_REQUEST);
183 36
			return $response;
184
		}
185
186 24
		$qb = $this->db->getQueryBuilder();
187
188
		// Fetch tag_id
189 24
		$qb->select('tag_id')
190 24
			->from('retention')
191 24
			->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
192 24
			->setMaxResults(1);
193 24
		$cursor = $qb->execute();
194 24
		$data = $cursor->fetch();
195 24
		$cursor->closeCursor();
196
197 24
		if ($data === false) {
198 6
			return new Http\NotFoundResponse();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \OCP\AppFrame...ttp\NotFoundResponse(); (OCP\AppFramework\Http\NotFoundResponse) is incompatible with the return type documented by OCA\Files_Retention\Cont...ntroller::editRetention of type OCP\AppFramework\Http\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
199
		}
200
201 18
		$qb = $this->db->getQueryBuilder();
202 18
		$qb->update('retention');
203
204 18
		if ($timeunit !== null) {
205 12
			$qb->set('time_unit', $qb->createNamedParameter($timeunit));
206 10
		}
207 18
		if ($timeamount !== null) {
208 12
			$qb->set('time_amount', $qb->createNamedParameter($timeamount));
209 10
		}
210 18
		$qb->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
211 18
		$qb->execute();
212
213 18
		$qb = $this->db->getQueryBuilder();
214 18
		$qb->select('*')
215 18
			->from('retention')
216 18
			->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
217 18
			->setMaxResults(1);
218 18
		$cursor = $qb->execute();
219 18
		$data = $cursor->fetch();
220 18
		$cursor->closeCursor();
221
222 18
		return new JSONResponse([
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \OCP\AppFrame...$data['time_amount'])); (OCP\AppFramework\Http\JSONResponse) is incompatible with the return type documented by OCA\Files_Retention\Cont...ntroller::editRetention of type OCP\AppFramework\Http\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
223 18
			'id' => $id,
224 18
			'tagid' => (int)$data['tag_id'],
225 18
			'timeunit' => (int)$data['time_unit'],
226 18
			'timeamount' => (int)$data['time_amount'],
227 15
		]);
228
	}
229
}
230