Passed
Push — master ( fa7ac3...812e1d )
by Ferry
04:19
created

DeveloperPluginStoreController::fetchPluginData()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 23
rs 9.5222
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 getInstall($key)
35
    {
36
        $pluginData = $this->fetchPluginData();
37
38
        if(isset($pluginData[$key])) {
39
            $plugin = $pluginData[$key];
40
41
            // Create temp file of zip plugin
42
            $temp = tmpfile();
43
            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

43
            fwrite(/** @scrutinizer ignore-type */ $temp, file_get_contents($plugin['url']));
Loading history...
44
            $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

44
            $filename = stream_get_meta_data(/** @scrutinizer ignore-type */ $temp)['uri'];
Loading history...
45
46
            // Extract zip plugin
47
            $zip = new \ZipArchive;
48
            $res = $zip->open($filename);
49
            if ($res === TRUE) {
50
                $zip->extractTo(app_path('CBPlugins'));
51
                $dirName = $zip->getNameIndex(0);
52
                $zip->close();
53
                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

53
                fclose(/** @scrutinizer ignore-type */ $temp);
Loading history...
54
55
                // Rename
56
                rename(app_path("CBPlugins/".$dirName), app_path("CBPlugins/".$key));
57
58
                return cb()->redirectBack('Install / update plugin has been succesfull!','success');
59
60
            } else {
61
                return cb()->redirectBack("Failed to install/update, can't open the plugin archive");
62
            }
63
        }else{
64
            return cb()->redirectBack('Failed to install/update, plugin key is not found');
65
        }
66
    }
67
68
    private function fetchPluginData()
69
    {
70
        if($data = Cache::get("plugin_store_data")) {
71
            return $data;
72
        }
73
74
        $data = file_get_contents(base64_decode("aHR0cDovL2NydWRib29zdGVyLmNvbS9hcGkvcGx1Z2luP2FjY2Vzc190b2tlbj1iVmMvWm5wWU5TWnJNVlpZT0hFNVUydHFjU1U9"));
75
        $result = [];
76
        if($data) {
77
            $data = json_decode($data, true);
78
            if($data['status']==true) {
79
80
                foreach($data['data'] as $item) {
81
                    $key = $item['key'];
82
                    $result[ $key ] = $item;
83
                }
84
85
                $result = collect($result)->sortBy("name")->all();
86
87
                Cache::put("plugin_store_data", $result, now()->addDays(3));
88
            }
89
        }
90
        return $result;
91
    }
92
}