Test Failed
Push — develop ( 5be0e5...3f5c00 )
by Paul
14:23
created

ImportReviewsCleanup   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 34
dl 0
loc 57
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A response() 0 4 1
A notices() 0 25 3
A handle() 0 9 2
A __construct() 0 5 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Commands;
4
5
use GeminiLabs\SiteReviews\Database\ImportManager;
6
use GeminiLabs\SiteReviews\Helpers\Str;
7
use GeminiLabs\SiteReviews\Modules\Notice;
8
use GeminiLabs\SiteReviews\Modules\Queue;
9
use GeminiLabs\SiteReviews\Request;
10
11
class ImportReviewsCleanup extends AbstractCommand
12
{
13
    /** @var string[] */
14
    protected array $errors = [];
15
    protected int $imported = 0;
16
    protected int $skipped = 0;
17
18
    public function __construct(Request $request)
19
    {
20
        $this->errors = $request->cast('errors', 'array');
21
        $this->imported = $request->cast('imported', 'int');
22
        $this->skipped = $request->cast('skipped', 'int');
23
    }
24
25
    public function handle(): void
26
    {
27
        wp_cache_flush();
28
        if (0 < $this->imported) {
29
            glsr(ImportManager::class)->flush(); // flush the temporary table in the database
30
            glsr(ImportManager::class)->unlinkTempFile(); //.delete the temporary import file if it exists
31
            glsr(Queue::class)->async('queue/recalculate-meta');
32
        }
33
        $this->notices();
34
    }
35
36
    public function response(): array
37
    {
38
        return [
39
            'notices' => glsr(Notice::class)->get(),
40
        ];
41
    }
42
43
    protected function notices(): void
44
    {
45
        $notice = sprintf(
46
            _nx('%s review was imported.', '%s reviews were imported.', $this->imported, 'admin-text', 'site-reviews'),
47
            number_format_i18n($this->imported)
48
        );
49
        if (0 === $this->skipped) {
50
            glsr(Notice::class)->addSuccess($notice);
51
            return;
52
        }
53
        $skipped = sprintf(
54
            _nx('%s entry was skipped.', '%s entries were skipped.', $this->skipped, 'admin-text', 'site-reviews'),
55
            number_format_i18n($this->skipped)
56
        );
57
        $notice = sprintf('<strong>%s</strong> %s', $notice, $skipped);
58
        $details = [];
59
        if (!empty($this->errors)) {
60
            natsort($this->errors);
61
            $errorDetail = _x('One or more warnings were triggered during import: %s', 'admin-text', 'site-reviews');
62
            $errors = array_map(fn ($error) => "<mark>{$error}</mark>", $this->errors);
63
            $errors = sprintf($errorDetail, Str::naturalJoin($errors));
64
            glsr_log()->warning($errors);
65
            $details[] = $errors;
66
        }
67
        glsr(Notice::class)->addWarning($notice, $details);
68
    }
69
}
70