Passed
Push — master ( 812e1d...638d34 )
by Ferry
03:57
created

DeveloperPluginStoreController::getUninstall()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 13
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: User
5
 * Date: 4/25/2019
6
 * Time: 9:28 PM
7
 */
8
9
namespace crocodicstudio\crudbooster\controllers;
10
11
use crocodicstudio\crudbooster\exceptions\CBValidationException;
12
use crocodicstudio\crudbooster\helpers\ModuleGenerator;
13
use Illuminate\Support\Facades\Cache;
14
use Illuminate\Support\Facades\DB;
15
use Illuminate\Support\Facades\Hash;
16
17
class DeveloperPluginStoreController extends Controller
18
{
19
20
    private $view = "crudbooster::dev_layouts.modules.plugin";
21
22
    public function __construct()
23
    {
24
        view()->share(['page_title'=>'Plugin Store']);
25
    }
26
27
28
    public function getIndex() {
29
        $data = [];
30
        $data['result'] = $this->fetchPluginData();
31
        return view($this->view.'.index',$data);
32
    }
33
34
    public function getUninstall($key)
35
    {
36
        $pluginData = $this->fetchPluginData();
37
38
        if(isset($pluginData[$key])) {
39
            if(file_exists(app_path("CBPlugins/".$key))) {
40
                rrmdir(app_path("CBPlugins/".$key));
41
                return cb()->redirectBack('Plugin has been uninstalled!','success');
42
            }else{
43
                return cb()->redirectBack('Failed to uninstall, plugin is not found');
44
            }
45
        }else {
46
            return cb()->redirectBack('Failed to uninstall, plugin key is not found');
47
        }
48
    }
49
50
    public function getInstall($key)
51
    {
52
        $pluginData = $this->fetchPluginData();
53
54
        if(isset($pluginData[$key])) {
55
            $plugin = $pluginData[$key];
56
57
            // Create temp file of zip plugin
58
            $temp = tmpfile();
59
            fwrite($temp, file_get_contents($plugin['url']));
0 ignored issues
show
Bug introduced by
It seems like $temp can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, 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

59
            fwrite(/** @scrutinizer ignore-type */ $temp, file_get_contents($plugin['url']));
Loading history...
60
            $filename = stream_get_meta_data($temp)['uri'];
0 ignored issues
show
Bug introduced by
It seems like $temp can also be of type false; however, parameter $stream of stream_get_meta_data() does only seem to accept resource, 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

60
            $filename = stream_get_meta_data(/** @scrutinizer ignore-type */ $temp)['uri'];
Loading history...
61
62
            // Extract zip plugin
63
            $zip = new \ZipArchive;
64
            $res = $zip->open($filename);
65
            if ($res === TRUE) {
66
                $zip->extractTo(app_path('CBPlugins'));
67
                $dirName = $zip->getNameIndex(0);
68
                $zip->close();
69
                fclose($temp);
0 ignored issues
show
Bug introduced by
It seems like $temp can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, 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

69
                fclose(/** @scrutinizer ignore-type */ $temp);
Loading history...
70
71
                // Rename
72
                rename(app_path("CBPlugins/".$dirName), app_path("CBPlugins/".$key));
73
74
                return cb()->redirectBack('Install / update plugin has been succesfull!','success');
75
76
            } else {
77
                return cb()->redirectBack("Failed to install/update, can't open the plugin archive");
78
            }
79
        }else{
80
            return cb()->redirectBack('Failed to install/update, plugin key is not found');
81
        }
82
    }
83
84
    private function fetchPluginData()
85
    {
86
        if($data = Cache::get("plugin_store_data")) {
87
            return $data;
88
        }
89
90
        $data = file_get_contents(base64_decode("aHR0cDovL2NydWRib29zdGVyLmNvbS9hcGkvcGx1Z2luP2FjY2Vzc190b2tlbj1iVmMvWm5wWU5TWnJNVlpZT0hFNVUydHFjU1U9"));
91
        $result = [];
92
        if($data) {
93
            $data = json_decode($data, true);
94
            if($data['status']==true) {
95
96
                foreach($data['data'] as $item) {
97
                    $key = $item['key'];
98
                    $result[ $key ] = $item;
99
                }
100
101
                $result = collect($result)->sortBy("name")->all();
102
103
                Cache::put("plugin_store_data", $result, now()->addDays(3));
104
            }
105
        }
106
        return $result;
107
    }
108
}