Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
50.00% covered (danger)
50.00%
10 / 20
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
CurlTrait
50.00% covered (danger)
50.00%
10 / 20
0.00% covered (danger)
0.00%
0 / 4
22.50
0.00% covered (danger)
0.00%
0 / 1
 checkOperationalityForCurlTrait
37.50% covered (danger)
37.50%
3 / 8
0.00% covered (danger)
0.00%
0 / 1
7.91
 checkOperationality
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 initCurl
66.67% covered (warning)
66.67%
6 / 9
0.00% covered (danger)
0.00%
0 / 1
3.33
 closeCurl
50.00% covered (danger)
50.00%
1 / 2
0.00% covered (danger)
0.00%
0 / 1
2.50
1<?php
2
3namespace WebPConvert\Convert\Converters\ConverterTraits;
4
5use WebPConvert\Convert\Exceptions\ConversionFailed\ConverterNotOperational\SystemRequirementsNotMetException;
6use 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 <it@rosell.dk>
15 * @since      Class available since Release 2.0.0
16 */
17trait 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    public function checkOperationalityForCurlTrait()
30    {
31        if (!extension_loaded('curl')) {
32            throw new SystemRequirementsNotMetException('Required cURL extension is not available.');
33        }
34
35        if (!function_exists('curl_init')) {
36            throw new SystemRequirementsNotMetException('Required url_init() function is not available.');
37        }
38
39        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    }
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|\CurlHandle  curl handle (from PHP8: CurlHandle)
62     */
63    protected static function initCurl()
64    {
65        // Get curl handle
66        $ch = \curl_init();
67
68        if (defined('CURLOPT_PROTOCOLS_STR')) {
69            curl_setopt($ch, CURLOPT_PROTOCOLS_STR, 'http,https');
70            curl_setopt($ch, CURLOPT_REDIR_PROTOCOLS_STR, 'http,https');
71        } else {
72            curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
73            curl_setopt($ch, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
74        }
75        if ($ch === false) {
76            throw new SystemRequirementsNotMetException('Could not initialise cURL.');
77        }
78        return $ch;
79    }
80
81    /**
82     * Close curl.
83     *
84     * Before PHP 8.0, we need to call curl_close
85     * After PHP 8.0, PHP is smart enough to destroy the object by itself
86     *
87     * @param resource $curlHandle
88     */
89    protected static function closeCurl($curlHandle)
90    {
91        if (PHP_VERSION_ID < 80000) {
92            curl_close($curlHandle);
93        }
94    }
95}