GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

TaskRepository   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 129
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A delete() 0 5 1
A toggle() 0 10 1
C create() 0 62 8
1
<?php
2
/**
3
 * Pterodactyl - Panel
4
 * Copyright (c) 2015 - 2017 Dane Everitt <[email protected]>.
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in all
14
 * copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
 * SOFTWARE.
23
 */
24
25
namespace Pterodactyl\Repositories;
26
27
use Cron;
28
use Validator;
29
use Pterodactyl\Models\Task;
30
use Pterodactyl\Models\User;
31
use Pterodactyl\Models\Server;
32
use Pterodactyl\Exceptions\DisplayException;
33
use Pterodactyl\Exceptions\DisplayValidationException;
34
35
class TaskRepository
36
{
37
    /**
38
     * The default values to use for new tasks.
39
     *
40
     * @var array
41
     */
42
    protected $defaults = [
43
        'year' => '*',
44
        'day_of_week' => '*',
45
        'month' => '*',
46
        'day_of_month' => '*',
47
        'hour' => '*',
48
        'minute' => '*/30',
49
    ];
50
51
    /**
52
     * Task action types.
53
     *
54
     * @var array
55
     */
56
    protected $actions = [
57
        'command',
58
        'power',
59
    ];
60
61
    /**
62
     * Deletes a given task.
63
     *
64
     * @param  int      $id
65
     * @return bool
66
     */
67
    public function delete($id)
68
    {
69
        $task = Task::findOrFail($id);
70
        $task->delete();
71
    }
72
73
    /**
74
     * Toggles a task active or inactive.
75
     *
76
     * @param  int  $id
77
     * @return bool
78
     */
79
    public function toggle($id)
80
    {
81
        $task = Task::findOrFail($id);
82
83
        $task->active = ! $task->active;
84
        $task->queued = false;
85
        $task->save();
86
87
        return $task->active;
88
    }
89
90
    /**
91
     * Create a new scheduled task for a given server.
92
     *
93
     * @param  int    $server
94
     * @param  int    $user
95
     * @param  array  $data
96
     * @return \Pterodactyl\Models\Task
97
     *
98
     * @throws \Pterodactyl\Exceptions\DisplayException
99
     * @throws \Pterodactyl\Exceptions\DisplayValidationException
100
     */
101
    public function create($server, $user, $data)
102
    {
103
        $server = Server::findOrFail($server);
104
        $user = User::findOrFail($user);
105
106
        $validator = Validator::make($data, [
107
            'action' => 'string|required',
108
            'data' => 'string|required',
109
            'year' => 'string|sometimes',
110
            'day_of_week' => 'string|sometimes',
111
            'month' => 'string|sometimes',
112
            'day_of_month' => 'string|sometimes',
113
            'hour' => 'string|sometimes',
114
            'minute' => 'string|sometimes',
115
        ]);
116
117
        if ($validator->fails()) {
118
            throw new DisplayValidationException(json_encode($validator->errors()));
119
        }
120
121
        if (! in_array($data['action'], $this->actions)) {
122
            throw new DisplayException('The action provided is not valid.');
123
        }
124
125
        $cron = $this->defaults;
126
        foreach ($this->defaults as $setting => $value) {
127
            if (array_key_exists($setting, $data) && ! is_null($data[$setting]) && $data[$setting] !== '') {
128
                $cron[$setting] = $data[$setting];
129
            }
130
        }
131
132
        // Check that is this a valid Cron Entry
133
        try {
134
            $buildCron = Cron::factory(sprintf('%s %s %s %s %s %s',
135
                $cron['minute'],
136
                $cron['hour'],
137
                $cron['day_of_month'],
138
                $cron['month'],
139
                $cron['day_of_week'],
140
                $cron['year']
141
            ));
142
        } catch (\Exception $ex) {
143
            throw $ex;
144
        }
145
146
        return Task::create([
147
            'user_id' => $user->id,
148
            'server_id' => $server->id,
149
            'active' => 1,
150
            'action' => $data['action'],
151
            'data' => $data['data'],
152
            'queued' => 0,
153
            'year' => $cron['year'],
154
            'day_of_week' => $cron['day_of_week'],
155
            'month' => $cron['month'],
156
            'day_of_month' => $cron['day_of_month'],
157
            'hour' => $cron['hour'],
158
            'minute' => $cron['minute'],
159
            'last_run' => null,
160
            'next_run' => $buildCron->getNextRunDate(),
161
        ]);
162
    }
163
}
164