Passed
Pull Request — master (#883)
by Stefano
01:26
created

SystemHelper::checkBeditaApiVersion()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 4
nop 0
dl 0
loc 17
rs 9.6111
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
     * Get the minimum value between post_max_size and upload_max_filesize.
17
     *
18
     * @return int
19
     */
20
    public function getMaxFileSize(): int
21
    {
22
        $postMaxSize = intVal(substr(ini_get('post_max_size'), 0, -1));
23
        $uploadMaxFilesize = intVal(substr(ini_get('upload_max_filesize'), 0, -1));
24
25
        return min($postMaxSize, $uploadMaxFilesize) * 1024 * 1024;
26
    }
27
28
    /**
29
     * Return false when API version is less than required, true otherwise.
30
     *
31
     * @return bool
32
     */
33
    public function checkBeditaApiVersion(): bool
34
    {
35
        $project = (array)$this->getView()->get('project');
36
        $apiVersion = Hash::get($project, 'version');
37
        if (empty($apiVersion)) {
38
            return true;
39
        }
40
        $requiredApiVersions = (array)Configure::read('BEditaAPI.versions');
41
        foreach ($requiredApiVersions as $requiredApiVersion) {
42
            $apiMajor = substr($apiVersion, 0, strpos($apiVersion, '.'));
43
            $requiredApiMajor = substr($requiredApiVersion, 0, strpos($requiredApiVersion, '.'));
44
            if ($apiMajor === $requiredApiMajor && version_compare($apiVersion, $requiredApiVersion) >= 0) {
45
                return true;
46
            }
47
        }
48
49
        return false;
50
    }
51
}
52