Skip to content
Prev Previous commit
Next Next commit
fix: A bit of code cleanup in OC\AppFramework\OCS\ApiHelper
Signed-off-by: Côme Chilliet <[email protected]>
  • Loading branch information
come-nc committed Sep 9, 2024
commit 37569466de10a3e44e4d0e4b2aad96cf2c544ea5
47 changes: 17 additions & 30 deletions lib/private/AppFramework/OCS/ApiHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,25 @@

namespace OC\AppFramework\OCS;

use OCP\API;
use OC\OCS\Result;
use OCP\AppFramework\Http;
use OCP\AppFramework\OCSController;
use OCP\IRequest;
use OCP\Server;
use XMLWriter;

class ApiHelper {
/**
* api actions
*/
protected static $actions = [];

/**
* respond to a call
* @param \OC\OCS\Result $result
* @param string $format the format xml|json
* @psalm-taint-escape html
*/
public static function respond($result, $format = 'xml') {
$request = \OC::$server->getRequest();
public static function respond(Result $result, string $format = 'xml'): void {
$request = Server::get(IRequest::class);

// Send 401 headers if unauthorised
if ($result->getStatusCode() === \OCP\AppFramework\OCSController::RESPOND_UNAUTHORISED) {
if ($result->getStatusCode() === OCSController::RESPOND_UNAUTHORISED) {
// If request comes from JS return dummy auth request
if ($request->getHeader('X-Requested-With') === 'XMLHttpRequest') {
header('WWW-Authenticate: DummyBasic realm="Authorisation Required"');
Expand Down Expand Up @@ -56,10 +55,7 @@ public static function respond($result, $format = 'xml') {
echo $body;
}

/**
* @param XMLWriter $writer
*/
private static function toXML($array, $writer) {
private static function toXML(iterable $array, XMLWriter $writer): void {
foreach ($array as $k => $v) {
if ($k[0] === '@') {
$writer->writeAttribute(substr($k, 1), $v);
Expand All @@ -86,9 +82,8 @@ public static function requestedFormat(): string {

/**
* Based on the requested format the response content type is set
* @param string $format
*/
public static function setContentType($format = null) {
public static function setContentType(?string $format = null): void {
$format = is_null($format) ? self::requestedFormat() : $format;
if ($format === 'xml') {
header('Content-type: text/xml; charset=UTF-8');
Expand All @@ -103,29 +98,21 @@ public static function setContentType($format = null) {
header('Content-Type: application/octet-stream; charset=utf-8');
}

/**
* @param \OCP\IRequest $request
* @return bool
*/
protected static function isV2(\OCP\IRequest $request) {
protected static function isV2(IRequest $request): bool {
$script = $request->getScriptName();

return str_ends_with($script, '/ocs/v2.php');
}

/**
* @param integer $sc
* @return int
*/
public static function mapStatusCodes($sc) {
public static function mapStatusCodes(int $sc): ?int {
switch ($sc) {
case \OCP\AppFramework\OCSController::RESPOND_NOT_FOUND:
case OCSController::RESPOND_NOT_FOUND:
return Http::STATUS_NOT_FOUND;
case \OCP\AppFramework\OCSController::RESPOND_SERVER_ERROR:
case OCSController::RESPOND_SERVER_ERROR:
return Http::STATUS_INTERNAL_SERVER_ERROR;
case \OCP\AppFramework\OCSController::RESPOND_UNKNOWN_ERROR:
case OCSController::RESPOND_UNKNOWN_ERROR:
return Http::STATUS_INTERNAL_SERVER_ERROR;
case \OCP\AppFramework\OCSController::RESPOND_UNAUTHORISED:
case OCSController::RESPOND_UNAUTHORISED:
// already handled for v1
return null;
case 100:
Expand All @@ -143,7 +130,7 @@ public static function mapStatusCodes($sc) {
* @param string $format
* @return string
*/
public static function renderResult($format, $meta, $data) {
public static function renderResult(string $format, $meta, $data): string {
$response = [
'ocs' => [
'meta' => $meta,
Expand Down