edulegit_callback::sync_task_user()   F
last analyzed

Complexity

Conditions 13
Paths 1026

Size

Total Lines 44
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 44
rs 2.45
cc 13
nc 1026
nop 1

How to fix   Complexity   

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
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17
/**
18
 * The assignsubmission_edulegit core class.
19
 *
20
 * @package   assignsubmission_edulegit
21
 * @author    Alex Crosby <[email protected]>
22
 * @copyright @2024 EduLegit.com
23
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
26
namespace assignsubmission_edulegit;
27
28
/**
29
 * Callback handler for the edulegit plugin.
30
 *
31
 * This class handles incoming payloads and processes specific events
32
 * related to the EduLegit webhook request.
33
 *
34
 * @package   assignsubmission_edulegit
35
 * @author    Alex Crosby <[email protected]>
36
 * @copyright @2024 EduLegit.com
37
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
class edulegit_callback {
40
41
    /**
42
     * Instance of the edulegit submission repository.
43
     *
44
     * @var edulegit_submission_repository
45
     */
46
    protected edulegit_submission_repository $repository;
47
48
    /**
49
     * Constructor for edulegit_callback class.
50
     *
51
     * @param edulegit_submission_repository|null $repository An instance of edulegit_submission_repository or null.
52
     */
53
    public function __construct(?edulegit_submission_repository $repository = null) {
54
        $this->repository = $repository ?? new edulegit_submission_repository();
55
    }
56
57
    /**
58
     * Handles incoming payload and processes the event.
59
     *
60
     * @param object $payload The payload object containing the event and data.
61
     * @return mixed Returns the result of the event handling or null if the event is not recognized.
62
     */
63
    public function handle(object $payload) {
64
        $event = $payload->event ?? null;
65
        $data = $payload->data ?? [];
66
67
        if ($event == 'taskUser.sync') {
68
            return $this->sync_task_user($data);
0 ignored issues
show
Bug introduced by
It seems like $data can also be of type array; however, parameter $data of assignsubmission_edulegi...lback::sync_task_user() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

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

68
            return $this->sync_task_user(/** @scrutinizer ignore-type */ $data);
Loading history...
69
        }
70
        return null;
71
    }
72
73
    /**
74
     * Synchronizes task user information with the repository.
75
     *
76
     * @param object $data The data object containing the task user's information.
77
     * @return int|null Returns the submission ID if successful, or null if not.
78
     */
79
    private function sync_task_user($data) {
80
        $id = $data->externalId ?? null;
81
        if (!$id) {
82
            return null;
83
        }
84
85
        $submission = $this->repository->get_by_id($id);
86
87
        if (!$submission) {
88
            return null;
89
        }
90
91
        $submission->status = 1;
92
        $submission->error = '';
93
94
        if (isset($data->title)) {
95
            $submission->title = $data->title;
96
        }
97
        if (isset($data->content)) {
98
            $submission->content = $data->content;
99
        }
100
        if (isset($data->url)) {
101
            $submission->url = $data->url;
102
        }
103
        if (isset($data->authKey)) {
104
            $submission->authkey = $data->authKey;
105
        }
106
        if (isset($data->score)) {
107
            $submission->score = $data->score;
108
        }
109
        if (isset($data->plagiarism)) {
110
            $submission->plagiarism = $data->plagiarism;
111
        }
112
        if (isset($data->aiAverageProbability)) {
113
            $submission->airate = $data->aiAverageProbability;
114
        }
115
        if (isset($data->aiProbability)) {
116
            $submission->aiprobability = $data->aiProbability;
117
        }
118
        if (isset($data->loginTimeToken)) {
119
            $submission->userkey = $data->loginTimeToken;
120
        }
121
122
        return $this->repository->update_submission($submission) ? $submission->id : null;
123
    }
124
}
125