Passed
Pull Request — master (#3505)
by
unknown
05:44
created

FolderController::listFoldersAction()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 39
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 26
c 1
b 0
f 0
nc 12
nop 1
dl 0
loc 39
rs 9.1928
1
<?php
2
/**
3
 * Teampass - a collaborative passwords manager.
4
 * ---
5
 * This library is distributed in the hope that it will be useful,
6
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8
 * ---
9
 *
10
 * @project   Teampass
11
 * @version    API
12
 *
13
 * @file      FolderControler.php
14
 * ---
15
 *
16
 * @author    Nils Laumaillé ([email protected])
17
 *
18
 * @copyright 2009-2023 Teampass.net
19
 *
20
 * @license   https://spdx.org/licenses/GPL-3.0-only.html#licenseText GPL-3.0
21
 * ---
22
 *
23
 * @see       https://www.teampass.net
24
 */
25
class FolderController extends BaseController
26
{
27
    /**
28
     * Get list of Folder
29
     *
30
     * @return void
31
     */
32
    public function listFoldersAction(array $userData)
33
    {
34
        $superGlobal = new protect\SuperGlobal\SuperGlobal();
35
        $strErrorDesc = '';
36
        $requestMethod = $superGlobal->get('REQUEST_METHOD', 'SERVER');
37
38
        // get parameters
39
        $arrQueryStringParams = $this->getQueryStringParams();
0 ignored issues
show
Unused Code introduced by
The assignment to $arrQueryStringParams is dead and can be removed.
Loading history...
40
		
41
        if (strtoupper($requestMethod) === 'GET') {
42
            if (empty($userData['folders_list'])) {
43
                $this->sendOutput("", ['HTTP/1.1 204 No Content']);
44
            } else {
45
                try {
46
                    $folderModel = new FolderModel();
47
48
                    $arrFolders = $folderModel->getFoldersInfo(explode(",", $userData['folders_list']));
49
50
                    $responseData = json_encode($arrFolders);
51
                } catch (Error $e) {
52
                    $strErrorDesc = $e->getMessage() . ' Something went wrong! Please contact support.';
53
                    $strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
54
                }
55
            }
56
        } else {
57
            $strErrorDesc = 'Method not supported';
58
            $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
59
        }
60
61
        // send output
62
        if (empty($strErrorDesc) === true) {
63
            $this->sendOutput(
64
                $responseData,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $responseData does not seem to be defined for all execution paths leading up to this point.
Loading history...
65
                ['Content-Type: application/json', 'HTTP/1.1 200 OK']
66
            );
67
        } else {
68
            $this->sendOutput(
69
                json_encode(['error' => $strErrorDesc]),
70
                ['Content-Type: application/json', $strErrorHeader]
71
            );
72
        }
73
    }
74
}
75