aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/api/controllers/Links.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-10-02 17:50:59 +0200
committerArthurHoaro <arthur@hoa.ro>2020-10-13 13:50:11 +0200
commitefb7d21b52eb033530e80e5e49d175e6e3b031f4 (patch)
tree4f34052788a08be1a30cb88c3339ae14e0b7c4da /application/api/controllers/Links.php
parent29c31b7ec6ca48ba37b7eb6da650931fd0cb7164 (diff)
downloadShaarli-efb7d21b52eb033530e80e5e49d175e6e3b031f4.tar.gz
Shaarli-efb7d21b52eb033530e80e5e49d175e6e3b031f4.tar.zst
Shaarli-efb7d21b52eb033530e80e5e49d175e6e3b031f4.zip
Add strict types for bookmarks management
Parameters typing and using strict types overall increase the codebase quality by enforcing the a given parameter will have the expected type. It also removes the need to unnecessary unit tests checking methods behavior with invalid input.
Diffstat (limited to 'application/api/controllers/Links.php')
-rw-r--r--application/api/controllers/Links.php19
1 files changed, 11 insertions, 8 deletions
diff --git a/application/api/controllers/Links.php b/application/api/controllers/Links.php
index 778097fd..73a1b84e 100644
--- a/application/api/controllers/Links.php
+++ b/application/api/controllers/Links.php
@@ -96,11 +96,12 @@ class Links extends ApiController
96 */ 96 */
97 public function getLink($request, $response, $args) 97 public function getLink($request, $response, $args)
98 { 98 {
99 if (!$this->bookmarkService->exists($args['id'])) { 99 $id = is_integer_mixed($args['id']) ? (int) $args['id'] : null;
100 if ($id === null || ! $this->bookmarkService->exists($id)) {
100 throw new ApiLinkNotFoundException(); 101 throw new ApiLinkNotFoundException();
101 } 102 }
102 $index = index_url($this->ci['environment']); 103 $index = index_url($this->ci['environment']);
103 $out = ApiUtils::formatLink($this->bookmarkService->get($args['id']), $index); 104 $out = ApiUtils::formatLink($this->bookmarkService->get($id), $index);
104 105
105 return $response->withJson($out, 200, $this->jsonStyle); 106 return $response->withJson($out, 200, $this->jsonStyle);
106 } 107 }
@@ -115,7 +116,7 @@ class Links extends ApiController
115 */ 116 */
116 public function postLink($request, $response) 117 public function postLink($request, $response)
117 { 118 {
118 $data = $request->getParsedBody(); 119 $data = (array) ($request->getParsedBody() ?? []);
119 $bookmark = ApiUtils::buildBookmarkFromRequest($data, $this->conf->get('privacy.default_private_links')); 120 $bookmark = ApiUtils::buildBookmarkFromRequest($data, $this->conf->get('privacy.default_private_links'));
120 // duplicate by URL, return 409 Conflict 121 // duplicate by URL, return 409 Conflict
121 if (! empty($bookmark->getUrl()) 122 if (! empty($bookmark->getUrl())
@@ -148,7 +149,8 @@ class Links extends ApiController
148 */ 149 */
149 public function putLink($request, $response, $args) 150 public function putLink($request, $response, $args)
150 { 151 {
151 if (! $this->bookmarkService->exists($args['id'])) { 152 $id = is_integer_mixed($args['id']) ? (int) $args['id'] : null;
153 if ($id === null || !$this->bookmarkService->exists($id)) {
152 throw new ApiLinkNotFoundException(); 154 throw new ApiLinkNotFoundException();
153 } 155 }
154 156
@@ -159,7 +161,7 @@ class Links extends ApiController
159 // duplicate URL on a different link, return 409 Conflict 161 // duplicate URL on a different link, return 409 Conflict
160 if (! empty($requestBookmark->getUrl()) 162 if (! empty($requestBookmark->getUrl())
161 && ! empty($dup = $this->bookmarkService->findByUrl($requestBookmark->getUrl())) 163 && ! empty($dup = $this->bookmarkService->findByUrl($requestBookmark->getUrl()))
162 && $dup->getId() != $args['id'] 164 && $dup->getId() != $id
163 ) { 165 ) {
164 return $response->withJson( 166 return $response->withJson(
165 ApiUtils::formatLink($dup, $index), 167 ApiUtils::formatLink($dup, $index),
@@ -168,7 +170,7 @@ class Links extends ApiController
168 ); 170 );
169 } 171 }
170 172
171 $responseBookmark = $this->bookmarkService->get($args['id']); 173 $responseBookmark = $this->bookmarkService->get($id);
172 $responseBookmark = ApiUtils::updateLink($responseBookmark, $requestBookmark); 174 $responseBookmark = ApiUtils::updateLink($responseBookmark, $requestBookmark);
173 $this->bookmarkService->set($responseBookmark); 175 $this->bookmarkService->set($responseBookmark);
174 176
@@ -189,10 +191,11 @@ class Links extends ApiController
189 */ 191 */
190 public function deleteLink($request, $response, $args) 192 public function deleteLink($request, $response, $args)
191 { 193 {
192 if (! $this->bookmarkService->exists($args['id'])) { 194 $id = is_integer_mixed($args['id']) ? (int) $args['id'] : null;
195 if ($id === null || !$this->bookmarkService->exists($id)) {
193 throw new ApiLinkNotFoundException(); 196 throw new ApiLinkNotFoundException();
194 } 197 }
195 $bookmark = $this->bookmarkService->get($args['id']); 198 $bookmark = $this->bookmarkService->get($id);
196 $this->bookmarkService->remove($bookmark); 199 $this->bookmarkService->remove($bookmark);
197 200
198 return $response->withStatus(204); 201 return $response->withStatus(204);