Passed
Push — master ( 820bb4...1bb720 )
by Nils
04:11
created

performVisibleFoldersHtmlUpdate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 59
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 35
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 59
rs 9.36

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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   
12
 * @file      background_tasks___user_task.php
13
 * ---
14
 *
15
 * @author    Nils Laumaillé ([email protected])
16
 *
17
 * @copyright 2009-2023 Teampass.net
18
 *
19
 * @license   https://spdx.org/licenses/GPL-3.0-only.html#licenseText GPL-3.0
20
 * ---
21
 *
22
 * @see       https://www.teampass.net
23
 */
24
25
require_once __DIR__.'/../sources/SecureHandler.php';
26
session_name('teampass_session');
27
session_start();
28
$_SESSION['CPM'] = 1;
29
30
// Load config
31
require_once __DIR__.'/../includes/config/tp.config.php';
32
require_once __DIR__.'/background_tasks___functions.php';
33
34
// increase the maximum amount of time a script is allowed to run
35
set_time_limit($SETTINGS['task_maximum_run_time']);
36
37
// Do checks
38
require_once $SETTINGS['cpassman_dir'].'/includes/config/include.php';
39
require_once $SETTINGS['cpassman_dir'].'/includes/config/settings.php';
40
header('Content-type: text/html; charset=utf-8');
41
header('Cache-Control: no-cache, must-revalidate');
42
require_once $SETTINGS['cpassman_dir'].'/sources/main.functions.php';
43
44
// Connect to mysql server
45
require_once $SETTINGS['cpassman_dir'].'/includes/libraries/Database/Meekrodb/db.class.php';
46
if (defined('DB_PASSWD_CLEAR') === false) {
47
    define('DB_PASSWD_CLEAR', defuseReturnDecrypted(DB_PASSWD, $SETTINGS));
48
}
49
DB::$host = DB_HOST;
50
DB::$user = DB_USER;
51
DB::$password = DB_PASSWD_CLEAR;
52
DB::$dbName = DB_NAME;
53
DB::$port = DB_PORT;
54
DB::$encoding = DB_ENCODING;
55
DB::$ssl = DB_SSL;
56
DB::$connect_options = DB_CONNECT_OPTIONS;
57
58
// log start
59
$logID = doLog('start', 'user_task', (isset($SETTINGS['enable_tasks_log']) === true ? (int) $SETTINGS['enable_tasks_log'] : 0));
60
61
// Manage emails to send in queue.
62
// Only manage 10 emails at time
63
DB::debugmode(false);
64
$rows = DB::query(
65
    'SELECT *
66
    FROM ' . prefixTable('processes') . '
67
    WHERE is_in_progress = %i AND process_type = %s
68
    ORDER BY increment_id ASC LIMIT 0,10',
69
    0,
70
    'user_build_cache_tree'
71
);
72
foreach ($rows as $record) {
73
    // get email properties
74
    $arguments = json_decode($record['arguments'], true);
75
76
    // update visible_folders HTML
77
    performVisibleFoldersHtmlUpdate($arguments['user_id'], $SETTINGS);
78
79
    // update DB
80
    DB::update(
81
        prefixTable('processes'),
82
        array(
83
            'updated_at' => time(),
84
            'finished_at' => time(),
85
            'is_in_progress' => -1,
86
        ),
87
        'increment_id = %i',
88
        $record['increment_id']
89
    );
90
}
91
92
// log end
93
doLog('end', '', (isset($SETTINGS['enable_tasks_log']) === true ? (int) $SETTINGS['enable_tasks_log'] : 0), $logID);
94
95
96
function performVisibleFoldersHtmlUpdate(
97
    int $user_id,
98
    array $SETTINGS
0 ignored issues
show
Unused Code introduced by
The parameter $SETTINGS is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

98
    /** @scrutinizer ignore-unused */ array $SETTINGS

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
99
)
100
{
101
    $html = [];
102
103
    // rebuild tree
104
    require_once __DIR__. '/../sources/SplClassLoader.php';
105
    require_once __DIR__. '/../includes/libraries/Tree/NestedTree/NestedTree.php';
106
    $tree = new Tree\NestedTree\NestedTree(prefixTable('nested_tree'), 'id', 'parent_id', 'title');
107
    $tree->rebuild();
108
109
    // get current folders visible for user
110
    $cache_tree = DB::queryFirstRow(
111
        'SELECT increment_id, data FROM ' . prefixTable('cache_tree') . ' WHERE user_id = %i',
112
        $user_id
113
    );
114
    $folders = json_decode($cache_tree['data'], true);//print_r($folders);
115
    foreach ($folders as $folder) {
116
        $idFolder = (int) explode("li_", $folder['id'])[1];
117
118
        // Get path
119
        $path = '';
120
        $tree_path = $tree->getPath($idFolder, false);
121
        foreach ($tree_path as $fld) {
122
            $path .= empty($path) === true ? $fld->title : '/'.$fld->title;
123
        }
124
125
        // get folder info
126
        $folder = DB::queryFirstRow(
127
            'SELECT title, parent_id, personal_folder FROM ' . prefixTable('nested_tree') . ' WHERE id = %i',
128
            $idFolder
129
        );
130
131
        // finalize
132
        array_push(
133
            $html,
134
            [
135
                "path" => $path,
136
                "id" => $idFolder,
137
                "level" => count($tree_path),
138
                "title" => $folder['title'],
139
                "disabled" => 0,
140
                "parent_id" => $folder['parent_id'],
141
                "perso" => $folder['personal_folder'],
142
                "is_visible_active" => 1,
143
            ]
144
        );
145
    }
146
147
    DB::update(
148
        prefixTable('cache_tree'),
149
        array(
150
            'visible_folders' => json_encode($html),
151
            'timestamp' => time(),
152
        ),
153
        'increment_id = %i',
154
        (int) $cache_tree['increment_id']
155
    );
156
}