FOSSology  4.4.0
Open Source License Compliance by Open Source Software
MaintenanceController.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2022 Samuel Dushimimana <dushsam100@gmail.com>
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\UI\Api\Controllers;
9 
16 use Psr\Http\Message\ServerRequestInterface;
17 
28 {
38  public function createMaintenance($request, $response, $args)
39  {
40  // Check if the request comes from the admin.
41  $this->throwNotAdminException();
42 
43  $body = $this->getParsedBody($request);
44  if (empty($body['options'])) {
45  throw new HttpBadRequestException("No options provided!");
46  }
47 
48  //Remove all duplicate options
49  if (!is_array($body['options'])) {
50  throw new HttpBadRequestException("Options property should be an array.");
51  }
52  if (in_array("o",$body["options"]) && empty($body["goldDate"])) {
53  throw new HttpBadRequestException("Please provide a gold date.");
54  }
55  if (in_array("l",$body["options"]) && empty($body["logsDate"])) {
56  throw new HttpBadRequestException("Please provide a log date.");
57  }
58 
59  $body['options'] = array_unique($body['options']);
60  $alteredOptions = array();
62  $maintain = $this->restHelper->getPlugin('maintagent');
63  $existingOptions = $maintain->getOptions();
64 
65  //Check if all the given keys exist in the known options array
66  foreach ($body['options'] as $key) {
67  if (!array_key_exists($key, $existingOptions)) {
68  throw new HttpNotFoundException("KEY '" . $key . "' NOT FOUND!");
69  }
70  $alteredOptions[$key] = $key;
71  }
72 
73  $body['options'] = $alteredOptions;
74  $mess = $maintain->handle($body);
75  $returnVal = new Info(201, $mess, InfoType::INFO);
76  return $response->withJson($returnVal->getArray(), $returnVal->getCode());
77  }
78 }
Base controller for REST calls.
getParsedBody(ServerRequestInterface $request)
Parse request body as JSON and return associative PHP array.
Override Slim response for withJson function.
Different type of infos provided by REST.
Definition: InfoType.php:16
Info model to contain general error and return values.
Definition: Info.php:19