SequenceOperationsAnalyzer   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 63
rs 10
wmc 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B analyze() 0 43 10
1
<?php
2
3
/**
4
 * @copyright Copyright (c) 2018 Matthias Held <[email protected]>
5
 * @author Matthias Held <[email protected]>
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
namespace OCA\RansomwareDetection\Analyzer;
23
24
use OCA\RansomwareDetection\Monitor;
25
26
class SequenceOperationsAnalyzer
27
{
28
    /**
29
     * Sequence operation class.
30
     *
31
     * @var int
32
     */
33
    const NO_WRITE_AND_DELETE = 0;
34
    const ONLY_WRITE = 1;
35
    const ONLY_DELETE = 2;
36
    const EQUAL_WRITE_AND_DELETE = 3;
37
    const DIFF_WRITE_AND_DELETE = 4;
38
39
    /**
40
     * Classifies the operations in a sequence.
41
     *
42
     * @param array $sequence
43
     *
44
     * @return int
45
     */
46
    public function analyze($sequence)
47
    {
48
        $numberOfWrittenFiles = 0;
49
        $numberOfDeletedFiles = 0;
50
        $numberOfRenamedFiles = 0;
51
52
        $sequenceClass = self::NO_WRITE_AND_DELETE;
53
54
        foreach ($sequence as $fileOperation) {
55
            switch ($fileOperation->getCommand()) {
56
                case Monitor::WRITE:
57
                    $numberOfWrittenFiles++;
58
                    break;
59
                case Monitor::READ:
60
                    break;
61
                case Monitor::RENAME:
62
                    $numberOfRenamedFiles++;
63
                    break;
64
                case Monitor::DELETE:
65
                    $numberOfDeletedFiles++;
66
                    break;
67
                default:
68
                    break;
69
            }
70
        }
71
72
        if ($numberOfWrittenFiles > 0) {
73
            if ($numberOfDeletedFiles > 0) {
74
                if ($numberOfWrittenFiles === $numberOfDeletedFiles) {
75
                    $sequenceClass = self::EQUAL_WRITE_AND_DELETE;
76
                } else {
77
                    $sequenceClass = self::DIFF_WRITE_AND_DELETE;
78
                }
79
            } else {
80
                $sequenceClass = self::ONLY_WRITE;
81
            }
82
        } else {
83
            if ($numberOfDeletedFiles > 0) {
84
                $sequenceClass = self::ONLY_DELETE;
85
            }
86
        }
87
88
        return $sequenceClass;
89
    }
90
}
91