Completed
Push — master ( 46139c...bf1053 )
by Alberto
28s queued 24s
created

SystemHelper::uploadMimeTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace App\View\Helper;
5
6
use Cake\Core\Configure;
7
use Cake\Utility\Hash;
8
use Cake\View\Helper;
9
10
/**
11
 * System helper
12
 */
13
class SystemHelper extends Helper
14
{
15
    /**
16
     * Accepted mime types for upload
17
     *
18
     * @var array
19
     */
20
    protected $defaultUploadMimeTypes = [
21
        'images' => [
22
            'image/apng',
23
            'image/bmp',
24
            'image/jp2',
25
            'image/jpeg',
26
            'image/jpg',
27
            'image/gif',
28
            'image/png',
29
            'image/svg+xml',
30
            'image/webp',
31
        ],
32
    ];
33
34
    /**
35
     * Get the minimum value between post_max_size and upload_max_filesize.
36
     *
37
     * @return int
38
     */
39
    public function getMaxFileSize(): int
40
    {
41
        $postMaxSize = intVal(substr(ini_get('post_max_size'), 0, -1));
42
        $uploadMaxFilesize = intVal(substr(ini_get('upload_max_filesize'), 0, -1));
43
44
        return min($postMaxSize, $uploadMaxFilesize) * 1024 * 1024;
45
    }
46
47
    /**
48
     * Return false when API version is less than required, true otherwise.
49
     *
50
     * @return bool
51
     */
52
    public function checkBeditaApiVersion(): bool
53
    {
54
        $project = (array)$this->getView()->get('project');
55
        $apiVersion = Hash::get($project, 'version');
56
        if (empty($apiVersion)) {
57
            return true;
58
        }
59
        $requiredApiVersions = (array)Configure::read('BEditaAPI.versions');
60
        foreach ($requiredApiVersions as $requiredApiVersion) {
61
            $apiMajor = substr($apiVersion, 0, strpos($apiVersion, '.'));
62
            $requiredApiMajor = substr($requiredApiVersion, 0, strpos($requiredApiVersion, '.'));
63
            if ($apiMajor === $requiredApiMajor && version_compare($apiVersion, $requiredApiVersion) >= 0) {
64
                return true;
65
            }
66
        }
67
68
        return false;
69
    }
70
71
    /**
72
     * Upload mime types
73
     *
74
     * @return array
75
     */
76
    public function uploadMimeTypes(): array
77
    {
78
        return (array)Configure::read('uploadMimeTypes', $this->defaultUploadMimeTypes);
79
    }
80
}
81