Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
53.33% covered (warning)
53.33%
8 / 15
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
CloudConverterTrait
53.33% covered (warning)
53.33%
8 / 15
66.67% covered (warning)
66.67%
2 / 3
9.66
0.00% covered (danger)
0.00%
0 / 1
 checkFileSizeVsIniSetting
41.67% covered (danger)
41.67%
5 / 12
0.00% covered (danger)
0.00%
0 / 1
7.18
 checkConvertabilityCloudConverterTrait
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 checkConvertability
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace WebPConvert\Convert\Converters\ConverterTraits;
4
5use WebPConvert\Convert\Exceptions\ConversionFailedException;
6use WebPConvert\Convert\Converters\AbstractConverter;
7use 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 <it@rosell.dk>
16 * @since      Class available since Release 2.0.0
17 */
18trait 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}