Passed
Push — master ( 1f91ec...0204e8 )
by Nils
04:11
created

FolderController::listFoldersAction()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 37
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 26
nc 12
nop 1
dl 0
loc 37
rs 9.1928
c 0
b 0
f 0
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
26
27
class FolderController extends BaseController
28
{
29
30
    /**
31
     * Get list of Folders
32
     *
33
     * @return void
34
     */
35
    public function listFoldersAction(array $userData)
36
    {
37
        $superGlobal = new protect\SuperGlobal\SuperGlobal();
38
        $strErrorDesc = '';
39
        $requestMethod = $superGlobal->get('REQUEST_METHOD', 'SERVER');
40
41
        // get parameters
42
        $arrQueryStringParams = $this->getQueryStringParams();
0 ignored issues
show
Unused Code introduced by
The assignment to $arrQueryStringParams is dead and can be removed.
Loading history...
43
44
        if (strtoupper($requestMethod) === 'GET') {
45
            if (empty($userData['folders_list'])) {
46
                $this->sendOutput("", ['HTTP/1.1 204 No Content']);
47
            } else {
48
                try {
49
                    $folderModel = new FolderModel();
50
                    $arrFolders = $folderModel->getFoldersInfo(explode(",", $userData['folders_list']));
51
                    $responseData = json_encode($arrFolders);
52
                } catch (Error $e) {
53
                    $strErrorDesc = $e->getMessage() . ' Something went wrong! Please contact support.';
54
                    $strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
55
                }
56
            }
57
        } else {
58
            $strErrorDesc = 'Method not supported';
59
            $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
60
        }
61
62
        // send output
63
        if (empty($strErrorDesc) === true) {
64
            $this->sendOutput(
65
                $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...
66
                ['Content-Type: application/json', 'HTTP/1.1 200 OK']
67
            );
68
        } else {
69
            $this->sendOutput(
70
                json_encode(['error' => $strErrorDesc]),
71
                ['Content-Type: application/json', $strErrorHeader]
72
            );
73
        }
74
    }
75
    //end listInFoldersAction() 
76
}
77