Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
54.55% covered (warning)
54.55%
6 / 11
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
CurlTrait
54.55% covered (warning)
54.55%
6 / 11
0.00% covered (danger)
0.00%
0 / 3
11.60
0.00% covered (danger)
0.00%
0 / 1
 checkOperationalityForCurlTrait
50.00% covered (danger)
50.00%
3 / 6
0.00% covered (danger)
0.00%
0 / 1
6.00
 checkOperationality
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 initCurl
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
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        if ($ch === false) {
68            throw new SystemRequirementsNotMetException('Could not initialise cURL.');
69        }
70        return $ch;
71    }
72}