Completed
Pull Request — master (#10)
by ARCANEDEV
04:28
created

ApplicationInfoComposer   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 108
ccs 0
cts 40
cp 0
rs 10
wmc 15
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A compose() 0 18 1
A getApplicationSize() 0 8 1
A getFolderSize() 0 10 3
D formatSize() 0 25 10
1
<?php namespace Arcanesoft\Foundation\ViewComposers\System;
2
3
use Illuminate\View\View;
4
5
/**
6
 * Class     ApplicationInfoComposer
7
 *
8
 * @package  Arcanesoft\Foundation\ViewComposers\System
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class ApplicationInfoComposer
12
{
13
    /* -----------------------------------------------------------------
14
     |  Constants
15
     | -----------------------------------------------------------------
16
     */
17
18
    const VIEW = 'foundation::admin.system.information._includes.application';
19
20
    /* -----------------------------------------------------------------
21
     |  Main Methods
22
     | -----------------------------------------------------------------
23
     */
24
25
    /**
26
     * Compose the view.
27
     *
28
     * @param  \Illuminate\View\View  $view
29
     */
30
    public function compose(View $view)
31
    {
32
        $app = app();
33
34
        $view->with('application', [
35
            'url'                 => config('app.url'),
36
            'locale'              => strtoupper(config('app.locale')),
37
            'timezone'            => config('app.timezone'),
38
            'debug_mode'          => config('app.debug', false),
39
            'maintenance_mode'    => $app->isDownForMaintenance(),
40
            'arcanesoft_version'  => foundation()->version(),
41
            'laravel_version'     => $app->version(),
42
            'app_size'            => $this->getApplicationSize(),
43
            'database_connection' => config('database.default'),
44
            'cache_driver'        => config('cache.default'),
45
            'session_driver'      => config('session.driver')
46
        ]);
47
    }
48
49
    /* -----------------------------------------------------------------
50
     |  Other Methods
51
     | -----------------------------------------------------------------
52
     */
53
54
    /**
55
     * Get the application size.
56
     *
57
     * @return string
58
     */
59
    private function getApplicationSize()
60
    {
61
        $size = cache()->remember('foundation.app.size', 5, function () {
62
            return $this->getFolderSize(base_path());
63
        });
64
65
        return $this->formatSize($size);
66
    }
67
68
    /**
69
     * Get the folder size.
70
     *
71
     * @param  string  $dir
72
     *
73
     * @return int
74
     */
75
    private function getFolderSize($dir)
76
    {
77
        $size = 0;
78
79
        foreach (glob(rtrim($dir, '/').'/*', GLOB_NOSORT) as $each) {
80
            $size += is_file($each) ? filesize($each) : $this->getFolderSize($each);
81
        }
82
83
        return $size;
84
    }
85
86
    /**
87
     * Format the size for humans.
88
     *
89
     * @param  int  $bytes
90
     *
91
     * @return string
92
     */
93
    private function formatSize($bytes)
94
    {
95
        $kb = 1024;
96
        $mb = $kb * 1024;
97
        $gb = $mb * 1024;
98
        $tb = $gb * 1024;
99
100
        if (($bytes >= 0) && ($bytes < $kb)) {
101
            return $bytes . ' B';
102
        }
103
        elseif (($bytes >= $kb) && ($bytes < $mb)) {
104
            return ceil($bytes / $kb).' KB';
105
        }
106
        elseif (($bytes >= $mb) && ($bytes < $gb)) {
107
            return ceil($bytes / $mb).' MB';
108
        }
109
        elseif (($bytes >= $gb) && ($bytes < $tb)) {
110
            return ceil($bytes / $gb).' GB';
111
        }
112
        elseif ($bytes >= $tb) {
113
            return ceil($bytes / $tb).' TB';
114
        }
115
116
        return $bytes.' B';
117
    }
118
}
119