Passed
Pull Request — master (#30)
by
unknown
03:50 queued 01:03
created

ProfileFromUrl::handle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 45
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 4
eloc 20
c 3
b 0
f 0
nc 4
nop 0
dl 0
loc 45
rs 9.6
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
        $url = $this->argument('url');
33
34
35
        // Validate the Url
36
        if (filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED) === false) {
37
            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...
38
        }
39
40
        // Publish the config
41
        \Artisan::call('vendor:publish', ['--provider' => 'MedianetDev\PConnector\PConnectorServiceProvider', '--tag' => 'config']);
42
43
        // Extract profile from url
44
        $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

44
        $gateway = parse_url(/** @scrutinizer ignore-type */ $url);
Loading history...
45
46
        $profiles = config('p-connector.profiles');
47
        $profile_name = $this->argument('profile') ?? 'profile';
48
49
        // Check existing profile name
50
        if (isset($profiles[$profile_name])) {
51
            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...
52
        }
53
54
        // Merge profiles
55
        $profiles = $profiles +
56
        [
57
            $profile_name => [
58
                'protocol' => $gateway['scheme'] ?? 'http',
59
                'host' => $gateway['host'] ?? 'foo.bar',
60
                'port' => $gateway['port'] ?? 80,
61
                'prefix' => $gateway['path'] ?? '/',
62
            ],
63
        ];
64
65
        // Add profile into config
66
        $path = config_path('p-connector.php');
67
        config(['p-connector.profiles' => $profiles]);
68
69
        // Set new Config
70
        if (file_exists($path)) {
71
            file_put_contents($path, "<?php \n return \n {$this->var_export_format(config('p-connector'))};");
72
        }
73
74
        $this->info('Profile added successfully.');
75
    }
76
77
    private function var_export_format($var, $indent="")
78
    {
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