Passed
Push — master ( 74ce7c...3af96c )
by Bjørn
03:41
created

checkFileSizeVsIniSetting()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 6.4984

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 4
nop 1
dl 0
loc 17
ccs 6
cts 13
cp 0.4615
crap 6.4984
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace WebPConvert\Convert\BaseConverters;
4
5
use WebPConvert\Convert\Exceptions\ConversionFailedException;
6
use WebPConvert\Convert\BaseConverters\AbstractConverter;
7
use WebPConvert\Convert\Helpers\PhpIniSizes;
8
9
/**
10
 * Base for converters that uses a cloud service.
11
 *
12
 * Handles checking that the file size of the source is smaller than the limits imposed in php.ini.
13
 *
14
 * @package    WebPConvert
15
 * @author     Bjørn Rosell <[email protected]>
16
 * @since      Class available since Release 2.0.0
17
 */
18
abstract class AbstractCloudConverter extends AbstractConverter
19
{
20
    /**
21
     * Test that filesize is below "upload_max_filesize" and "post_max_size" values in php.ini.
22
     *
23
     * @param  string  $iniSettingId  Id of ini setting (ie "upload_max_filesize")
24
     *
25
     * @throws  ConversionFailedException  if filesize is larger than the ini setting
26
     * @return  void
27
     */
28 1
    private function checkFileSizeVsIniSetting($iniSettingId)
29
    {
30 1
        $fileSize = @filesize($this->source);
31 1
        if ($fileSize === false) {
32
            return;
33
        }
34 1
        $sizeInIni = PhpIniSizes::getIniBytes($iniSettingId);
35 1
        if ($sizeInIni === false) {
36
            // Not sure if we should throw an exception here, or not...
37
            return;
38
        }
39 1
        if ($sizeInIni < $fileSize) {
40
            throw new ConversionFailedException(
41
                'File is larger than your ' . $iniSettingId . ' (set in your php.ini). File size:' .
42
                    round($fileSize/1024) . ' kb. ' .
43
                    $iniSettingId . ' in php.ini: ' . ini_get($iniSettingId) .
44
                    ' (parsed as ' . round($sizeInIni/1024) . ' kb)'
45
            );
46
        }
47 1
    }
48
49
    /**
50
     * Test that filesize is below "upload_max_filesize" and "post_max_size" values in php.ini.
51
     *
52
     * @throws  ConversionFailedException  if filesize is larger than "upload_max_filesize" or "post_max_size"
53
     * @return  void
54
     */
55 1
    protected function checkFilesizeRequirements()
56
    {
57 1
        $this->checkFileSizeVsIniSetting('upload_max_filesize');
0 ignored issues
show
Bug introduced by
The method checkFileSizeVsIniSetting() does not exist on WebPConvert\Convert\Base...\AbstractCloudConverter. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
        $this->/** @scrutinizer ignore-call */ 
58
               checkFileSizeVsIniSetting('upload_max_filesize');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58 1
        $this->checkFileSizeVsIniSetting('post_max_size');
59 1
    }
60
61
    /**
62
     * Check if specific file is convertable with current converter / converter settings.
63
     * @return void
64
     */
65 1
    public function checkConvertability()
66
    {
67 1
        $this->checkFilesizeRequirements();
0 ignored issues
show
Bug introduced by
The method checkFilesizeRequirements() does not exist on WebPConvert\Convert\Base...\AbstractCloudConverter. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
        $this->/** @scrutinizer ignore-call */ 
68
               checkFilesizeRequirements();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
68 1
    }
69
}
70