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

ApplicationInfoComposer::getFolderSize()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 10
ccs 0
cts 5
cp 0
crap 12
rs 9.4285
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