Passed
Push — master ( e6c3c7...30bf9d )
by
unknown
13:45
created

UserSettingsController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 40
dl 0
loc 58
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getValidActionFromRequest() 0 4 2
B processAjaxRequest() 0 39 8
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace TYPO3\CMS\Backend\Controller;
19
20
use Psr\Http\Message\ResponseInterface;
21
use Psr\Http\Message\ServerRequestInterface;
22
use TYPO3\CMS\Backend\Configuration\BackendUserConfiguration;
23
use TYPO3\CMS\Core\Http\JsonResponse;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26
/**
27
 * A wrapper class to call BE_USER->uc
28
 * used for AJAX and Storage/Persistent JS object
29
 * @internal This class is a specific Backend controller implementation and is not considered part of the Public TYPO3 API.
30
 */
31
class UserSettingsController
32
{
33
    private const ALLOWED_ACTIONS = [
34
        'GET' => ['get', 'getAll'],
35
        'POST' => ['set', 'addToList', 'removeFromList', 'unset', 'clear']
36
    ];
37
38
    /**
39
     * Processes all AJAX calls and returns a JSON for the data
40
     *
41
     * @param ServerRequestInterface $request
42
     * @return ResponseInterface
43
     */
44
    public function processAjaxRequest(ServerRequestInterface $request): ResponseInterface
45
    {
46
        // do the regular / main logic, depending on the action parameter
47
        $action = $this->getValidActionFromRequest($request);
48
49
        $key = $request->getParsedBody()['key'] ?? $request->getQueryParams()['key'] ?? '';
50
        $value = $request->getParsedBody()['value'] ?? $request->getQueryParams()['value'] ?? '';
51
        $backendUserConfiguration = GeneralUtility::makeInstance(BackendUserConfiguration::class);
52
        switch ($action) {
53
            case 'get':
54
                $content = $backendUserConfiguration->get($key);
55
                break;
56
            case 'getAll':
57
                $content = $backendUserConfiguration->getAll();
58
                break;
59
            case 'set':
60
                $backendUserConfiguration->set($key, $value);
61
                $content = $backendUserConfiguration->getAll();
62
                break;
63
            case 'addToList':
64
                $backendUserConfiguration->addToList($key, $value);
65
                $content = $backendUserConfiguration->getAll();
66
                break;
67
            case 'removeFromList':
68
                $backendUserConfiguration->removeFromList($key, $value);
69
                $content = $backendUserConfiguration->getAll();
70
                break;
71
            case 'unset':
72
                $backendUserConfiguration->unsetOption($key);
73
                $content = $backendUserConfiguration->getAll();
74
                break;
75
            case 'clear':
76
                $backendUserConfiguration->clear();
77
                $content = ['result' => true];
78
                break;
79
            default:
80
                $content = ['result' => false];
81
        }
82
        return (new JsonResponse())->setPayload($content);
83
    }
84
85
    protected function getValidActionFromRequest(ServerRequestInterface $request): string
86
    {
87
        $action = $request->getParsedBody()['action'] ?? $request->getQueryParams()['action'] ?? '';
88
        return in_array($action, (self::ALLOWED_ACTIONS[$request->getMethod()] ?? []), true) ? $action : '';
89
    }
90
}
91