• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

The-oGlow / ya-corapi / 19987852443

06 Dec 2025 11:29AM UTC coverage: 36.484% (-1.3%) from 37.825%
19987852443

push

github

oglowa
#3: Update files / untested

687 of 1883 relevant lines covered (36.48%)

1.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

36.84
/src/Yacorapi/Provider/CurlProvider.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of ezlogging
7
 *
8
 * (c) 2024 Oliver Glowa, coding.glowa.com
9
 *
10
 * This source file is subject to the Apache-2.0 license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13

14
namespace oglowa\tools\Yacorapi\Provider;
15

16
use Ds\Map;
17
use oglowa\tools\Yacorapi\ConstData;
18
use oglowa\tools\Yacorapi\IResponse;
19
use oglowa\tools\Yacorapi\Request\RequestType;
20

21
class CurlProvider extends AbstractProvider
22
{
23
    /** @var null|IResponse */
24
    private $dryRunResponse;
25

26
    public function __construct(?IResponse $dryRunResponse = null)
2✔
27
    {
28
        parent::__construct();
2✔
29
        $this->logger->debug('START');
2✔
30
        $this->dryRunResponse = $dryRunResponse;
2✔
31
        $this->logger->debug('END');
2✔
32
    }
33

34
    /**
35
     * @param string $execUrl
36
     * @param int    $reqType
37
     *
38
     * @return array<mixed,mixed>
39
     */
40
    protected function execInternal(string $execUrl, int $reqType = RequestType::REQ_TYP_GET)
1✔
41
    {
42
        $this->logger->debug('START - execUrl,reqType', [$execUrl, $reqType]);
1✔
43

44
        $curlSession = $this->prepareCurl($reqType);
1✔
45
        $rawData     = $this->execCurl($curlSession, $execUrl);
1✔
46

47
        $this->logger->debug('END');
1✔
48

49
        return $rawData;
1✔
50
    }
51

52
    /**
53
     * @param string           $execUrl
54
     * @param Map<mixed,mixed> $parameters
55
     * @param int              $reqType
56
     *
57
     * @return array<mixed,mixed>
58
     */
59
    protected function execPostInternal(string $execUrl, Map $parameters, $reqType = RequestType::REQ_TYP_PUT)
×
60
    {
61
        $this->logger->debug('START - execUrl,parameters,reqType', [$execUrl, $parameters, $reqType]);
×
62

63
        $curlSession = $this->prepareCurlWrite($parameters, $reqType);
×
64
        $rawData     = $this->execCurl($curlSession, $execUrl);
×
65

66
        $this->logger->debug('END');
×
67

68
        return $rawData;
×
69
    }
70

71
    /**
72
     * @param int $reqType
73
     *
74
     * @return false|resource
75
     */
76
    private function prepareCurl(int $reqType)
1✔
77
    {
78
        $this->logger->debug('START - reqType', [$reqType]);
1✔
79

80
        $newSession = curl_init();
1✔
81
        // Set cURL options
82
        curl_setopt($newSession, CURLOPT_RETURNTRANSFER, true);
1✔
83
        curl_setopt($newSession, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
1✔
84
        $this->prepareCertificate($newSession);
1✔
85
        $this->prepareAuthorisation($newSession);
1✔
86

87
        $this->logger->debug('END');
1✔
88

89
        return $newSession;
1✔
90
    }
91

92
    /**
93
     * @param Map<mixed, mixed> $parameters
94
     * @param int               $reqType
95
     *
96
     * @return false|resource
97
     */
98
    private function prepareCurlWrite(Map $parameters, int $reqType)
×
99
    {
100
        $this->logger->debug('START - parameters,reqType', [$parameters, $reqType]);
×
101

102
        $newSession = $this->prepareCurl($reqType);
×
103
        switch ($reqType) {
104
            case RequestType::REQ_TYP_POST: {
×
105
                $this->preparePostParameter($newSession, $parameters);
×
106
                break;
×
107
            }
108
            case RequestType::REQ_TYP_PUT:
×
109
            default: {
110
                $this->preparePutParameter($newSession, $parameters);
×
111
            }
112
        }
113

114
        $this->logger->debug('END');
×
115

116
        return $newSession;
×
117
    }
118

119
    /**
120
     * Run the query.
121
     *
122
     * @param false|resource $execSession
123
     * @param string         $execUrl
124
     * @param bool           $dryRun
125
     *
126
     * @return array<mixed,mixed>
127
     */
128
    private function execCurl($execSession, string $execUrl, bool $dryRun = false)
1✔
129
    {
130
        $this->logger->debug('START - execUrl,dryRun', [$execUrl, $dryRun]);
1✔
131

132
        /** @var array<mixed,mixed> */
133
        $rawData = [];
1✔
134
        if ($dryRun) {
1✔
135
            $this->logger->info('DRYRUN is activated');
×
136
            if (!is_null($this->dryRunResponse)) {
×
137
                $rawData = $this->dryRunResponse->getResponse()->toArray();
×
138
            }
139
        } else {
140
            if (is_resource($execSession) && !empty($execUrl)) {
1✔
141
                curl_setopt($execSession, CURLOPT_URL, $execUrl);
×
142
                $curlResponse = curl_exec($execSession);
×
143

144
                // Check for errors
145
                if (curl_errno($execSession) !== 0) {
×
146
                    $this->logger->error('curl_error', [curl_error($execSession)]);
×
147
                }
148

149
                // Close the cURL session
150
                curl_close($execSession);
×
151

152
                // Decode the response
153
                if (is_string($curlResponse)) {
×
154
                    $rawData = json_decode($curlResponse, true);
×
155
                }
156
            }
157
        }
158
        $this->logger->debug('END');
1✔
159

160
        return $rawData;
1✔
161
    }
162

163
    /**
164
     * @param false|resource $newSession
165
     */
166
    private function prepareCertificate(&$newSession): void
1✔
167
    {
168
        $this->logger->debug('START');
1✔
169

170
        if (is_resource($newSession)) {
1✔
171
            if (file_exists($this->constData->c(ConstData::KEY_MY_CERT_CA))) {
×
172
                curl_setopt($newSession, CURLOPT_CAINFO, $this->constData->c(ConstData::KEY_MY_CERT_CA));
×
173
            } else {
174
                $this->logger->warning('CA certificate not found!', [$this->constData->c(ConstData::KEY_MY_CERT_CA)]);
×
175
                curl_setopt($newSession, CURLOPT_SSL_VERIFYPEER, false); // NOSONAR: php:S4830
×
176
            }
177
        }
178

179
        $this->logger->debug('END');
1✔
180
    }
181

182
    /**
183
     * @param false|resource $newSession
184
     */
185
    private function prepareAuthorisation(&$newSession): void
1✔
186
    {
187
        $this->logger->debug('START');
1✔
188

189
        if (is_resource($newSession)) {
1✔
190
            $token = $this->getTokenValue();
×
191
            if (!empty($token)) {
×
192
                curl_setopt(
×
193
                    $newSession,
×
194
                    CURLOPT_HTTPHEADER,
×
195
                    [
×
196
                        'Accept: application/json',
×
197
                        'Content-Type: application/json',
×
198
                        'Authorization: Bearer ' . $token,
×
199
                    ]
×
200
                );
×
201
            } else {
202
                $auth = $this->getAuthValue();
×
203
                if (!empty($auth)) {
×
204
                    curl_setopt($newSession, CURLOPT_USERPWD, $auth);
×
205
                }
206
            }
207
        }
208

209
        $this->logger->debug('END');
1✔
210
    }
211

212
    /**
213
     * @param false|resource    $newSession
214
     * @param Map<mixed, mixed> $parameters
215
     */
216
    private function preparePutParameter(&$newSession, Map $parameters): void
×
217
    {
218
        $this->logger->debug('START - parameters', [$parameters]);
×
219

220
        if (is_resource($newSession)) {
×
221
            curl_setopt($newSession, CURLOPT_CUSTOMREQUEST, 'PUT');
×
222
            $parametersAsString = json_encode($parameters);
×
223
            if (is_string($parametersAsString)) {
×
224
                curl_setopt($newSession, CURLOPT_POSTFIELDS, $parametersAsString);
×
225
            }
226
        }
227

228
        $this->logger->debug('END');
×
229
    }
230

231
    /**
232
     * @param false|resource    $newSession
233
     * @param Map<mixed, mixed> $parameters
234
     */
235
    private function preparePostParameter(&$newSession, Map $parameters): void
×
236
    {
237
        $this->logger->debug('START - parameters', [$parameters]);
×
238

239
        if (is_resource($newSession)) {
×
240
            curl_setopt($newSession, CURLOPT_POST, true);
×
241
            $parametersAsString = json_encode($parameters);
×
242
            if (is_string($parametersAsString)) {
×
243
                curl_setopt($newSession, CURLOPT_POSTFIELDS, $parametersAsString);
×
244
            }
245
        }
246

247
        $this->logger->debug('END');
×
248
    }
249
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc