• 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

0.0
/src/common/AbstractStoreAdapter.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\common;
15

16
use Monolog\ConsoleLogger;
17
use oglowa\tools\Yacorapi\ConstData;
18
use oglowa\tools\Yacorapi\Store\FileStoreItem;
19
use Psr\Log\LoggerInterface;
20

21
abstract class AbstractStoreAdapter implements IStoreAdapter
22
{
23
    /** Field Separator */
24
    public const            C_ITEM_SEP = ';';
25

26
    protected const         C_DIR_REK = true;
27

28
    protected const         C_CODE_UTF8 = 'UTF-8';
29

30
    protected const         C_CHAR_LENGTH = 1000;
31

32
    protected const         C_FILE_READ = 'r';
33

34
    protected const         C_DIR_MASK = 0777;
35

36
    /** @var ConstData */
37
    protected $constData;
38

39
    /** @var LoggerInterface */
40
    private $logger;
41

42
    /** @var string */
43
    private $sessionTargetDir;
44

45
    /** @var IStoreItem
46
     * @psalm-suppress PropertyNotSetInConstructor
47
     */
48
    protected $storeItem;
49

50
    public function __construct(string $outputFileName, string $fileSuffix = '', string $customTargetDir = '')
×
51
    {
52
        $this->logger = new ConsoleLogger(AbstractStoreAdapter::class);
×
53
        $this->logger->debug("START");
×
54

55
        // Init Dynamic Consts
56
        $this->constData        = new ConstData(get_class($this));
×
57
        $this->sessionTargetDir = $this->prepareTargetFolder(
×
58
            $outputFileName,
×
59
            $this->constData->c(ConstData::KEY_TARGET_ROOTDIR),
×
60
            $this->constData->c(ConstData::KEY_TARGET_DIR)
×
61
        );
×
62
        $this->prepareTargetFolderSpecial($this->sessionTargetDir, ConstData::TARGET_ORGDIR, ConstData::TARGET_MODDIR);
×
63
        $this->storeItem = $this->prepareStoreItem($outputFileName, $fileSuffix, $customTargetDir);
×
64

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

68
    protected function prepareTargetFolder(string $outputFileName, string $targetRootDir, string $targetDir): string
×
69
    {
70
        $this->logger->debug('START');
×
71

72
        $sessionDir = $this->constData->prepareFinalTarget($targetDir, $outputFileName);
×
73

74
        $this->logger->debug('create TARGET_ROOT', [$targetRootDir]);
×
75
        $this->mkdir($targetRootDir);
×
76
        $this->logger->debug('create session folder in TARGET_DIR', [$sessionDir]);
×
77
        $this->mkdir($sessionDir);
×
78

79
        $this->logger->debug('END');
×
80

81
        return $sessionDir;
×
82
    }
83

84
    protected function prepareTargetFolderSpecial(string $sessionDir, string $orgDir, string $modDir): void
×
85
    {
86
        $this->logger->debug('START');
×
87

88
        if (file_exists($sessionDir)) {
×
89
            $targetOrgDir = $this->constData->prepareFinalTarget($sessionDir, $orgDir);
×
90
            $targetModDir = $this->constData->prepareFinalTarget($sessionDir, $modDir);
×
91

92
            $this->logger->debug('create TARGET_ORG_DIR & TARGET_MOD_DIR', [$targetOrgDir, $targetModDir]);
×
93
            $this->mkdir($targetOrgDir);
×
94
            $this->mkdir($targetModDir);
×
95
        } else {
96
            $this->logger->warning('session folder does not exists!', [$sessionDir]);
×
97
        }
98

99
        $this->logger->debug('END');
×
100
    }
101

102
    protected function prepareStoreItem(string $outputFileName, string $fileSuffix, string $customTargetDir): IStoreItem
×
103
    {
104
        return $this->invokeStoreItem($customTargetDir, $this->extendNameWithSuffix($outputFileName, $fileSuffix));
×
105
    }
106

107
    protected function extendNameWithSuffix(string $outputFileName, string $suffix = ''): string
×
108
    {
109
        $this->logger->debug("START");
×
110

111
        $fileName = basename($outputFileName);
×
112
        if (!empty($suffix)) {
×
113
            $fileName .= '-' . $suffix;
×
114
        }
115

116
        return $fileName;
×
117
    }
118

119
    protected function mkdir(string $folder): bool
×
120
    {
121
        if (!file_exists($folder)) {
×
122
            return mkdir($folder, self::C_DIR_MASK, self::C_DIR_REK);
×
123
        } else {
124
            return true;
×
125
        }
126
    }
127

128
    /**
129
     * @param string|string[] $dataHeader
130
     *
131
     * @return string
132
     */
133
    protected function flattenDataHeader($dataHeader): string
×
134
    {
135
        $this->logger->debug("START");
×
136

137
        $header = "";
×
138
        if (!empty($dataHeader)) {
×
139
            if (!is_array($dataHeader)) {
×
140
                $dataHeader = [$dataHeader];
×
141
            }
142
            $header = implode(self::C_ITEM_SEP, $dataHeader);
×
143
        }
144

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

147
        return $header;
×
148
    }
149

150
    /**
151
     * @param IStoreItem $targetFile
152
     * @param mixed      $anyData
153
     */
154
    final protected function writeData(IStoreItem $targetFile, $anyData): void
×
155
    {
156
        $this->logger->debug("START - targetFile", [$targetFile]);
×
157

158
        if (!is_null($anyData)) {
×
159
            $targetFolder = dirname($targetFile->__toString());
×
160
            $this->mkdir($targetFolder);
×
161
            file_put_contents($targetFile->__toString(), $anyData, FILE_APPEND);
×
162
            file_put_contents($targetFile->__toString(), "\n", FILE_APPEND);
×
163
        }
164

165
        $this->logger->debug('END');
×
166
    }
167

168
    /**
169
     * @param string $customTargetDir
170
     * @param string $outputFileName
171
     * @param string $fileExtension
172
     * @param string $storeItemClazz
173
     * @param string $methodName
174
     *
175
     * @return IStoreItem
176
     *
177
     * @SuppressWarnings("PHPMD.ExitExpression")
178
     */
179
    protected function invokeStoreItem(
×
180
        string $customTargetDir,
181
        string $outputFileName,
182
        string $fileExtension = IStoreItem::EXT_TEXT,
×
183
        string $storeItemClazz = FileStoreItem::class,
×
184
        string $methodName = 'prepareTargetFile'
185
    ): IStoreItem {
186
        $this->logger->debug("START");
×
187

188
        if (empty($customTargetDir)) {
×
189
            $customTargetDir = $this->sessionTargetDir;
×
190
        }
191
        $params = [$customTargetDir, $outputFileName, $fileExtension];
×
192

193
        try {
194
            /** @var IStoreItem */
195
            $newClazz = $storeItemClazz::$methodName(...$params); // @phpstan-ignore staticMethod.dynamicName
×
196
        } catch (\Exception $e) {
×
197
            $this->logger->error($e->getMessage(), [$e->getTrace()]);
×
198

NEW
199
            exit(30); // NOSONAR: php:S1799
×
200
        }
201

202
        $this->logger->debug('END');
×
203

204
        return $newClazz;
×
205
    }
206

207
    /**
208
     * REFACTOR: no usage so far.
209
     *
210
     * @param string $fileName
211
     *
212
     * @return mixed[]
213
     */
214
    public function readResultFile(string $fileName)
×
215
    {
216
        $this->logger->debug('START', [$fileName]);
×
217

218
        $resultList = [];
×
219
        if (file_exists($fileName)) {
×
220
            $fHandle = fopen($fileName, self::C_FILE_READ);
×
221

222
            if (!empty($fHandle)) {
×
223
                while ($line = fgets($fHandle, self::C_CHAR_LENGTH)) {
×
224
                    $convertedLine = mb_convert_encoding($line, self::C_CODE_UTF8);
×
225
                    if (is_string($convertedLine)) {
×
226
                        $resultList[] = explode(self::C_ITEM_SEP, $convertedLine);
×
227
                    }
228
                }
229
                fclose($fHandle);
×
230
            }
231
        } else {
232
            $this->logger->debug('+++ file does not exists! +++', [$fileName]);
×
233
        }
234

235
        $this->logger->debug('END', [$fileName]);
×
236

237
        return $resultList;
×
238
    }
239
}
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