GenerateUIPreload   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
eloc 28
c 5
b 1
f 0
dl 0
loc 77
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 5 1
A generatePreloadJS() 0 34 1
A getPath() 0 13 2
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
        $app_url = config('app.url');
53
        $client_id = config('enterpriseauth.credentials.client_id');
54
        $callback_uri = config('enterpriseauth.credentials.callback_url');
55
56
        $msauthjs = file_get_contents(__DIR__.'/msauth.js');
57
58
        // generate javascript file contents
59
        $contents = <<<EOF
60
console.log('inside preload.js');
61
62
// app url
63
var globalUrl = "$app_url";
64
65
// list of scopes we need to request a token for
66
var APIScopes = [
67
    // IF we are getting an access token for ourselves, just send the client id
68
    "$client_id"
69
    // IF we are getting an access token for ANOTHER app id, it needs to look like this
70
    //"api://$client_id/.default"
71
];
72
73
// client id and redirect uri for logging people in
74
var msalconfig = {
75
    clientID: "$client_id",
76
    redirectUri: "$callback_uri",
77
    cacheLocation: "localStorage"
78
};
79
80
$msauthjs
81
EOF;
82
        // finally write the file out
83
        file_put_contents($path, $contents);
84
    }
85
}
86