ByteHelper   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 19
c 2
b 0
f 0
dl 0
loc 38
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A str2bytes() 0 12 2
A maximum_upload_size() 0 13 5
1
<?php
2
3
namespace carono\exchange1c\helpers;
4
5
class ByteHelper
6
{
7
    /**
8
     * @param $value
9
     * @return mixed
10
     */
11
    public static function str2bytes($value)
12
    {
13
        $unit_byte = preg_replace('/[^a-zA-Z]/', '', $value);
14
        $num_val = preg_replace('/[^\d]/', '', $value);
15
        switch ($unit_byte) {
16
            case 'M':
17
                $k = 2;
18
                break;
19
            default:
20
                $k = 1;
21
        }
22
        return $num_val * pow(1024, $k);
23
    }
24
25
    /**
26
     * The maximum file upload size by getting PHP settings
27
     *
28
     * @return integer|float|false file size limit in BYTES based
29
     */
30
    public static function maximum_upload_size()
31
    {
32
        static $upload_size = null;
33
        if ($upload_size === null) {
34
            $post_max_size = self::str2bytes(ini_get('post_max_size'));
35
            $upload_max_filesize = self::str2bytes(ini_get('upload_max_filesize'));
36
            $memory_limit = self::str2bytes(ini_get('memory_limit'));
37
            if (empty($post_max_size) && empty($upload_max_filesize) && empty($memory_limit)) {
38
                return false;
39
            }
40
            $upload_size = min($post_max_size, $upload_max_filesize, $memory_limit);
41
        }
42
        return $upload_size;
43
    }
44
}