Completed
Push — master ( b520c4...41a82a )
by Bjørn
02:59
created

CurlTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 52.94%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 54
ccs 9
cts 17
cp 0.5294
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initCurl() 0 8 2
A checkOperationalityForCurlTrait() 0 13 4
A checkOperationality() 0 3 1
1
<?php
2
3
namespace WebPConvert\Convert\Converters\ConverterTraits;
4
5
use WebPConvert\Convert\Exceptions\ConversionFailed\ConverterNotOperational\SystemRequirementsNotMetException;
6
use WebPConvert\Convert\Converters\AbstractConverter;
7
8
/**
9
 * Trait for converters that works by uploading to a cloud service.
10
 *
11
 * The trait adds a method for checking against upload limits.
12
 *
13
 * @package    WebPConvert
14
 * @author     Bjørn Rosell <[email protected]>
15
 * @since      Class available since Release 2.0.0
16
 */
17
trait CurlTrait
18
{
19
20
    /**
21
     * Check basis operationality for converters relying on curl.
22
     *
23
     * Performs the same as ::checkOperationality(). It is here so converters that overrides the
24
     * ::checkOperationality() still has a chance to do the checks.
25
     *
26
     * @throws  SystemRequirementsNotMetException
27
     * @return  void
28
     */
29 3
    public function checkOperationalityForCurlTrait()
30
    {
31 3
        if (!extension_loaded('curl')) {
32
            throw new SystemRequirementsNotMetException('Required cURL extension is not available.');
33
        }
34
35 3
        if (!function_exists('curl_init')) {
36
            throw new SystemRequirementsNotMetException('Required url_init() function is not available.');
37
        }
38
39 3
        if (!function_exists('curl_file_create')) {
40
            throw new SystemRequirementsNotMetException(
41
                'Required curl_file_create() function is not available (requires PHP > 5.5).'
42
            );
43
        }
44 3
    }
45
46
    /**
47
     * Check basis operationality for converters relying on curl
48
     *
49
     * @throws  SystemRequirementsNotMetException
50
     * @return  void
51
     */
52
    public function checkOperationality()
53
    {
54
        $this->checkOperationalityForCurlTrait();
55
    }
56
57
    /**
58
     * Init curl.
59
     *
60
     * @throws  SystemRequirementsNotMetException  if curl could not be initialized
61
     * @return  resource  curl handle
62
     */
63 5
    protected static function initCurl()
64
    {
65
        // Get curl handle
66 5
        $ch = curl_init();
67 5
        if ($ch === false) {
68
            throw new SystemRequirementsNotMetException('Could not initialise cURL.');
69
        }
70 5
        return $ch;
71
    }
72
}
73