Passed
Pull Request — master (#30)
by
unknown
11:47
created

ProfileFromUrl::var_export_format()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 18
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 16
nc 9
nop 2
dl 0
loc 18
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
namespace MedianetDev\PConnector\Console\Commands;
4
5
use Illuminate\Console\Command;
6
7
class ProfileFromUrl extends Command
8
{
9
    /**
10
     * The name and signature of the console command.
11
     *
12
     * @var string
13
     */
14
    protected $signature = 'pconnector:generate {url : URL api} {profile? : Name of profile}';
15
16
    /**
17
     * The console command description.
18
     *
19
     * @var string
20
     */
21
    protected $description = 'Generate a PConnector profile';
22
23
    
24
25
    /**
26
     * Execute the console command.
27
     *
28
     * @return bool|null
29
     */
30
    public function handle()
31
    {
32
33
        $url = $this->argument('url');
34
35
        
36
        // Validate the Url
37
        if (filter_var($url, FILTER_VALIDATE_URL,FILTER_FLAG_PATH_REQUIRED) === FALSE) {
38
            return $this->error('Not a valid URL!');
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->error('Not a valid URL!') targeting Illuminate\Console\Command::error() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
39
        }
40
41
        // Publish the config
42
        \Artisan::call('vendor:publish', ['--provider' => 'MedianetDev\PConnector\PConnectorServiceProvider', '--tag' => 'config']);
43
44
        // Extract profile from url
45
        $gateway = parse_url($url);
0 ignored issues
show
Bug introduced by
It seems like $url can also be of type array; however, parameter $url of parse_url() does only seem to accept string, 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

45
        $gateway = parse_url(/** @scrutinizer ignore-type */ $url);
Loading history...
46
47
        $profiles = config('p-connector.profiles');
48
        $profile_name = $this->argument('profile') ?? 'profile';
49
        
50
        // Check existing profile name
51
        if(isset($profiles[$profile_name])){
52
            return $this->error('Please check your profile name');
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->error('Please check your profile name') targeting Illuminate\Console\Command::error() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
53
        }
54
55
        // Merge profiles
56
        $profiles = $profiles +
57
        [
58
            $profile_name => [
59
                'protocol' => $gateway['scheme'] ?? 'http',
60
                'host' => $gateway['host'] ?? 'foo.bar',
61
                'port' => $gateway['port'] ?? 80,
62
                'prefix' => $gateway['path'] ?? '/',
63
            ],
64
        ];
65
66
        // Add profile into config
67
        $path = config_path('p-connector.php');
68
        config(['p-connector.profiles' => $profiles]);
69
        
70
        // Set new Config
71
        if (file_exists($path)) {
72
            file_put_contents($path, "<?php \n return \n {$this->var_export_format(config('p-connector'))};");
73
        }
74
75
        $this->info('Profile added successfully.');
76
    }
77
78
    private function var_export_format($var, $indent="") {
79
        switch (gettype($var)) {
80
            case "string":
81
                return '"' . addcslashes($var, "\\\$\"\r\n\t\v\f") . '"';
82
            case "array":
83
                $indexed = array_keys($var) === range(0, count($var) - 1);
84
                $r = [];
85
                foreach ($var as $key => $value) {
86
                    $r[] = "$indent    "
87
                         . ($indexed ? "" : $this->var_export_format($key) . " => ")
88
                         . $this->var_export_format($value, "$indent    ");
89
                }
90
                return "[\n" . implode(",\n", $r) . "\n" . $indent . "]";
91
            case "boolean":
92
                return $var ? "TRUE" : "FALSE";
93
            case 'integer':  case 'double': return $var;
94
            default:
95
                return var_export($var, TRUE);
96
        }
97
    }
98
}
99