ProcessCsvFile::reader()   A
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 24
ccs 0
cts 19
cp 0
rs 9.0444
c 0
b 0
f 0
cc 6
nc 9
nop 1
crap 42
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Commands;
4
5
use GeminiLabs\League\Csv\CannotInsertRecord;
6
use GeminiLabs\League\Csv\CharsetConverter;
7
use GeminiLabs\League\Csv\EscapeFormula;
8
use GeminiLabs\League\Csv\Exception;
9
use GeminiLabs\League\Csv\Info;
10
use GeminiLabs\League\Csv\Reader;
11
use GeminiLabs\League\Csv\Statement;
12
use GeminiLabs\League\Csv\Writer;
13
use GeminiLabs\SiteReviews\Database\ImportManager;
14
use GeminiLabs\SiteReviews\Exceptions\FileNotFoundException;
15
use GeminiLabs\SiteReviews\Helpers\Arr;
16
use GeminiLabs\SiteReviews\Helpers\Str;
17
use GeminiLabs\SiteReviews\Modules\Date;
18
use GeminiLabs\SiteReviews\Modules\Dump;
19
use GeminiLabs\SiteReviews\Modules\Notice;
20
use GeminiLabs\SiteReviews\Modules\Rating;
21
use GeminiLabs\SiteReviews\Request;
22
use GeminiLabs\SiteReviews\Upload;
23
use GeminiLabs\SiteReviews\UploadedFile;
24
25
class ProcessCsvFile extends AbstractCommand
26
{
27
    use Upload;
28
29
    public const ALLOWED_DATE_FORMATS = [
30
        'd-m-Y', 'd-m-Y H:i', 'd-m-Y H:i:s',
31
        'd/m/Y', 'd/m/Y H:i', 'd/m/Y H:i:s',
32
        'm-d-Y', 'm-d-Y H:i', 'm-d-Y H:i:s',
33
        'm/d/Y', 'm/d/Y H:i', 'm/d/Y H:i:s',
34
        'Y-m-d', 'Y-m-d H:i', 'Y-m-d H:i:s',
35
        'Y/m/d', 'Y/m/d H:i', 'Y/m/d H:i:s',
36
    ];
37
38
    public const ALLOWED_DELIMITERS = [
39
        ',', ';',
40
    ];
41
42
    public const REQUIRED_KEYS = [
43
        'date', 'rating',
44
    ];
45
46
    protected string $dateFormat;
47
48
    protected string $delimiter;
49
50
    protected array $errors;
51
52
    protected int $skipped;
53
54
    protected int $total;
55
56
    public function __construct(Request $request)
57
    {
58
        $this->dateFormat = Str::restrictTo(static::ALLOWED_DATE_FORMATS, $request->date_format, 'Y-m-d', true);
59
        $this->delimiter = Str::restrictTo(static::ALLOWED_DELIMITERS, $request->delimiter, '');
60
        $this->errors = [];
61
        $this->skipped = 0;
62
        $this->total = 0;
63
    }
64
65
    public function handle(): void
66
    {
67
        try {
68
            $file = $this->file();
69
        } catch (FileNotFoundException $e) {
70
            glsr(Notice::class)->addError($e->getMessage());
71
            $this->fail();
72
            return;
73
        }
74
        if (!$this->validateFile($file)) {
75
            $this->fail();
76
            return;
77
        }
78
        if (!$this->process($file)) {
79
            $this->fail();
80
        }
81
    }
82
83
    public function response(): array
84
    {
85
        return [
86
            'errors' => $this->errors, // @todo store this to the session
87
            'notices' => glsr(Notice::class)->get(), // this should be empty on success
88
            'skipped' => $this->skipped,
89
            'total' => $this->total,
90
        ];
91
    }
92
93
    protected function formatRecord(array $record): array
94
    {
95
        if (!empty($record['date'])) {
96
            $date = \DateTime::createFromFormat($this->dateFormat, $record['date']);
97
            $record['date'] = $date->format('Y-m-d H:i:s'); // format the provided date
98
        }
99
        return $record;
100
    }
101
102
    protected function process(UploadedFile $file): bool
103
    {
104
        if (!defined('WP_IMPORTING')) {
105
            define('WP_IMPORTING', true);
106
        }
107
        glsr(ImportManager::class)->flush(); // flush the temporary table in the database
108
        glsr(ImportManager::class)->unlinkTempFile(); // delete the temporary import file if it exists
109
        try {
110
            wp_raise_memory_limit('admin');
111
            $reader = $this->reader($file->getPathname());
112
            $header = array_map('trim', $reader->getHeader());
113
            if (!empty(array_diff(static::REQUIRED_KEYS, $header))) {
114
                throw new Exception(_x('The CSV file could not be imported. Please verify the following details and try again:', 'admin-text', 'site-reviews'));
115
            }
116
            $filePath = glsr(ImportManager::class)->tempFilePath();
117
            $writer = Writer::createFromPath($filePath, 'w+');
0 ignored issues
show
Deprecated Code introduced by
The function GeminiLabs\League\Csv\Ab...ctCsv::createFromPath() has been deprecated: since version 9.27.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

117
            $writer = /** @scrutinizer ignore-deprecated */ Writer::createFromPath($filePath, 'w+');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
118
            $writer->addFormatter(new EscapeFormula());
119
            $writer->insertOne($header);
120
            $writer->addFormatter(fn (array $record) => $this->formatRecord($record));
121
            $chunks = $reader->chunkBy(1000);
122
            foreach ($chunks as $chunk) {
123
                $records = Statement::create()
0 ignored issues
show
Deprecated Code introduced by
The function GeminiLabs\League\Csv\Statement::create() has been deprecated: Since version 9.22.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

123
                $records = /** @scrutinizer ignore-deprecated */ Statement::create()

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
124
                    ->where(fn (array $record) => !empty(array_filter($record, 'trim'))) // @phpstan-ignore-line remove empty rows
125
                    ->where(fn (array $record) => $this->validateRecord($record))
126
                    ->process($reader, $header);
127
                $writer->insertAll($records);
128
                $this->total += count($records);
129
            }
130
            glsr(ImportManager::class)->prepare(); // create a temporary table for importing
131
            return true;
132
        } catch (CannotInsertRecord $e) {
133
            glsr(Notice::class)->addError(_x('Unable to process a row in the CSV document:', 'admin-text', 'site-reviews'), [
134
                glsr(Dump::class)->dump($e->getRecord()),
135
            ]);
136
        } catch (Exception $e) {
137
            glsr(Notice::class)->addError($e->getMessage(), [
138
                '👉🏼 '._x('Does the CSV file include all required columns?', 'admin-text', 'site-reviews'),
139
                '👉🏼 '._x('Have you named all of the columns in the CSV file?', 'admin-text', 'site-reviews'),
140
                '👉🏼 '._x('Have you removed all empty columns from the CSV file?', 'admin-text', 'site-reviews'),
141
                '👉🏼 '._x('Have you selected the correct delimiter?', 'admin-text', 'site-reviews'),
142
                '👉🏼 '._x('Is the CSV file encoded as UTF-8?', 'admin-text', 'site-reviews'),
143
            ]);
144
        } catch (\OutOfRangeException|\Exception|\TypeError $e) {
145
            glsr(Notice::class)->addError($e->getMessage());
146
        }
147
        glsr(ImportManager::class)->unlinkTempFile();
148
        return false;
149
    }
150
151
    /**
152
     * @throws Exception
153
     */
154
    protected function reader(string $filepath): Reader
155
    {
156
        $reader = Reader::createFromPath($filepath);
0 ignored issues
show
Deprecated Code introduced by
The function GeminiLabs\League\Csv\Reader::createFromPath() has been deprecated: since version 9.27.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

156
        $reader = /** @scrutinizer ignore-deprecated */ Reader::createFromPath($filepath);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
157
        if (empty($this->delimiter)) {
158
            $delimiters = Info::getDelimiterStats($reader, static::ALLOWED_DELIMITERS);
159
            $delimiters = array_keys(array_filter($delimiters));
160
            if (1 !== count($delimiters)) {
161
                throw new Exception(_x('Cannot detect the delimiter used in the CSV file (supported delimiters are comma and semicolon).', 'admin-text', 'site-reviews'));
162
            }
163
            $this->delimiter = $delimiters[0];
164
        }
165
        $reader->setDelimiter($this->delimiter);
166
        $reader->setHeaderOffset(0);
167
        $reader->skipEmptyRecords();
168
        $reader->addFormatter(fn (array $record) => array_map('trim', $record));
169
        if ($reader->supportsStreamFilterOnRead()) {
170
            $inputBom = $reader->getInputBOM();
171
            if (in_array($inputBom, [Reader::BOM_UTF16_LE, Reader::BOM_UTF16_BE], true)) {
172
                return CharsetConverter::addTo($reader, 'utf-16', 'utf-8'); // @phpstan-ignore-line
173
            } elseif (in_array($inputBom, [Reader::BOM_UTF32_LE, Reader::BOM_UTF32_BE], true)) {
174
                return CharsetConverter::addTo($reader, 'utf-32', 'utf-8'); // @phpstan-ignore-line
175
            }
176
        }
177
        return $reader;
178
    }
179
180
    protected function validateFile(UploadedFile $file): bool
181
    {
182
        if (!$file->isValid()) {
183
            glsr(Notice::class)->addError($file->getErrorMessage());
184
            return false;
185
        }
186
        if (!$file->hasMimeType('text/csv')) {
187
            glsr(Notice::class)->addError(sprintf(
188
                _x('The import file does not look like a valid CSV file (detected: %s). If this is incorrect, make sure that your server is configured to detect mime types.', 'admin-text', 'site-reviews'),
189
                $file->getMimeType()
190
            ));
191
            return false;
192
        }
193
        return true;
194
    }
195
196
    protected function validateRecord(array $record): bool
197
    {
198
        $record = array_map('trim', $record);
199
        $required = [
200
            'date' => glsr(Date::class)->isDate(Arr::getAs('string', $record, 'date'), $this->dateFormat),
201
            'rating' => glsr(Rating::class)->isValid(Arr::getAs('int', $record, 'rating')),
202
        ];
203
        if (2 === count(array_filter($required))) {
204
            return true;
205
        }
206
        $errorMessages = [
207
            'date' => _x('Incorrect date format', 'admin-text', 'site-reviews'),
208
            'rating' => _x('Empty or invalid rating', 'admin-text', 'site-reviews'),
209
        ];
210
        $errors = array_intersect_key($errorMessages, array_diff_key($required, array_filter($required)));
211
        $this->errors = array_merge($this->errors, $errors);
212
        ++$this->skipped;
213
        return false;
214
    }
215
}
216