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

The-oGlow / ya-corapi / 19973072714

05 Dec 2025 07:00PM UTC coverage: 37.825%. First build
19973072714

push

github

web-flow
Push2master (#4)

11 of 51 new or added lines in 13 files covered. (21.57%)

713 of 1885 relevant lines covered (37.82%)

1.88 hits per line

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

18.92
/src/Yacorapi/Response/AbstractResponse.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\Response;
15

16
use Ds\Map;
17
use Ds\Set;
18
use Monolog\ConsoleLogger;
19
use oglowa\tools\Yacorapi\IResponse;
20
use ollily\Tools\String\ToStringTrait;
21
use Psr\Log\LoggerInterface;
22

23
abstract class AbstractResponse implements IResponse
24
{
25
    use ToStringTrait;
26

27
    /** @var LoggerInterface */
28
    protected $logger;
29

30
    /** @var Map<mixed,mixed> */
31
    private $response;
32

33
    /** @var Map<mixed,mixed> */
34
    private $results;
35

36
    /**
37
     * Response constructor.
38
     *
39
     * @param null|array<mixed,mixed> $data
40
     */
41
    public function __construct(?array $data = [])
4✔
42
    {
43
        $this->logger = new ConsoleLogger(get_class($this));
4✔
44
        $this->logger->debug('START');
4✔
45

46
        $this->prepareData($data);
4✔
47

48
        $this->logger->debug('END');
4✔
49
    }
50

51
    /**
52
     * @return Map<mixed,mixed>
53
     */
54
    public function getResponse(): Map
3✔
55
    {
56
        return $this->response;
3✔
57
    }
58

59
    /**
60
     * @param mixed $key
61
     *
62
     * @return bool
63
     */
64
    public function keyExists($key): bool
×
65
    {
NEW
66
        return !empty($key) && $this->response->hasKey($key);
×
67
    }
68

69
    /**
70
     * @return Set<mixed>
71
     */
72
    public function keys(): Set
×
73
    {
74
        return $this->response->keys();
×
75
    }
76

77
    /**
78
     * @param mixed $key
79
     * @param mixed $default
80
     *
81
     * @return mixed
82
     */
83
    public function getValue($key, $default = '')
×
84
    {
85
        $value = $default;
×
86
        if ($this->keyExists($key)) {
×
87
            $value = $this->response->get($key, $default);
×
88
        }
89

90
        return $value;
×
91
    }
92

93
    /**
94
     * @return bool
95
     */
96
    public function checkStatus(): bool
×
97
    {
98
        $this->logger->debug('START');
×
99

100
        $statusOk = false;
×
101
        if ($this->keyExists(self::KEY_STATUS_CODE)) {
×
102
            $this->logger->error(
×
103
                self::MSG_ERROR,
×
104
                [$this->getValue(self::KEY_STATUS_CODE), $this->getValue(self::KEY_REASON), $this->getValue(self::KEY_MESSAGE)]
×
105
            );
×
106
        } else {
107
            $statusOk = true;
×
108
        }
109
        $this->logger->debug('END', [$statusOk]);
×
110

111
        return $statusOk;
×
112
    }
113

114
    /**
115
     * @return bool
116
     */
117
    public function checkData(): bool
×
118
    {
119
        $this->logger->debug('START');
×
120

121
        if ($this->isResultsAvailable()) {
×
122
            $hasData = $this->checkStatus();
×
123
            if ($hasData) {
×
124
                if (!$this->keyExists(IResponse::KEY_RESULTS) || $this->getValue(IResponse::KEY_SIZE) <= 0) {
×
125
                    $this->logger->info('Response has no results!');
×
126
                    $hasData = false;
×
127
                } else {
128
                    $this->logger->info('Response has results with size', [$this->keyExists(IResponse::KEY_RESULTS), $this->getValue(IResponse::KEY_SIZE)]);
×
129
                }
130
            }
131
        } else {
132
            $this->logger->info('Results are not available!');
×
133
            $hasData = false;
×
134
        }
135

136
        $this->logger->debug('END - hasData', [$hasData]);
×
137

138
        return $hasData;
×
139
    }
140

141
    /**
142
     * @return mixed pageId=Data is valid, else FALSE
143
     */
144
    public function checkDataWrite()
×
145
    {
146
        $this->logger->debug('START');
×
147

148
        if ($this->isResultsAvailable()) {
×
149
            $hasData = $this->checkStatus();
×
150
            if ($hasData) {
×
151
                if (!$this->keyExists(IResponse::KEY_KEY) || $this->getValue(IResponse::KEY_KEY) <= 0) {
×
152
                    $this->logger->info('No pageId found or is 0!');
×
153
                    $hasData = false;
×
154
                } else {
155
                    $pageId = $this->getValue(IResponse::KEY_KEY);
×
156
                    $this->logger->info('Write to pageId', [$pageId]);
×
157
                    $hasData = $pageId;
×
158
                }
159
            }
160
        } else {
161
            $this->logger->info('Results are not available!');
×
162
            $hasData = false;
×
163
        }
164

165
        $this->logger->debug('END - hasData', [$hasData]);
×
166

167
        return $hasData;
×
168
    }
169

170
    /**
171
     * @return Map<mixed,mixed>
172
     */
173
    public function getResults(): Map
3✔
174
    {
175
        return $this->results;
3✔
176
    }
177

178
    /**
179
     * @param int $idx
180
     *
181
     * @return mixed
182
     */
183
    public function getResult(int $idx)
×
184
    {
185
        $result = null;
×
186
        if ($this->isResultsAvailable()) {
×
187
            $result = $this->results->toArray()[$idx];
×
188
        }
189

190
        return $result;
×
191
    }
192

193
    public function isResultsAvailable(): bool
×
194
    {
195
        return !$this->results->isEmpty();
×
196
    }
197

198
    /**
199
     * @return mixed[]
200
     *
201
     * @SuppressWarnings("PHPMD.CamelCaseMethodName")
202
     */
203
    protected function __toStringValues()
×
204
    {
205
        return [self::KEY_RESPONSE => $this->response, self::KEY_RESULTS => $this->results];
×
206
    }
207

208
    /**
209
     * @param null|array<mixed,mixed> $data
210
     */
211
    private function prepareData(?array $data = []): void
4✔
212
    {
213
        if (is_null($data)) {
4✔
214
            $data = [];
×
215
        }
216
        if (array_key_exists(self::KEY_RESULTS, $data)) {
4✔
217
            $this->results = new Map($data[self::KEY_RESULTS]);
×
218
            unset($data[self::KEY_RESULTS]);
×
219
        } else {
220
            $this->results = new Map([]);
4✔
221
        }
222
        $this->response = new Map($data);
4✔
223
    }
224
}
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