Passed
Push — master ( 262b3f...86674d )
by Yannick
09:44
created

OnlyofficeCallbackService   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 69
c 1
b 0
f 0
dl 0
loc 130
rs 10
wmc 20

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getPermissionsByDocInfo() 0 17 2
A processTrackerStatusMustsave() 0 3 1
A processTrackerStatusClosed() 0 3 1
A processTrackerStatusForcesave() 0 5 1
A processTrackerStatusEditing() 0 3 1
A __construct() 0 5 1
A processTrackerStatusEditingAndClosed() 0 6 1
A processTrackerStatusCorrupted() 0 3 1
B processTrackerStatusMustsaveAndCorrupted() 0 61 11
1
<?php
2
/**
3
 * (c) Copyright Ascensio System SIA 2025.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
use Onlyoffice\DocsIntegrationSdk\Service\Callback\CallbackService;
18
19
class OnlyofficeCallbackService extends CallbackService
20
{
21
    private $docData;
22
23
    public function __construct($settingsManager, $jwtManager, $docData = [])
24
    {
25
        parent::__construct($settingsManager, $jwtManager);
26
        $this->docData = $docData;
27
        $this->trackResult = 1;
28
    }
29
30
    public function processTrackerStatusEditing($callback, string $fileid)
31
    {
32
        return $this->processTrackerStatusEditingAndClosed($callback, $fileid);
33
    }
34
35
    public function processTrackerStatusMustsave($callback, string $fileid)
36
    {
37
        return $this->processTrackerStatusMustsaveAndCorrupted($callback, $fileid);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->processTrackerSta...ted($callback, $fileid) targeting OnlyofficeCallbackServic...sMustsaveAndCorrupted() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
38
    }
39
40
    public function processTrackerStatusCorrupted($callback, string $fileid)
41
    {
42
        return $this->processTrackerStatusMustsaveAndCorrupted($callback, $fileid);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->processTrackerSta...ted($callback, $fileid) targeting OnlyofficeCallbackServic...sMustsaveAndCorrupted() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
43
    }
44
45
    public function processTrackerStatusClosed($callback, string $fileid)
46
    {
47
        return $this->processTrackerStatusEditingAndClosed($callback, $fileid);
48
    }
49
50
    public function processTrackerStatusForcesave($callback, string $fileid)
51
    {
52
        $result['error'] = $this->trackResult;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.
Loading history...
53
54
        return $result;
55
    }
56
57
    public function processTrackerStatusMustsaveAndCorrupted($callback, string $fileid)
58
    {
59
        $downloadUri = $callback->getUrl();
60
        $this->settingsManager->replaceDocumentServerUrlToInternal($downloadUri);
61
        try {
62
            if (!empty($this->docData['docId']) && !empty($this->docData['courseCode'])) {
63
                $docInfo = DocumentManager::get_document_data_by_id($fileid, $this->docData['courseCode'], false, $this->docData['sessionId']);
0 ignored issues
show
Deprecated Code introduced by
The function DocumentManager::get_document_data_by_id() has been deprecated: use $repo->find() ( Ignorable by Annotation )

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

63
                $docInfo = /** @scrutinizer ignore-deprecated */ DocumentManager::get_document_data_by_id($fileid, $this->docData['courseCode'], false, $this->docData['sessionId']);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
64
                if (false === $docInfo) {
65
                    $result['error'] = 'File not found';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.
Loading history...
66
67
                    return $result;
68
                }
69
                $filePath = $docInfo['absolute_path'];
70
            } else {
71
                $result['error'] = 'Bad Request';
72
73
                return $result;
74
            }
75
            list($isAllowToEdit, $isMyDir, $isGroupAccess, $isReadonly) = $this->getPermissionsByDocInfo($docInfo);
76
77
            if ($isReadonly) {
78
                $result['error'] = $this->trackResult;
79
80
                return $result;
81
            }
82
            if (($new_data = file_get_contents($downloadUri)) === false) {
83
                $result['error'] = $this->trackResult;
84
85
                return $result;
86
            }
87
88
            if ($isAllowToEdit || $isMyDir || $isGroupAccess) {
89
                $groupInfo = GroupManager::get_group_properties($this->docData['groupId']);
90
91
                if ($fp = @fopen($filePath, 'w')) {
92
                    fputs($fp, $new_data);
93
                    fclose($fp);
94
                    api_item_property_update($this->docData['courseInfo'],
0 ignored issues
show
Bug introduced by
The function api_item_property_update was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

94
                    /** @scrutinizer ignore-call */ 
95
                    api_item_property_update($this->docData['courseInfo'],
Loading history...
95
                        TOOL_DOCUMENT,
96
                        $fileid,
97
                        'DocumentUpdated',
98
                        $this->docData['userId'],
99
                        $groupInfo,
100
                        null,
101
                        null,
102
                        null,
103
                        $this->docData['sessionId']);
104
                    update_existing_document($this->docData['courseInfo'],
0 ignored issues
show
Bug introduced by
The function update_existing_document was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

104
                    /** @scrutinizer ignore-call */ 
105
                    update_existing_document($this->docData['courseInfo'],
Loading history...
105
                        $fileid,
106
                        filesize($filePath),
107
                        false);
108
                    $this->trackResult = 0;
109
                    $result['error'] = $this->trackResult;
110
111
                    return $result;
112
                }
113
            }
114
        } catch (UnexpectedValueException $e) {
115
            $result['error'] = 'Bad Request';
116
117
            return $result;
118
        }
119
    }
120
121
    public function processTrackerStatusEditingAndClosed($callback, string $fileid)
122
    {
123
        $this->trackResult = 0;
124
        $result['error'] = $this->trackResult;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.
Loading history...
125
126
        return $result;
127
    }
128
129
    /**
130
     * Method checks access rights to document and returns permissions.
131
     */
132
    public function getPermissionsByDocInfo(array $docInfo)
133
    {
134
        $isAllowToEdit = api_is_allowed_to_edit(true, true);
135
        $isMyDir = DocumentManager::is_my_shared_folder($this->docData['userId'], $docInfo['absolute_parent_path'], $this->docData['sessionId']);
0 ignored issues
show
Bug introduced by
The method is_my_shared_folder() does not exist on DocumentManager. ( Ignorable by Annotation )

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

135
        /** @scrutinizer ignore-call */ 
136
        $isMyDir = DocumentManager::is_my_shared_folder($this->docData['userId'], $docInfo['absolute_parent_path'], $this->docData['sessionId']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
136
137
        $isGroupAccess = false;
138
        if (!empty($groupId)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $groupId seems to never exist and therefore empty should always be true.
Loading history...
139
            $courseInfo = api_get_course_info($this->docData['courseCode']);
140
            Session::write('_real_cid', $courseInfo['real_id']);
141
            $groupProperties = GroupManager::get_group_properties($this->docData['groupId']);
142
            $docInfoGroup = api_get_item_property_info($courseInfo['real_id'], 'document', $docInfo['id'], $this->docData['sessionId']);
0 ignored issues
show
Bug introduced by
The function api_get_item_property_info was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

142
            $docInfoGroup = /** @scrutinizer ignore-call */ api_get_item_property_info($courseInfo['real_id'], 'document', $docInfo['id'], $this->docData['sessionId']);
Loading history...
143
            $isGroupAccess = GroupManager::allowUploadEditDocument($this->docData['userId'], $this->docData['courseCode'], $groupProperties, $docInfoGroup);
144
        }
145
146
        $isReadonly = $docInfo['readonly'];
147
148
        return [$isAllowToEdit, $isMyDir, $isGroupAccess, $isReadonly];
149
    }
150
}
151