JobStatusManager::addError()   A
last analyzed

Complexity

Conditions 3
Paths 7

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 10
c 2
b 0
f 0
dl 0
loc 13
rs 9.9332
cc 3
nc 7
nop 3
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * JobStatusManager.php
6
 * Copyright (c) 2020 [email protected].
7
 *
8
 * This file is part of the Firefly III bunq importer
9
 * (https://github.com/firefly-iii/bunq-importer).
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
23
 */
24
25
namespace App\Bunq\Download\JobStatus;
26
27
use App\Services\Session\Constants;
28
use Illuminate\Contracts\Filesystem\FileNotFoundException;
29
use Illuminate\Support\Facades\Storage;
30
31
/**
32
 * Class JobStatusManager.
33
 */
34
class JobStatusManager
35
{
36
    /**
37
     * @param string $downloadIdentifier
38
     * @param int    $index
39
     * @param string $error
40
     */
41
    public static function addError(string $downloadIdentifier, int $index, string $error): void
42
    {
43
        $disk = Storage::disk('jobs');
44
        try {
45
            if ($disk->exists($downloadIdentifier)) {
46
                $status                   = JobStatus::fromArray(json_decode($disk->get($downloadIdentifier), true, 512, JSON_THROW_ON_ERROR));
47
                $status->errors[$index]   = $status->errors[$index] ?? [];
48
                $status->errors[$index][] = $error;
49
                self::storeJobStatus($downloadIdentifier, $status);
50
            }
51
        } catch (FileNotFoundException $e) {
52
            app('log')->error($e->getMessage());
53
            app('log')->error($e->getTraceAsString());
54
        }
55
    }
56
57
    /**
58
     * @param string $downloadIdentifier
59
     * @param int    $index
60
     * @param string $message
61
     */
62
    public static function addMessage(string $downloadIdentifier, int $index, string $message): void
63
    {
64
        $disk = Storage::disk('jobs');
65
        try {
66
            if ($disk->exists($downloadIdentifier)) {
67
                $status                     = JobStatus::fromArray(json_decode($disk->get($downloadIdentifier), true, 512, JSON_THROW_ON_ERROR));
68
                $status->messages[$index]   = $status->messages[$index] ?? [];
69
                $status->messages[$index][] = $message;
70
                self::storeJobStatus($downloadIdentifier, $status);
71
            }
72
        } catch (FileNotFoundException $e) {
73
            app('log')->error($e->getMessage());
74
            app('log')->error($e->getTraceAsString());
75
        }
76
    }
77
78
    /**
79
     * @param string $downloadIdentifier
80
     * @param int    $index
81
     * @param string $warning
82
     */
83
    public static function addWarning(string $downloadIdentifier, int $index, string $warning): void
84
    {
85
        $disk = Storage::disk('jobs');
86
        try {
87
            if ($disk->exists($downloadIdentifier)) {
88
                $status                     = JobStatus::fromArray(json_decode($disk->get($downloadIdentifier), true, 512, JSON_THROW_ON_ERROR));
89
                $status->warnings[$index]   = $status->warnings[$index] ?? [];
90
                $status->warnings[$index][] = $warning;
91
                self::storeJobStatus($downloadIdentifier, $status);
92
            }
93
        } catch (FileNotFoundException $e) {
94
            app('log')->error($e->getMessage());
95
            app('log')->error($e->getTraceAsString());
96
        }
97
    }
98
99
    /**
100
     * @param string $status
101
     *
102
     * @return JobStatus
103
     */
104
    public static function setJobStatus(string $status): JobStatus
105
    {
106
        $downloadIdentifier = session()->get(Constants::DOWNLOAD_JOB_IDENTIFIER);
107
        app('log')->debug(sprintf('Now in download setJobStatus(%s) for job %s', $status, $downloadIdentifier));
108
109
        $jobStatus         = self::startOrFindJob($downloadIdentifier);
110
        $jobStatus->status = $status;
111
112
        self::storeJobStatus($downloadIdentifier, $jobStatus);
113
114
        return $jobStatus;
115
    }
116
117
    /**
118
     * @param string $downloadIdentifier
119
     *
120
     * @return JobStatus
121
     */
122
    public static function startOrFindJob(string $downloadIdentifier): JobStatus
123
    {
124
        //app('log')->debug(sprintf('Now in (download) startOrFindJob(%s)', $downloadIdentifier));
125
        $disk = Storage::disk('jobs');
126
        try {
127
            //app('log')->debug(sprintf('Try to see if file exists for download job %s.', $downloadIdentifier));
128
            if ($disk->exists($downloadIdentifier)) {
129
                //app('log')->debug(sprintf('Status file exists for download job %s.', $downloadIdentifier));
130
                $array  = json_decode($disk->get($downloadIdentifier), true, 512, JSON_THROW_ON_ERROR);
131
                $status = JobStatus::fromArray($array);
132
133
                //app('log')->debug(sprintf('Status found for download job %s', $downloadIdentifier), $array);
134
135
                return $status;
136
            }
137
        } catch (FileNotFoundException $e) {
138
            app('log')->error('Could not find download job file, write a new one.');
139
            app('log')->error($e->getMessage());
140
        }
141
        app('log')->debug('Download job file does not exist or error, create a new one.');
142
        $status = new JobStatus;
143
        $disk->put($downloadIdentifier, json_encode($status->toArray(), JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT));
144
145
        app('log')->debug('Return download job status.', $status->toArray());
146
147
        return $status;
148
    }
149
150
    /**
151
     * @param string    $downloadIdentifier
152
     * @param JobStatus $status
153
     */
154
    private static function storeJobStatus(string $downloadIdentifier, JobStatus $status): void
155
    {
156
        app('log')->debug(sprintf('Now in storeJobStatus(%s): %s', $downloadIdentifier, $status->status));
157
        $disk = Storage::disk('jobs');
158
        $disk->put($downloadIdentifier, json_encode($status->toArray(), JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT));
159
    }
160
}
161