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

The-oGlow / ya-corapi / 20730329328

05 Jan 2026 09:54PM UTC coverage: 50.144% (+3.8%) from 46.354%
20730329328

push

github

oglowa
#3: Update files / tested

522 of 537 new or added lines in 20 files covered. (97.21%)

153 existing lines in 8 files now uncovered.

1048 of 2090 relevant lines covered (50.14%)

8.43 hits per line

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

84.67
/src/Yacorapi/ConstData.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;
15

16
use Ds\Map;
17
use Monolog\ConsoleLogger;
18
use Monolog\DoNothingLogger;
19
use oglowa\tools\common\AbstractSingleton;
20
use oglowa\tools\Yacorapi\Data\RequestParameterData;
21
use oglowa\tools\Yacorapi\MyAuth as PersonalAuth;
22
use ollily\Tools\EnvironmentVariableTrait;
23
use Psr\Log\LoggerInterface;
24

25
final class ConstData extends AbstractSingleton
26
{
27
    use EnvironmentVariableTrait;
28

29
    //
30
    // Public Consts
31
    // Extension
32
    public const EXTENSION_RAPI_CLIENT = 1;
33

34
    public const EXTENSION_ATLASSIAN = 2;
35

36
    public const EXTENSION_ATLASSIAN_ADMIN = 4;
37

38
    public const EXTENSION_ATLASSIAN_USER_MACRO = 8;
39

40
    public const EXTENSION_THIRD_PARTY = 16;
41

42
    public const EXTENSION_PROJECTDOC_TOOLBOX = 32;
43

44
    public const EXTENSION_MIN = self::EXTENSION_RAPI_CLIENT + self::EXTENSION_ATLASSIAN;
45

46
    public const EXTENSION_ALL = self::EXTENSION_MIN +
47
        self::EXTENSION_ATLASSIAN_ADMIN +
48
        self::EXTENSION_ATLASSIAN_USER_MACRO +
49
        self::EXTENSION_THIRD_PARTY +
50
        self::EXTENSION_PROJECTDOC_TOOLBOX;
51

52
    // Page Consts
53
    public const PAGE_START = 0;
54

55
    public const PAGE_LIMIT = 50;
56

57
    public const PAGE_MAX_PAGES = 20;
58

59
    public const PAGE_MAX_RESULTS = 50 * 20;
60

61
    // Instance Consts
62
    public const KEY_USE_PROD = 'USE_PROD';
63

64
    public const KEY_CONF_BASE_URL = 'CONF_BASE_URL';
65

66
    public const KEY_AUTH_TOKEN_NAME = 'AUTH_TOKEN_NAME';
67

68
    public const KEY_MY_CERT_CA = 'MY_CERT_CA';
69

70
    // Folder Consts
71
    public const KEY_MY_DIR = 'MY_DIR';
72
    //    public const KEY_SCRIPT_NAME_PH    = 'SCRIPT_NAME_PH';
73

74
    public const KEY_PROJECT_ROOT = 'PROJECT_ROOT';
75

76
    public const KEY_TARGET_ROOTDIR = 'TARGET_ROOTDIR';
77

78
    public const KEY_TARGET_DIR = 'TARGET_DIR';
79
    //    public const KEY_TARGET_FILENAME   = 'TARGET_FILENAME';
80

81
    public const KEY_INPUT_ROOTDIR = 'INPUT_ROOTDIR';
82

83
    public const KEY_INPUT_DIR = 'INPUT_DIR';
84

85
    // Url Consts
86
    public const KEY_CONF_CONTENT_URL = 'CONF_CONTENT_URL';
87

88
    public const KEY_CONF_SEARCH_URL = 'CONF_SEARCH_URL';
89

90
    public const KEY_CONF_SPACE_URL = 'CONF_SPACE_URL';
91

92
    // Misc Consts
93
    public const KEY_WEB_SHOW_PAGEID = 'WEB_SHOW_PAGEID';
94

95
    public const KEY_SEARCH_LIMIT = 'SEARCH_LIMIT';
96

97
    public const TARGET_ORGDIR = 'org';
98

99
    public const TARGET_MODDIR = 'mod';
100

101
    //
102
    // Private Consts
103
    // User Configuration Consts
104
    private const CONF_USERCERTFILE = 'cacert.pem';
105

106
    private const CONF_USERAUTHFILE = 'MyAuth.php';
107

108
    private const CONF_USERFOLDER = '.ya-corapi';
109

110
    // Auth Consts
111
    private const CONF_PAT_PROD = 'CONF_PAT_PROD';
112

113
    private const CONF_PAT_TEST = 'CONF_PAT_TEST';
114

115
    private const CLI_LONG_OPTS = [self::KEY_USE_PROD . ':'];
116

117
    /** @var LoggerInterface */
118
    private static $logger;
119

120
    /** @var string */
121
    private static $tsNow;
122

123
    // Variables
124
    /** @var Map<string,scalar> */
125
    private $definedConst;
126

127
    /**
128
     * @var PersonalAuth
129
     *
130
     * @psalm-suppress UndefinedDocblockClass
131
     * @phpstan-ignore class.notFound,property.onlyWritten
132
     */
133
    private $userAuth;
134

135
    public function __construct(string $key = '', bool $withLogger = true)
49✔
136
    {
137
        // Init logger at first
138
        if ($withLogger) {
49✔
139
            self::$logger = new ConsoleLogger(get_class($this));
49✔
140
        } else {
UNCOV
141
            self::$logger = new DoNothingLogger();
×
142
        }
143
        self::$logger->debug('START');
49✔
144

145
        // Init static vars
146
        self::initTsNow();
49✔
147
        parent::__construct($key, $withLogger);
49✔
148

149
        self::$logger->debug('END');
49✔
150
    }
151

152
    /**
153
     * @return string
154
     */
UNCOV
155
    public static function getTsNow(): string
×
156
    {
UNCOV
157
        self::initTsNow();
×
158

UNCOV
159
        return self::$tsNow;
×
160
    }
161

162
    private static function initTsNow(): void
49✔
163
    {
164
        if (empty(self::$tsNow)) {
49✔
165
            self::$tsNow = date('Ymd-His');
1✔
166
        }
167
    }
168

169
    /**
170
     * @param string $constKey
171
     * @param mixed  $default
172
     *
173
     * @return mixed
174
     *
175
     * @SuppressWarnings("PHPMD.ShortMethodName")
176
     */
177
    public function c(string $constKey, $default = null)
8✔
178
    {
179
        return self::getConst($constKey, $default);
8✔
180
    }
181

182
    /**
183
     * @param string $constName
184
     *
185
     * @return bool
186
     */
187
    public function isDefined(string $constName): bool
49✔
188
    {
189
        $found = $this->definedConst->hasKey($constName);
49✔
190
        self::$logger->debug('Const is defined', [$constName, $found]);
49✔
191

192
        return $found;
49✔
193
    }
194

195
    public function prepareFinalTarget(string $pathPre, string $outputFileName, string $pathPost = ''): string
2✔
196
    {
197
        self::$logger->debug('START - pathPre,outputFileName,pathPost ', [$pathPre, $outputFileName, $pathPost]);
2✔
198

199
        $pathMid = dirname($outputFileName);
2✔
200
        if (!empty($pathMid)) {
2✔
201
            $pathMidSplit = explode(DIRECTORY_SEPARATOR, $pathMid);
2✔
202
            $callback     = function (string $val): string {
2✔
203
                return substr($val, 0, 2);
2✔
204
            };
2✔
205
            $pathMid      = implode('-', array_map($callback, $pathMidSplit));
2✔
206
        }
207
        $fullTargetFile = $pathPre . DIRECTORY_SEPARATOR . $pathMid . DIRECTORY_SEPARATOR . basename($outputFileName);
2✔
208
        if (!empty($pathPost)) {
2✔
UNCOV
209
            $fullTargetFile .= DIRECTORY_SEPARATOR . $pathPost;
×
210
        }
211

212
        self::$logger->debug('END - fullTargetFile', [$fullTargetFile]);
2✔
213

214
        return $fullTargetFile;
2✔
215
    }
216

217
    protected function prepareSettings(): void
49✔
218
    {
219
        self::$logger->debug('START');
49✔
220
        /** @psalm-suppress MixedPropertyTypeCoercion */
221
        $this->definedConst = new Map();
49✔
222

223
        $this->putConst(self::KEY_MY_DIR, self::getHome() . DIRECTORY_SEPARATOR . self::CONF_USERFOLDER);
49✔
224
        $this->prepareUserAuthorization((string)$this->definedConst->get(self::KEY_MY_DIR), self::CONF_USERAUTHFILE);
49✔
225

226
        if (class_exists(PersonalAuth::class)) {
49✔
227
            $this->putConst(self::KEY_CONF_BASE_URL, PersonalAuth::CONF_BASE_URL());
49✔
228
        }
229
        $this->defineConsts();
49✔
230

231
        self::$logger->debug('END - Is prepared', ['true']);
49✔
232
    }
233

234
    /**
235
     * @param Map <mixed, mixed> $overrideParameters
236
     *
237
     * @return bool
238
     */
239
    protected function validateSettings(Map $overrideParameters): bool
49✔
240
    {
241
        self::$logger->debug('START');
49✔
242

243
        $valid1 = self::validateMandatory();
49✔
244
        $valid2 = self::validateForProductionUse($overrideParameters);
49✔
245

246
        self::$logger->debug('END - Is valid', [$valid1, $valid2]);
49✔
247

248
        return $valid1 && $valid2;
49✔
249
    }
250

251
    /**
252
     * @param string $authFilePath
253
     * @param string $authFileName
254
     *
255
     * @return bool
256
     *
257
     * @SuppressWarnings("PHPMD.ExitExpression")
258
     */
259
    protected function prepareUserAuthorization(string $authFilePath, string $authFileName): bool
49✔
260
    {
261
        self::$logger->debug('START');
49✔
262

263
        $prepared = false;
49✔
264

265
        $authFile = $authFilePath . DIRECTORY_SEPARATOR . $authFileName;
49✔
266
        if (file_exists($authFile)) {
49✔
267
            include_once $authFile; // NOSONAR: php:S4832
49✔
268
            if (class_exists(PersonalAuth::class)) {
49✔
269
                $this->userAuth = new PersonalAuth();
49✔
270
                $prepared       = true;
49✔
271
            } else {
UNCOV
272
                self::$logger->emergency('User athorization not loaded!', [$authFile]);
×
273

UNCOV
274
                exit(12); // NOSONAR: php:S1799
×
275
            }
276
        } else {
UNCOV
277
            self::$logger->emergency('User athorization not loaded!', [$authFile]);
×
278

UNCOV
279
            exit(11); // NOSONAR: php:S1799
×
280
        }
281

282
        self::$logger->debug('END - Is prepared', [$prepared]);
49✔
283

284
        return $prepared;
49✔
285
    }
286

287
    protected function defineConsts(): void
49✔
288
    {
289
        self::$logger->debug('START');
49✔
290

291
        // Common
292
        $this->putConst(self::KEY_MY_CERT_CA, ((string)self::getConst(self::KEY_MY_DIR)) . DIRECTORY_SEPARATOR . self::CONF_USERCERTFILE);
49✔
293
        $this->putConst(self::KEY_WEB_SHOW_PAGEID, sprintf('%s/pages/viewpage.action?pageId=', self::getConst(self::KEY_CONF_BASE_URL)));
49✔
294

295
        // Urls
296
        $this->putConst(self::KEY_CONF_CONTENT_URL, sprintf('%s/rest/api/content', self::getConst(self::KEY_CONF_BASE_URL)));
49✔
297
        $this->putConst(self::KEY_CONF_SEARCH_URL, sprintf('%s/rest/api/search', self::getConst(self::KEY_CONF_BASE_URL)));
49✔
298
        $this->putConst(self::KEY_CONF_SPACE_URL, sprintf('%s/rest/api/space', self::getConst(self::KEY_CONF_BASE_URL)));
49✔
299

300
        // Folders
301
        $this->putConst(self::KEY_PROJECT_ROOT, realpath(__DIR__ . str_repeat(DIRECTORY_SEPARATOR . '..', 2)));
49✔
302
        $this->putConst(
49✔
303
            self::KEY_TARGET_ROOTDIR,
49✔
304
            sprintf('%s%starget', self::getConst(self::KEY_PROJECT_ROOT), DIRECTORY_SEPARATOR)
49✔
305
        );
49✔
306
        $this->putConst(
49✔
307
            self::KEY_TARGET_DIR,
49✔
308
            sprintf(
49✔
309
                '%s%s%s',
49✔
310
                self::getConst(self::KEY_TARGET_ROOTDIR),
49✔
311
                DIRECTORY_SEPARATOR,
49✔
312
                '' . self::$tsNow
49✔
313
            )
49✔
314
        );
49✔
315
        $this->putConst(
49✔
316
            self::KEY_INPUT_ROOTDIR,
49✔
317
            sprintf('%s%sinput', self::getConst(self::KEY_PROJECT_ROOT), DIRECTORY_SEPARATOR)
49✔
318
        );
49✔
319
        $this->putConst(
49✔
320
            self::KEY_INPUT_DIR,
49✔
321
            sprintf(
49✔
322
                '%s',
49✔
323
                self::getConst(self::KEY_INPUT_ROOTDIR)
49✔
324
            )
49✔
325
        );
49✔
326

327
        self::$logger->debug('END - Is defined', ['true']);
49✔
328
    }
329

330
    /**
331
     * @param Map <mixed, mixed> $overrideParameters
332
     *
333
     * @return bool
334
     */
335
    protected function validateForProductionUse(Map $overrideParameters): bool
49✔
336
    {
337
        self::$logger->debug('START');
49✔
338

339
        $validated = true;
49✔
340

341
        $ovUseProd = $this->parseBool($overrideParameters, self::KEY_USE_PROD);
49✔
342
        if (!empty($ovUseProd)) {
49✔
UNCOV
343
            $ovUseProd = filter_var($ovUseProd, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
×
344
        }
345
        if (is_bool($ovUseProd)) {
49✔
UNCOV
346
            $this->putConst(self::KEY_USE_PROD, $ovUseProd);
×
347
        } else {
348
            if (class_exists(PersonalAuth::class)) {
49✔
349
                $this->putConst(self::KEY_USE_PROD, PersonalAuth::USE_PROD);
49✔
350
            } else {
UNCOV
351
                $this->putConst(self::KEY_USE_PROD, false);
×
352
            }
353
        }
354

355
        if ($this->getConst(self::KEY_USE_PROD, false) === true) {
49✔
UNCOV
356
            self::$logger->info('+++ RUNNING ON PRODUCTION IS OK 4 U? +++');
×
UNCOV
357
            if (!$this->isDefined(self::KEY_AUTH_TOKEN_NAME)) {
×
UNCOV
358
                $this->putConst(self::KEY_AUTH_TOKEN_NAME, self::CONF_PAT_PROD);
×
359
            }
360
        } else {
361
            if (!$this->isDefined(self::KEY_AUTH_TOKEN_NAME)) {
49✔
362
                $this->putConst(self::KEY_AUTH_TOKEN_NAME, self::CONF_PAT_TEST);
49✔
363
            }
364
        }
365

366
        self::$logger->debug('END - Is valid', [$validated]);
49✔
367

368
        return $validated;
49✔
369
    }
370

371
    /**
372
     * @return bool
373
     *
374
     * @SuppressWarnings("PHPMD.ExitExpression")
375
     */
376
    protected function validateMandatory(): bool
49✔
377
    {
378
        self::$logger->debug('START');
49✔
379

380
        $validated = true;
49✔
381

382
        if (!$this->isDefined(self::KEY_CONF_BASE_URL)) {
49✔
383
            self::$logger->emergency('No URL for confluence is set');
×
384
            $validated = true;
×
385

UNCOV
386
            exit(1); // NOSONAR: php:S1799
×
387
        }
388
        if (!$this->isDefined(self::KEY_SEARCH_LIMIT)) {
49✔
389
            $this->putConst(self::KEY_SEARCH_LIMIT, ((string)RequestParameterData::SEARCH_LIMIT_MAX));
49✔
390
        }
391

392
        self::$logger->debug('END - Is valid', [$validated]);
49✔
393

394
        return $validated;
49✔
395
    }
396

397
    /**
398
     * @return array<mixed>
399
     */
400
    protected function prepareLongOpts(): array
49✔
401
    {
402
        return self::CLI_LONG_OPTS;
49✔
403
    }
404

405
    /**
406
     * @param mixed $constName
407
     * @param mixed $constValue
408
     * @param bool  $replace    TRUE=replace constant, if already exists, else FALSE
409
     */
410
    private function putConst($constName, $constValue, bool $replace = true): void
49✔
411
    {
412
        if ($this->isDefined($constName)) {
49✔
413
            if ($replace) {
×
UNCOV
414
                $this->definedConst->put($constName, $constValue);
×
415
            } else {
UNCOV
416
                self::$logger->info("'$constName' exists and will not be replaced.");
×
417
            }
418
        } else {
419
            $this->definedConst->put($constName, $constValue);
49✔
420
        }
421
    }
422

423
    /**
424
     * @param string $constKey
425
     * @param mixed  $default
426
     *
427
     * @return mixed
428
     */
429
    private function getConst(string $constKey, $default = null)
49✔
430
    {
431
        return $this->definedConst->get($constKey, $default);
49✔
432
    }
433
}
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