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