• 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

10.64
/src/Yacorapi/Client/RapiClient.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\Client;
15

16
use Ds\Map;
17
use Monolog\ConsoleLogger;
18
use oglowa\tools\Yacorapi\ConstData;
19
use oglowa\tools\Yacorapi\Data\RequestParameterData;
20
use oglowa\tools\Yacorapi\IRapiClient;
21
use oglowa\tools\Yacorapi\IResponse;
22
use oglowa\tools\Yacorapi\Macro\AllMacro;
23
use oglowa\tools\Yacorapi\Projectdoc\Extension\ProjectdocExtension;
24
use oglowa\tools\Yacorapi\Request\RequestType;
25
use oglowa\tools\Yacorapi\Response\ResponseAddonMacroDecorate;
26
use oglowa\tools\Yacorapi\Response\ResponseSpaceDataDecorate;
27
use oglowa\tools\Yacorapi\Statistic\IStatistic;
28
use oglowa\tools\Yacorapi\Statistic\PagetypeStatistic;
29
use oglowa\tools\Yacorapi\Statistic\ValueStatistic;
30
use Psr\Log\LoggerInterface;
31

32
/**
33
 * @SuppressWarnings("PHPMD.TooManyPublicMethods")
34
 * @SuppressWarnings("PHPMD.CouplingBetweenObjects")
35
 */
36
class RapiClient extends AbstractRapiClient // NOSONAR: php:S1448
37
{
38
    /** @var ProjectdocExtension
39
     * @psalm-suppress PropertyNotSetInConstructor
40
     * @phpstan-ignore property.onlyWritten
41
     */
42
    private $projectdocExtension;
43

44
    /** @var LoggerInterface */
45
    private $logger;
46

47
    /**
48
     * @inheritdoc
49
     */
50
    public static function newClient(): IRapiClient
2✔
51
    {
52
        return new RapiClient();
2✔
53
    }
54

55
    /**
56
     * RapiClient constructor.
57
     *
58
     * @param int $modeExtension
59
     */
60
    protected function __construct(
2✔
61
        int $modeExtension = ConstData::EXTENSION_COMMON_ON + ConstData::EXTENSION_ADMIN_ON + ConstData::EXTENSION_PROJECTDOC_ON
62
    ) {
63
        // Init Logger
64
        $this->logger = new ConsoleLogger(RapiClient::class);
2✔
65
        $this->logger->debug('START');
2✔
66
        parent::__construct($modeExtension);
2✔
67

68
        $this->logger->debug('END');
2✔
69
    }
70

71
    // Public methods
72

73
    /**
74
     * @inheritdoc
75
     */
76
    public function readPageByPageId(int $pageId): IResponse
1✔
77
    {
78
        $this->logger->debug('START - pageId', [$pageId]);
1✔
79

80
        $prepareUrl = $this->commonExtension->prepareLoadUrl($pageId);
1✔
81

82
        return $this->exec($prepareUrl);
1✔
83
    }
84

85
    /**
86
     * @inheritdoc
87
     */
88
    public function readPagesWithFilter(string $filterTerm, string $spaceKey = ''): IResponse
×
89
    {
90
        $this->logger->debug('START - filterTerm,spaceKey', [$filterTerm, $spaceKey]);
×
91

92
        $prepareUrl = $this->commonExtension->prepareBrowseUrl($filterTerm, $spaceKey);
×
93

94
        return $this->exec($prepareUrl);
×
95
    }
96

97
    /**
98
     * @inheritdoc
99
     */
100
    public function scanPagesWithFilter(string $filterTerm, string $spaceKey = ''): IResponse
×
101
    {
102
        $this->logger->debug('START - filterTerm,spaceKey', [$filterTerm, $spaceKey]);
×
103

104
        $prepareUrl = $this->commonExtension->prepareScanUrl($filterTerm, $spaceKey);
×
105

106
        return $this->exec($prepareUrl);
×
107
    }
108

109
    /**
110
     * @inheritdoc
111
     */
112
    public function searchPagesWithFilter(
×
113
        string $filterTerm,
114
        string $spaceKey,
115
        int $searchFromPos = RequestParameterData::SEARCH_START,
116
        int $searchLimit = RequestParameterData::SEARCH_LIMIT_ZERO,
117
        string $itemType = RequestParameterData::ITEM_TYPE_PAGE
118
    ): IResponse {
119
        $this->logger->debug(
×
120
            'START - filterTerm,spaceKey,searchFromPos,searchLimit,itemType',
×
121
            [$filterTerm, $spaceKey, $searchFromPos, $searchLimit, $itemType]
×
122
        );
×
123
        $searchLimit = (int)($searchLimit < RequestParameterData::SEARCH_LIMIT_1ENTRY ? $this->constData->c(ConstData::KEY_SEARCH_LIMIT) : $searchLimit);
×
124
        $prepareUrl  = $this->commonExtension->prepareSearchUrlExt($filterTerm, $spaceKey, $searchFromPos, $searchLimit, $itemType);
×
125

126
        return $this->exec($prepareUrl);
×
127
    }
128

129
    /**
130
     * @inheritdoc
131
     */
132
    public function countItemsinSpace(string $spaceKey, string $itemType = RequestParameterData::ITEM_TYPE_PAGE): IStatistic
×
133
    {
134
        $this->logger->debug('START - spaceKey, itemType', [$spaceKey, $itemType]);
×
135

136
        $prepareUrl = $this->commonExtension->prepareCountItemsUrl($itemType, $spaceKey);
×
137
        $response   = $this->exec($prepareUrl);
×
138

139
        $itemCount      = $response->getValue(IResponse::KEY_TOTAL_SIZE, 0);
×
140
        $valueStatistic = new ValueStatistic(ValueStatistic::KEY_COUNT);
×
141
        $valueStatistic->addValue($itemCount);
×
142
        $singleStatistic = new PagetypeStatistic($itemType);
×
143
        $singleStatistic->addItem($itemType, $valueStatistic);
×
144

145
        $this->logger->debug('END');
×
146

147
        return $singleStatistic;
×
148
    }
149

150
    /**
151
     * @inheritdoc
152
     */
153
    public function readRestrictionsByPageId(int $pageId): IResponse
×
154
    {
155
        $this->logger->debug('START - pageId', [$pageId]);
×
156

157
        $prepareUrl = $this->adminExtension->prepareRestrictByOpUrl($pageId);
×
158

159
        return $this->exec($prepareUrl);
×
160
    }
161

162
    /**
163
     * REFACTOR: API-Function doesn't work or description is wrong.
164
     * {@inheritdoc}
165
     */
166
    public function writeRestrictionsByPageId(int $pageId, array $writeRestrictions = [], array $readRestrictions = []): bool // NOSONAR: php:S1172
×
167
    {
168
        throw new \BadMethodCallException('API-Function does not work or description is wrong');
×
169
    }
170

171
    /**
172
     * REFACTOR: Listing only 100 spaces, loop is missing.
173
     * {@inheritdoc}
174
     */
175
    public function listSpaces(string $spaceType = RequestParameterData::SPACE_TYPE_GLOBAL, int $limit = RequestParameterData::SPACE_LIMIT_DEFAULT): IResponse
×
176
    {
177
        $this->logger->debug('START - spaceType,limit', [$spaceType, $limit]);
×
178

179
        $prepareUrl = $this->commonExtension->prepareSpaceListUrl($spaceType, $limit);
×
180

181
        return new ResponseSpaceDataDecorate($this->exec($prepareUrl));
×
182
    }
183

184
    /**
185
     * @inheritdoc
186
     */
187
    public function countMacrosInSpace(string $spaceKey, ResponseAddonMacroDecorate $addonSet, IStatistic $outputMatrix): IStatistic
×
188
    {
189
        $this->logger->debug('START - spaceKey,addonSet', [$spaceKey, $addonSet]);
×
190

191
        $mapAddons = $addonSet->getResponse();
×
192

NEW
193
        $response = $this->loopAddons($spaceKey, $addonSet->getMode(), $mapAddons, $outputMatrix);
×
194

195
        $this->logger->debug('END');
×
196

197
        return $response;
×
198
    }
199

200
    /**
201
     * @inheritdoc
202
     */
203
    public function movePage(int $pageId, int $newParentId): IResponse
×
204
    {
205
        $this->logger->debug('START - pageId,newParentId', [$pageId, $newParentId]);
×
206

207
        $page = $this->readPageByPageId($pageId);
×
208

209
        $pageVersion = $page->getValue(RequestParameterData::PROP_VERSION, []);
×
NEW
210
        if (is_array($pageVersion) && array_key_exists(RequestParameterData::PROP_NUMBER, $pageVersion)) {
×
211
            $pageVersion = (int)$pageVersion[RequestParameterData::PROP_NUMBER];
×
212
        } else {
213
            $pageVersion = 1;
×
214
        }
215

216
        $parameters = new Map();
×
217
        $parameters->put(RequestParameterData::PROP_TITLE, $page->getValue(RequestParameterData::PROP_TITLE));
×
218
        $parameters->put(RequestParameterData::PROP_TYPE, $page->getValue(RequestParameterData::PROP_TYPE));
×
219
        $parameters->put(RequestParameterData::PROP_ANCESTORS, [[RequestParameterData::PROP_ID => $newParentId]]);
×
220
        $parameters->put(
×
221
            RequestParameterData::PROP_VERSION,
×
NEW
222
            [
×
NEW
223
                RequestParameterData::PROP_NUMBER  => ++$pageVersion, // NOSONAR php:S881
×
NEW
224
                RequestParameterData::PROP_MESSAGE => self::MSG_MOVED_TO_NEW_PARENT . $newParentId
×
NEW
225
            ]
×
226
        );
×
227

228
        $prepareUrl = $this->commonExtension->prepareUpdateURL($pageId);
×
229
        $response   = $this->execPost($prepareUrl, $parameters, RequestType::REQ_TYP_PUT);
×
230

231
        $this->logger->debug('END');
×
232

233
        return $response;
×
234
    }
235

236
    /**
237
     * @inheritdoc
238
     */
239
    public function createPage(
×
240
        string $spaceKey,
241
        string $pageTitle,
242
        string $pageBody,
243
        ?int $parentId = null,
244
        string $itemType = RequestParameterData::ITEM_TYPE_PAGE
245
    ): IResponse {
246
        $this->logger->debug('START - spaceKey,pageTitle,parentId,pageType,pageBody', [$spaceKey, $pageTitle, $parentId, $itemType, empty($pageBody)]);
×
247

248
        /** @var Map<mixed,mixed> */
249
        $parameters = new Map(
×
250
            [
×
251
                RequestParameterData::PROP_TYPE   => $itemType,
×
252
                RequestParameterData::PROP_TITLE  => $pageTitle,
×
253
                RequestParameterData::PROP_STATUS => RequestParameterData::STATUS_TYPE_CURRENT,
×
254
                RequestParameterData::PROP_BODY   => [
×
255
                    RequestParameterData::PROP_STORAGE => [
×
256
                        RequestParameterData::PROP_VALUE          => $pageBody,
×
257
                        RequestParameterData::PROP_REPRESENTATION => RequestParameterData::REPRESENTATION_TYPE_STORAGE,
×
258
                    ],
×
259
                ],
×
260
            ]
×
261
        );
×
262
        if (empty($spaceKey)) {
×
263
            throw new \InvalidArgumentException(self::MSG_SPACE_IS_EMPTY);
×
264
        } else {
265
            $parameters->put(RequestParameterData::PROP_SPACE, [RequestParameterData::PROP_KEY => $spaceKey]);
×
266
        }
267
        if (is_numeric($parentId)) {
×
268
            $parameters->put(RequestParameterData::PROP_ANCESTORS, [RequestParameterData::PROP_ID => $parentId]);
×
269
        } else {
270
            throw new \InvalidArgumentException(self::MSG_PARENT_ID_MUST_BE_NUMERIC);
×
271
        }
272
        $prepareUrl = $this->commonExtension->prepareCreatePage();
×
273
        $response   = $this->execPost($prepareUrl, $parameters, RequestType::REQ_TYP_POST);
×
274

275
        $this->logger->debug('END');
×
276

277
        return $response;
×
278
    }
279

280
    /**
281
     * @inheritdoc
282
     */
283
    public function updatePage(int $pageId, $pageBody, $pageTitle = '', $comment = '', $itemType = RequestParameterData::ITEM_TYPE_PAGE): bool
×
284
    {
285
        $this->logger->debug('START - pageId,pageTitle,pageType,bodySize,comment', [$pageId, $pageTitle, $itemType, strlen($pageBody), $comment]);
×
286
        $success = false;
×
287

288
        if (empty($pageId)) {
×
289
            $currentPage    = $this->readPageByPageId($pageId);
×
290
            $currentVersion = (int)$currentPage->getValue(RequestParameterData::PROP_VERSION, 1);
×
291
            $nextVersion    = $currentVersion + 1;
×
292

293
            if (empty($comment)) {
×
294
                $comment = self::MSG_UPDATE_PAGE_WITHOUT_CHANGES;
×
295
            }
296
            $prepareURL = $this->commonExtension->prepareUpdateURL($pageId);
×
297
            $parameters = new Map(
×
298
                [
×
299
                    RequestParameterData::PROP_ID      => $pageId,
×
300
                    RequestParameterData::PROP_TYPE    => $itemType,
×
301
                    RequestParameterData::PROP_TITLE   => $pageTitle,
×
302
                    RequestParameterData::PROP_BODY    => [
×
303
                        RequestParameterData::PROP_STORAGE => [
×
304
                            RequestParameterData::PROP_VALUE          => $pageBody,
×
305
                            RequestParameterData::PROP_REPRESENTATION => RequestParameterData::REPRESENTATION_TYPE_STORAGE
×
306
                        ]
×
307
                    ],
×
308
                    RequestParameterData::PROP_VERSION => [RequestParameterData::PROP_NUMBER => $nextVersion, RequestParameterData::PROP_MESSAGE => $comment],
×
309
                ]
×
310
            );
×
311

312
            $response = $this->execPost($prepareURL, $parameters, RequestType::REQ_TYP_PUT);
×
313
            $success  = $response->checkData();
×
314
            $this->logger->debug('Update page with title', [$pageId, $pageTitle, ($success ? 'successful' : 'failed')]);
×
315
        } else {
316
            $this->logger->error('No correct pageId', [$pageId]);
×
317
        }
318
        $this->logger->debug('END');
×
319

320
        return $success;
×
321
    }
322

323
    /**
324
     * @inheritdoc
325
     */
326
    public function prepareAddonSet($mode = AllMacro::MACRO_ALL): ResponseAddonMacroDecorate
×
327
    {
328
        $this->logger->debug('START - mode', [$mode]);
×
329

330
        $addonSet = new ResponseAddonMacroDecorate($mode, $this->addons->getDataByMode($mode));
×
331

332
        $this->logger->debug('END');
×
333

334
        return $addonSet;
×
335
    }
336

337
    // Private methods
338

339
    /**
340
     * @param int $modeExtension
341
     */
342
    protected function initExtensions(int $modeExtension): void
2✔
343
    {
344
        parent::initExtensions($modeExtension);
2✔
345
        if ((ConstData::EXTENSION_PROJECTDOC_ON & $modeExtension) == ConstData::EXTENSION_PROJECTDOC_ON) {
2✔
346
            $this->projectdocExtension = new ProjectdocExtension();
2✔
347
        } else {
348
            $this->logger->notice('ProjectdocExtension not loaded');
×
349
        }
350
    }
351
}
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