Passed
Push — master ( 32d89b...9acc29 )
by meta
04:28
created

GenerateUIPreload::generatePreloadJS()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 1
dl 0
loc 26
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace Metaclassing\EnterpriseAuth\Console;
4
5
use Illuminate\Console\Command;
6
7
class GenerateUIPreload extends Command
8
{
9
    /**
10
     * The name and signature of the console command.
11
     *
12
     * @var string
13
     */
14
    protected $signature = 'enterpriseauth:uipreload {--destination=*}';
15
16
    /**
17
     * The console command description.
18
     *
19
     * @var string
20
     */
21
    protected $description = 'Generate UI auth preload javascript';
22
23
    /**
24
     * Execute the console command.
25
     *
26
     * @return void
27
     */
28
    public function handle()
29
    {
30
        $path = $this->getPath();
31
        $this->generatePreloadJS($path);
32
        $this->info('Generated UI auth preload javascript to '.$path);
33
    }
34
35
    public function getPath()
36
    {
37
        // start in APPDIR/public
38
        $base = base_path('public');
39
        // see if the user gave us a specific path
40
        $path = $this->option('destination');
41
        $path = reset($path);
42
        // otherwise use the default path
43
        if(! $path) {
44
            $path = 'ui/preload.js';
45
        }
46
        // fully calculated path is APPDIR/public/ui/preload.js
47
        return $base . '/' . $path;
48
    }
49
50
    public function generatePreloadJS($path)
51
    {
52
        $client_id = config('enterpriseauth.credentials.client_id');
53
        $callback_uri = config('enterpriseauth.credentials.callback_url');
54
55
        $msauthjs = file_get_contents(__DIR__ . '/msauth.js');
56
57
        // generate javascript file contents
58
        $contents = <<<EOF
59
console.log('inside preload.js');
60
61
// list of scopes we need to request a token for
62
var APIScopes = [
63
    "api://$client_id/.default"
64
];
65
66
// client id and redirect uri for logging people in
67
var msalconfig = {
68
    clientID: "$client_id",
69
    redirectUri: "$callback_uri"
70
};
71
72
$msauthjs
73
EOF;
74
        // finally write the file out
75
        file_put_contents($path, $contents);
76
    }
77
}
78