Issues (37)

app/Services/Sync/JobStatus/JobStatusManager.php (1 issue)

Severity
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\Services\Sync\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 $identifier
38
     * @param int    $index
39
     * @param string $error
40
     */
41
    public static function addError(string $identifier, int $index, string $error): void
42
    {
43
        $disk = Storage::disk('jobs');
44
        try {
45
            if ($disk->exists($identifier)) {
46
                $status                   = JobStatus::fromArray(json_decode($disk->get($identifier), true, 512, JSON_THROW_ON_ERROR));
47
                $status->errors[$index]   = $status->errors[$index] ?? [];
48
                $status->errors[$index][] = $error;
49
                self::storeJobStatus($identifier, $status);
50
            }
51
        } catch (FileNotFoundException $e) {
52
            app('log')->error($e);
53
        }
54
    }
55
56
    /**
57
     * @param string $identifier
58
     * @param int    $index
59
     * @param string $message
60
     */
61
    public static function addMessage(string $identifier, int $index, string $message): void
62
    {
63
        $disk = Storage::disk('jobs');
64
        try {
65
            if ($disk->exists($identifier)) {
66
                $status                     = JobStatus::fromArray(json_decode($disk->get($identifier), true, 512, JSON_THROW_ON_ERROR));
67
                $status->messages[$index]   = $status->messages[$index] ?? [];
68
                $status->messages[$index][] = $message;
69
                self::storeJobStatus($identifier, $status);
70
            }
71
        } catch (FileNotFoundException $e) {
72
            app('log')->error($e);
73
        }
74
    }
75
76
    /**
77
     * @param string $identifier
78
     * @param int    $index
79
     * @param string $warning
80
     */
81
    public static function addWarning(string $identifier, int $index, string $warning): void
82
    {
83
        $disk = Storage::disk('jobs');
84
        try {
85
            if ($disk->exists($identifier)) {
86
                $status                     = JobStatus::fromArray(json_decode($disk->get($identifier), true, 512, JSON_THROW_ON_ERROR));
87
                $status->warnings[$index]   = $status->warnings[$index] ?? [];
88
                $status->warnings[$index][] = $warning;
89
                self::storeJobStatus($identifier, $status);
90
            }
91
        } catch (FileNotFoundException $e) {
92
            app('log')->error($e);
93
        }
94
    }
95
96
    /**
97
     * @param string $status
98
     *
99
     * @return JobStatus
100
     */
101
    public static function setJobStatus(string $status): JobStatus
102
    {
103
        $syncIdentifier = session()->get(Constants::SYNC_JOB_IDENTIFIER);
104
        app('log')->debug(sprintf('Now in Sync setJobStatus(%s)', $status));
105
        app('log')->debug(sprintf('Found "%s" in the session', $syncIdentifier));
106
107
        $jobStatus         = self::startOrFindJob($syncIdentifier);
108
        $jobStatus->status = $status;
109
110
        self::storeJobStatus($syncIdentifier, $jobStatus);
111
112
        return $jobStatus;
113
    }
114
115
    /**
116
     * @param string $identifier
117
     *
118
     * @return JobStatus
119
     */
120
    public static function startOrFindJob(string $identifier): JobStatus
121
    {
122
        //app('log')->debug(sprintf('Now in (sync) startOrFindJob(%s)', $identifier));
123
        $disk = Storage::disk('jobs');
124
        try {
125
            //app('log')->debug(sprintf('Try to see if file exists for sync job %s.', $identifier));
126
            if ($disk->exists($identifier)) {
127
                //app('log')->debug(sprintf('Status file exists for sync job %s.', $identifier));
128
                $array  = json_decode($disk->get($identifier), true, 512, JSON_THROW_ON_ERROR);
129
                $status = JobStatus::fromArray($array);
130
                unset($array['messages']);
131
132
                //app('log')->debug(sprintf('Status found for sync job %s', $identifier), $array);
133
134
                return $status;
135
            }
136
        } catch (FileNotFoundException $e) {
137
            app('log')->error('Could not find sync file, write a new one.');
138
            app('log')->error($e->getMessage());
139
        }
140
        app('log')->debug('Sync file does not exist or error, create a new one.');
141
        $status = new JobStatus;
142
        $disk->put($identifier, json_encode($status->toArray(), JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT));
143
144
        //app('log')->debug('Return sync status.', $status->toArray());
145
146
        return $status;
147
    }
148
149
    /**
150
     * @param string    $syncIdentifier
151
     * @param JobStatus $status
152
     */
153
    private static function storeJobStatus(string $syncIdentifier, JobStatus $status): void
154
    {
155
        app('log')->debug(sprintf('Now in Sync storeJobStatus(%s): %s', $syncIdentifier, $status->status));
156
        $array = $status->toArray();
0 ignored issues
show
The assignment to $array is dead and can be removed.
Loading history...
157
        $disk  = Storage::disk('jobs');
158
        $disk->put($syncIdentifier, json_encode($status->toArray(), JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT));
159
        app('log')->debug('Done with storing.');
160
    }
161
}
162