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.
Completed
Pull Request — develop (#248)
by Dane
03:14
created

Service::updateFile()   B

Complexity

Conditions 4
Paths 7

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
rs 8.5806
cc 4
eloc 16
nc 7
nop 2
1
<?php
2
/**
3
 * Pterodactyl - Panel
4
 * Copyright (c) 2015 - 2016 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\ServiceRepository;
26
27
use DB;
28
use Uuid;
29
use Storage;
30
use Validator;
31
use Pterodactyl\Models;
32
use Pterodactyl\Exceptions\DisplayException;
33
use Pterodactyl\Exceptions\DisplayValidationException;
34
35
class Service
36
{
37
    public function __construct()
38
    {
39
        //
40
    }
41
42
    public function create(array $data)
43
    {
44
        $validator = Validator::make($data, [
45
            'name' => 'required|string|min:1|max:255',
46
            'description' => 'required|string',
47
            'file' => 'required|unique:services,file|regex:/^[\w.-]{1,50}$/',
48
            'executable' => 'max:255|regex:/^(.*)$/',
49
            'startup' => 'string',
50
        ]);
51
52
        if ($validator->fails()) {
53
            throw new DisplayValidationException($validator->errors());
54
        }
55
56
        $data['author'] = env('SERVICE_AUTHOR', (string) Uuid::generate(4));
57
58
        $service = new Models\Service;
59
        DB::beginTransaction();
60
61
        try {
62
            $service->fill($data);
63
            $service->save();
64
65
            Storage::put('services/' . $data['file'] . '/main.json', '{}');
0 ignored issues
show
Bug introduced by
The method put() does not seem to exist on object<Illuminate\Contracts\Filesystem\Factory>.

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...
66
            Storage::copy('services/.templates/index.js', 'services/' . $data['file'] . '/index.js');
0 ignored issues
show
Bug introduced by
The method copy() does not seem to exist on object<Illuminate\Contracts\Filesystem\Factory>.

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...
67
68
            DB::commit();
69
        } catch (\Exception $ex) {
70
            DB::rollBack();
71
            throw $ex;
72
        }
73
74
        return $service->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Pterodactyl\Models\Service>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
75
    }
76
77
    public function update($id, array $data)
78
    {
79
        $service = Models\Service::findOrFail($id);
80
81
        $validator = Validator::make($data, [
82
            'name' => 'sometimes|required|string|min:1|max:255',
83
            'description' => 'sometimes|required|string',
84
            'file' => 'sometimes|required|regex:/^[\w.-]{1,50}$/',
85
            'executable' => 'sometimes|max:255|regex:/^(.*)$/',
86
            'startup' => 'sometimes|string',
87
        ]);
88
89
        if ($validator->fails()) {
90
            throw new DisplayValidationException($validator->errors());
91
        }
92
93
        $service->fill($data);
94
95
        return $service->save();
96
    }
97
98
    public function delete($id)
99
    {
100
        $service = Models\Service::findOrFail($id);
101
        $servers = Models\Server::where('service', $service->id)->get();
102
        $options = Models\ServiceOptions::select('id')->where('parent_service', $service->id);
103
104
        if (count($servers) !== 0) {
105
            throw new DisplayException('You cannot delete a service that has servers associated with it.');
106
        }
107
108
        DB::beginTransaction();
109
        try {
110
            Models\ServiceVariables::whereIn('option_id', $options->get()->toArray())->delete();
111
            $options->delete();
112
            $service->delete();
113
114
            Storage::deleteDirectory('services/' . $service->file);
0 ignored issues
show
Bug introduced by
The method deleteDirectory() does not seem to exist on object<Illuminate\Contracts\Filesystem\Factory>.

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...
115
            DB::commit();
116
        } catch (\Exception $ex) {
117
            DB::rollBack();
118
            throw $ex;
119
        }
120
    }
121
122
    public function updateFile($id, array $data)
123
    {
124
        $service = Models\Service::findOrFail($id);
125
126
        $validator = Validator::make($data, [
127
            'file' => 'required|in:index,main',
128
            'contents' => 'required|string',
129
        ]);
130
131
        if ($validator->fails()) {
132
            throw new DisplayValidationException($validator->errors());
133
        }
134
135
        $filename = ($data['file'] === 'main') ? 'main.json' : 'index.js';
136
        $filepath = 'services/' . $service->file . '/' . $filename;
137
        $backup = 'services/.bak/' . str_random(12) . '.bak';
138
139
        try {
140
            Storage::move($filepath, $backup);
0 ignored issues
show
Bug introduced by
The method move() does not seem to exist on object<Illuminate\Contracts\Filesystem\Factory>.

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...
141
            Storage::put($filepath, $data['contents']);
0 ignored issues
show
Bug introduced by
The method put() does not seem to exist on object<Illuminate\Contracts\Filesystem\Factory>.

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...
142
        } catch (\Exception $ex) {
143
            Storage::move($backup, $filepath);
0 ignored issues
show
Bug introduced by
The method move() does not seem to exist on object<Illuminate\Contracts\Filesystem\Factory>.

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...
144
            throw $ex;
145
        }
146
    }
147
}
148