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

The-oGlow / ya-corapi / 19987901267

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

push

github

web-flow
Push2Master (#5)

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

76.3
/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_COMMON_OFF = 1;
33

34
    public const EXTENSION_COMMON_ON = 2;
35

36
    public const EXTENSION_ADMIN_OFF = 4;
37

38
    public const EXTENSION_ADMIN_ON = 8;
39

40
    public const EXTENSION_PROJECTDOC_OFF = 16;
41

42
    public const EXTENSION_PROJECTDOC_ON = 32;
43

44
    // Page Consts
45
    public const PAGE_START = 0;
46

47
    public const PAGE_LIMIT = 50;
48

49
    public const PAGE_MAX_PAGES = 20;
50

51
    public const PAGE_MAX_RESULTS = 50 * 20;
52

53
    // Instance Consts
54
    public const KEY_USE_PROD = 'USE_PROD';
55

56
    public const KEY_CONF_BASE_URL = 'CONF_BASE_URL';
57

58
    public const KEY_AUTH_TOKEN_NAME = 'AUTH_TOKEN_NAME';
59

60
    public const KEY_MY_CERT_CA = 'MY_CERT_CA';
61

62
    // Folder Consts
63
    public const KEY_MY_DIR = 'MY_DIR';
64
    //    public const KEY_SCRIPT_NAME_PH    = 'SCRIPT_NAME_PH';
65

66
    public const KEY_PROJECT_ROOT = 'PROJECT_ROOT';
67

68
    public const KEY_TARGET_ROOTDIR = 'TARGET_ROOTDIR';
69

70
    public const KEY_TARGET_DIR = 'TARGET_DIR';
71
    //    public const KEY_TARGET_FILENAME   = 'TARGET_FILENAME';
72

73
    public const KEY_INPUT_ROOTDIR = 'INPUT_ROOTDIR';
74

75
    public const KEY_INPUT_DIR = 'INPUT_DIR';
76

77
    // Url Consts
78
    public const KEY_CONF_CONTENT_URL = 'CONF_CONTENT_URL';
79

80
    public const KEY_CONF_SEARCH_URL = 'CONF_SEARCH_URL';
81

82
    public const KEY_CONF_SPACE_URL = 'CONF_SPACE_URL';
83

84
    // Misc Consts
85
    public const KEY_WEB_SHOW_PAGEID = 'WEB_SHOW_PAGEID';
86

87
    public const KEY_SEARCH_LIMIT = 'SEARCH_LIMIT';
88

89
    public const TARGET_ORGDIR = 'org';
90

91
    public const TARGET_MODDIR = 'mod';
92

93
    //
94
    // Private Consts
95
    // User Configuration Consts
96
    private const CONF_USERCERTFILE = 'cacert.pem';
97

98
    private const CONF_USERAUTHFILE = 'MyAuth.php';
99

100
    private const CONF_USERFOLDER = '.ya-corapi';
101

102
    // Auth Consts
103
    private const CONF_PAT_PROD = 'CONF_PAT_PROD';
104

105
    private const CONF_PAT_TEST = 'CONF_PAT_TEST';
106

107
    private const CLI_LONG_OPTS  = [self::KEY_USE_PROD . ':'];
108

109
    /** @var LoggerInterface */
110
    private static $logger;
111

112
    /** @var string */
113
    private static $tsNow;
114

115
    // Variables
116
    /** @var Map<string,scalar> */
117
    private $definedConst;
118

119
    /**
120
     * @var PersonalAuth
121
     *
122
     * @psalm-suppress UndefinedDocblockClass
123
     * @phpstan-ignore class.notFound,property.onlyWritten
124
     */
125
    private $userAuth;
126

127
    public function __construct(string $key = '', bool $withLogger = true)
18✔
128
    {
129
        // Init logger at first
130
        if ($withLogger) {
18✔
131
            self::$logger = new ConsoleLogger(get_class($this));
18✔
132
        } else {
133
            self::$logger = new DoNothingLogger();
×
134
        }
135
        self::$logger->debug('START');
18✔
136

137
        // Init static vars
138
        self::initTsNow();
18✔
139
        parent::__construct($key, $withLogger);
18✔
140

141
        self::$logger->debug('END');
18✔
142
    }
143

144
    /**
145
     * @return string
146
     */
147
    public static function getTsNow(): string
×
148
    {
149
        self::initTsNow();
×
150

151
        return self::$tsNow;
×
152
    }
153

154
    private static function initTsNow(): void
18✔
155
    {
156
        if (empty(self::$tsNow)) {
18✔
157
            self::$tsNow = date('Ymd-His');
1✔
158
        }
159
    }
160

161
    /**
162
     * @param string $constKey
163
     * @param mixed  $default
164
     *
165
     * @return mixed
166
     *
167
     * @SuppressWarnings("PHPMD.ShortMethodName")
168
     */
169
    public function c(string $constKey, $default = null)
6✔
170
    {
171
        return self::getConst($constKey, $default);
6✔
172
    }
173

174
    /**
175
     * @param string $constName
176
     *
177
     * @return bool
178
     */
179
    public function isDefined(string $constName): bool
18✔
180
    {
181
        $found = $this->definedConst->hasKey($constName);
18✔
182
        self::$logger->debug('Const is defined', [$constName, $found]);
18✔
183

184
        return $found;
18✔
185
    }
186

187
    public function prepareFinalTarget(string $pathPre, string $outputFileName, string $pathPost = ''): string
×
188
    {
189
        self::$logger->debug('START - pathPre,outputFileName,pathPost ', [$pathPre, $outputFileName, $pathPost]);
×
190

191
        $pathMid = dirname($outputFileName);
×
192
        if (!empty($pathMid)) {
×
193
            $pathMidSplit = explode(DIRECTORY_SEPARATOR, $pathMid);
×
194
            $callback     = function (string $val): string {
195
                return substr($val, 0, 2);
×
196
            };
197
            $pathMid      = implode('-', array_map($callback, $pathMidSplit));
×
198
        }
199
        $fullTargetFile = $pathPre . DIRECTORY_SEPARATOR . $pathMid . DIRECTORY_SEPARATOR . basename($outputFileName);
×
200
        if (!empty($pathPost)) {
×
201
            $fullTargetFile .= DIRECTORY_SEPARATOR . $pathPost;
×
202
        }
203

204
        self::$logger->debug('END - fullTargetFile', [$fullTargetFile]);
×
205

206
        return $fullTargetFile;
×
207
    }
208

209
    protected function prepareSettings(): void
18✔
210
    {
211
        self::$logger->debug('START');
18✔
212
        /** @psalm-suppress MixedPropertyTypeCoercion */
213
        $this->definedConst = new Map();
18✔
214

215
        $this->putConst(self::KEY_MY_DIR, self::getHome() . DIRECTORY_SEPARATOR . self::CONF_USERFOLDER);
18✔
216
        $this->prepareUserAuthorization((string)$this->definedConst->get(self::KEY_MY_DIR), self::CONF_USERAUTHFILE);
18✔
217

218
        if (class_exists(PersonalAuth::class)) {
18✔
219
            $this->putConst(self::KEY_CONF_BASE_URL, PersonalAuth::CONF_BASE_URL());
18✔
220
        }
221
        $this->defineConsts();
18✔
222

223
        self::$logger->debug('END - Is prepared', ['true']);
18✔
224
    }
225

226
    /**
227
     * @param Map<mixed, mixed> $overrideParameters
228
     *
229
     * @return bool
230
     */
231
    protected function validateSettings(Map $overrideParameters): bool
18✔
232
    {
233
        self::$logger->debug('START');
18✔
234

235
        $valid1 = self::validateMandatory();
18✔
236
        $valid2 = self::validateForProductionUse($overrideParameters);
18✔
237

238
        self::$logger->debug('END - Is valid', [$valid1, $valid2]);
18✔
239

240
        return $valid1 && $valid2;
18✔
241
    }
242

243
    /**
244
     * @param string $authFilePath
245
     * @param string $authFileName
246
     *
247
     * @return bool
248
     *
249
     * @SuppressWarnings("PHPMD.ExitExpression")
250
     */
251
    protected function prepareUserAuthorization(string $authFilePath, string $authFileName): bool
18✔
252
    {
253
        self::$logger->debug('START');
18✔
254

255
        $prepared = false;
18✔
256

257
        $authFile = $authFilePath . DIRECTORY_SEPARATOR . $authFileName;
18✔
258
        if (file_exists($authFile)) {
18✔
259
            include_once $authFile; // NOSONAR: php:S4832
18✔
260
            if (class_exists(PersonalAuth::class)) {
18✔
261
                $this->userAuth = new PersonalAuth();
18✔
262
                $prepared       = true;
18✔
263
            } else {
264
                self::$logger->emergency('User athorization not loaded!', [$authFile]);
×
265

266
                exit(12); // NOSONAR: php:S1799
×
267
            }
268
        } else {
269
            self::$logger->emergency('User athorization not loaded!', [$authFile]);
×
270

271
            exit(11); // NOSONAR: php:S1799
×
272
        }
273

274
        self::$logger->debug('END - Is prepared', [$prepared]);
18✔
275

276
        return $prepared;
18✔
277
    }
278

279
    protected function defineConsts(): void
18✔
280
    {
281
        self::$logger->debug('START');
18✔
282

283
        // Common
284
        $this->putConst(self::KEY_MY_CERT_CA, ((string)self::getConst(self::KEY_MY_DIR)) . DIRECTORY_SEPARATOR . self::CONF_USERCERTFILE);
18✔
285
        $this->putConst(self::KEY_WEB_SHOW_PAGEID, sprintf('%s/pages/viewpage.action?pageId=', self::getConst(self::KEY_CONF_BASE_URL)));
18✔
286

287
        // Urls
288
        $this->putConst(self::KEY_CONF_CONTENT_URL, sprintf('%s/rest/api/content', self::getConst(self::KEY_CONF_BASE_URL)));
18✔
289
        $this->putConst(self::KEY_CONF_SEARCH_URL, sprintf('%s/rest/api/search', self::getConst(self::KEY_CONF_BASE_URL)));
18✔
290
        $this->putConst(self::KEY_CONF_SPACE_URL, sprintf('%s/rest/api/space', self::getConst(self::KEY_CONF_BASE_URL)));
18✔
291

292
        // Folders
293
        $this->putConst(self::KEY_PROJECT_ROOT, realpath(__DIR__ . str_repeat(DIRECTORY_SEPARATOR . '..', 2)));
18✔
294
        $this->putConst(
18✔
295
            self::KEY_TARGET_ROOTDIR,
18✔
296
            sprintf('%s%starget', self::getConst(self::KEY_PROJECT_ROOT), DIRECTORY_SEPARATOR)
18✔
297
        );
18✔
298
        $this->putConst(
18✔
299
            self::KEY_TARGET_DIR,
18✔
300
            sprintf(
18✔
301
                '%s%s%s',
18✔
302
                self::getConst(self::KEY_TARGET_ROOTDIR),
18✔
303
                DIRECTORY_SEPARATOR,
18✔
304
                '' . self::$tsNow
18✔
305
            )
18✔
306
        );
18✔
307
        $this->putConst(
18✔
308
            self::KEY_INPUT_ROOTDIR,
18✔
309
            sprintf('%s%sinput', self::getConst(self::KEY_PROJECT_ROOT), DIRECTORY_SEPARATOR)
18✔
310
        );
18✔
311
        $this->putConst(
18✔
312
            self::KEY_INPUT_DIR,
18✔
313
            sprintf(
18✔
314
                '%s',
18✔
315
                self::getConst(self::KEY_INPUT_ROOTDIR)
18✔
316
            )
18✔
317
        );
18✔
318

319
        self::$logger->debug('END - Is defined', ['true']);
18✔
320
    }
321

322
    /**
323
     * @param Map<mixed, mixed> $overrideParameters
324
     *
325
     * @return bool
326
     */
327
    protected function validateForProductionUse(Map $overrideParameters): bool
18✔
328
    {
329
        self::$logger->debug('START');
18✔
330

331
        $validated = true;
18✔
332

333
        $ovUseProd = $this->parseBool($overrideParameters, self::KEY_USE_PROD);
18✔
334
        if (!empty($ovUseProd)) {
18✔
335
            $ovUseProd = filter_var($ovUseProd, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
×
336
        }
337
        if (is_bool($ovUseProd)) {
18✔
338
            $this->putConst(self::KEY_USE_PROD, $ovUseProd);
×
339
        } else {
340
            if (class_exists(PersonalAuth::class)) {
18✔
341
                $this->putConst(self::KEY_USE_PROD, PersonalAuth::USE_PROD);
18✔
342
            } else {
343
                $this->putConst(self::KEY_USE_PROD, false);
×
344
            }
345
        }
346

347
        if ($this->getConst(self::KEY_USE_PROD, false) === true) {
18✔
348
            self::$logger->info('+++ RUNNING ON PRODUCTION IS OK 4 U? +++');
×
349
            if (!$this->isDefined(self::KEY_AUTH_TOKEN_NAME)) {
×
350
                $this->putConst(self::KEY_AUTH_TOKEN_NAME, self::CONF_PAT_PROD);
×
351
            }
352
        } else {
353
            if (!$this->isDefined(self::KEY_AUTH_TOKEN_NAME)) {
18✔
354
                $this->putConst(self::KEY_AUTH_TOKEN_NAME, self::CONF_PAT_TEST);
18✔
355
            }
356
        }
357

358
        self::$logger->debug('END - Is valid', [$validated]);
18✔
359

360
        return $validated;
18✔
361
    }
362

363
    /**
364
     * @return bool
365
     *
366
     * @SuppressWarnings("PHPMD.ExitExpression")
367
     */
368
    protected function validateMandatory(): bool
18✔
369
    {
370
        self::$logger->debug('START');
18✔
371

372
        $validated = true;
18✔
373

374
        if (!$this->isDefined(self::KEY_CONF_BASE_URL)) {
18✔
375
            self::$logger->emergency('No URL for confluence is set');
×
376
            $validated = true;
×
377

378
            exit(1); // NOSONAR: php:S1799
×
379
        }
380
        if (!$this->isDefined(self::KEY_SEARCH_LIMIT)) {
18✔
381
            $this->putConst(self::KEY_SEARCH_LIMIT, ((string)RequestParameterData::SEARCH_LIMIT_MAX));
18✔
382
        }
383

384
        self::$logger->debug('END - Is valid', [$validated]);
18✔
385

386
        return $validated;
18✔
387
    }
388

389
    /**
390
     * @return array<mixed>
391
     */
392
    protected function prepareLongOpts(): array
18✔
393
    {
394
        return self::CLI_LONG_OPTS;
18✔
395
    }
396

397
    /**
398
     * @param mixed $constName
399
     * @param mixed $constValue
400
     * @param bool  $replace    TRUE=replace constant, if already exists, else FALSE
401
     */
402
    private function putConst($constName, $constValue, bool $replace = true): void
18✔
403
    {
404
        if ($this->isDefined($constName)) {
18✔
405
            if ($replace) {
×
406
                $this->definedConst->put($constName, $constValue);
×
407
            } else {
408
                self::$logger->info("'$constName' exists and will not be replaced.");
×
409
            }
410
        } else {
411
            $this->definedConst->put($constName, $constValue);
18✔
412
        }
413
    }
414

415
    /**
416
     * @param string $constKey
417
     * @param mixed  $default
418
     *
419
     * @return mixed
420
     */
421
    private function getConst(string $constKey, $default = null)
18✔
422
    {
423
        return $this->definedConst->get($constKey, $default);
18✔
424
    }
425
}
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