Passed
Push — master ( 47232b...207a99 )
by Bjørn
01:36 queued 12s
created

checkConvertabilityCloudConverterTrait()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
namespace WebPConvert\Convert\Converters\ConverterTraits;
4
5
use WebPConvert\Convert\Exceptions\ConversionFailedException;
6
use WebPConvert\Convert\Converters\AbstractConverter;
7
use WebPConvert\Convert\Helpers\PhpIniSizes;
8
9
/**
10
 * Trait for converters that works by uploading to a cloud service.
11
 *
12
 * The trait adds a method for checking against upload limits.
13
 *
14
 * @package    WebPConvert
15
 * @author     Bjørn Rosell <[email protected]>
16
 * @since      Class available since Release 2.0.0
17
 */
18
trait CloudConverterTrait
19
{
20
21
    /**
22
     * Test that filesize is below "upload_max_filesize" and "post_max_size" values in php.ini.
23
     *
24
     * @param  string  $iniSettingId  Id of ini setting (ie "upload_max_filesize")
25
     *
26
     * @throws  ConversionFailedException  if filesize is larger than the ini setting
27
     * @return  void
28
     */
29
    private function checkFileSizeVsIniSetting($iniSettingId)
30
    {
31
        $fileSize = @filesize($this->source);
32
        if ($fileSize === false) {
33
            return;
34
        }
35
        $sizeInIni = PhpIniSizes::getIniBytes($iniSettingId);
36
        if ($sizeInIni === false) {
37
            // Not sure if we should throw an exception here, or not...
38
            return;
39
        }
40
        if ($sizeInIni < $fileSize) {
41
            throw new ConversionFailedException(
42
                'File is larger than your ' . $iniSettingId . ' (set in your php.ini). File size:' .
43
                    round($fileSize/1024) . ' kb. ' .
44
                    $iniSettingId . ' in php.ini: ' . ini_get($iniSettingId) .
45
                    ' (parsed as ' . round($sizeInIni/1024) . ' kb)'
46
            );
47
        }
48
    }
49
50
    /**
51
     * Check convertability of cloud converters (that file is not bigger than limits set in php.ini).
52
     *
53
     * Performs the same as ::Convertability(). It is here so converters that overrides the
54
     * ::Convertability() still has a chance to do the checks.
55
     *
56
     * @throws  ConversionFailedException  if filesize is larger than "upload_max_filesize" or "post_max_size"
57
     * @return  void
58
     */
59
    public function checkConvertabilityCloudConverterTrait()
60
    {
61
        $this->checkFileSizeVsIniSetting('upload_max_filesize');
62
        $this->checkFileSizeVsIniSetting('post_max_size');
63
    }
64
65
    /**
66
     * Check convertability of cloud converters (file upload limits).
67
     */
68
    public function checkConvertability()
69
    {
70
        $this->checkConvertabilityCloudConverterTrait();
71
    }
72
}
73