1 | <?php |
||||||
2 | declare(strict_types=1); |
||||||
3 | /** |
||||||
4 | * CreateExport.php |
||||||
5 | * Copyright (c) 2017 [email protected] |
||||||
6 | * |
||||||
7 | * This file is part of Firefly III. |
||||||
8 | * |
||||||
9 | * Firefly III is free software: you can redistribute it and/or modify |
||||||
10 | * it under the terms of the GNU General Public License as published by |
||||||
11 | * the Free Software Foundation, either version 3 of the License, or |
||||||
12 | * (at your option) any later version. |
||||||
13 | * |
||||||
14 | * Firefly III is distributed in the hope that it will be useful, |
||||||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
17 | * GNU General Public License for more details. |
||||||
18 | * |
||||||
19 | * You should have received a copy of the GNU General Public License |
||||||
20 | * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
||||||
21 | */ |
||||||
22 | |||||||
23 | namespace FireflyIII\Console\Commands; |
||||||
24 | |||||||
25 | use Carbon\Carbon; |
||||||
26 | use FireflyIII\Export\ProcessorInterface; |
||||||
27 | use FireflyIII\Models\AccountType; |
||||||
28 | use FireflyIII\Repositories\Account\AccountRepositoryInterface; |
||||||
29 | use FireflyIII\Repositories\ExportJob\ExportJobRepositoryInterface; |
||||||
30 | use FireflyIII\Repositories\Journal\JournalRepositoryInterface; |
||||||
31 | use FireflyIII\Repositories\User\UserRepositoryInterface; |
||||||
32 | use Illuminate\Console\Command; |
||||||
33 | use Storage; |
||||||
34 | |||||||
35 | /** |
||||||
36 | * Class CreateExport. |
||||||
37 | * |
||||||
38 | * Generates export from the command line. |
||||||
39 | */ |
||||||
40 | class CreateExport extends Command |
||||||
41 | { |
||||||
42 | use VerifiesAccessToken; |
||||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
43 | /** |
||||||
44 | * The console command description. |
||||||
45 | * |
||||||
46 | * @var string |
||||||
47 | */ |
||||||
48 | protected $description = 'Use this command to create a new import. Your user ID can be found on the /profile page.'; |
||||||
49 | |||||||
50 | /** |
||||||
51 | * The name and signature of the console command. |
||||||
52 | * |
||||||
53 | * @var string |
||||||
54 | */ |
||||||
55 | protected $signature |
||||||
56 | = 'firefly:create-export |
||||||
57 | {--user= : The user ID that the import should import for.} |
||||||
58 | {--token= : The user\'s access token.} |
||||||
59 | {--with_attachments : Include user\'s attachments?} |
||||||
60 | {--with_uploads : Include user\'s uploads?}'; |
||||||
61 | |||||||
62 | /** |
||||||
63 | * Execute the console command. |
||||||
64 | * |
||||||
65 | * @return mixed |
||||||
66 | */ |
||||||
67 | public function handle() |
||||||
68 | { |
||||||
69 | if (!$this->verifyAccessToken()) { |
||||||
70 | $this->error('Invalid access token.'); |
||||||
71 | |||||||
72 | return 1; |
||||||
73 | } |
||||||
74 | $this->line('Full export is running...'); |
||||||
75 | // make repositories |
||||||
76 | /** @var UserRepositoryInterface $userRepository */ |
||||||
77 | $userRepository = app(UserRepositoryInterface::class); |
||||||
78 | /** @var ExportJobRepositoryInterface $jobRepository */ |
||||||
79 | $jobRepository = app(ExportJobRepositoryInterface::class); |
||||||
80 | /** @var AccountRepositoryInterface $accountRepository */ |
||||||
81 | $accountRepository = app(AccountRepositoryInterface::class); |
||||||
82 | /** @var JournalRepositoryInterface $journalRepository */ |
||||||
83 | $journalRepository = app(JournalRepositoryInterface::class); |
||||||
84 | |||||||
85 | // set user |
||||||
86 | $user = $userRepository->findNull((int)$this->option('user')); |
||||||
87 | $jobRepository->setUser($user); |
||||||
88 | $journalRepository->setUser($user); |
||||||
89 | $accountRepository->setUser($user); |
||||||
90 | |||||||
91 | // first date |
||||||
92 | $firstJournal = $journalRepository->first(); |
||||||
0 ignored issues
–
show
The function
FireflyIII\Repositories\...itoryInterface::first() has been deprecated.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
93 | $first = new Carbon; |
||||||
94 | if (null !== $firstJournal->id) { |
||||||
95 | $first = $firstJournal->date; |
||||||
96 | } |
||||||
97 | |||||||
98 | // create job and settings. |
||||||
99 | $job = $jobRepository->create(); |
||||||
100 | $settings = [ |
||||||
101 | 'accounts' => $accountRepository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT]), |
||||||
102 | 'startDate' => $first, |
||||||
103 | 'endDate' => new Carbon, |
||||||
104 | 'exportFormat' => 'csv', |
||||||
105 | 'includeAttachments' => $this->option('with_attachments'), |
||||||
106 | 'includeOldUploads' => $this->option('with_uploads'), |
||||||
107 | 'job' => $job, |
||||||
108 | ]; |
||||||
109 | |||||||
110 | /** @var ProcessorInterface $processor */ |
||||||
111 | $processor = app(ProcessorInterface::class); |
||||||
112 | $processor->setSettings($settings); |
||||||
113 | |||||||
114 | $processor->collectJournals(); |
||||||
115 | $processor->convertJournals(); |
||||||
116 | $processor->exportJournals(); |
||||||
117 | if ($settings['includeAttachments']) { |
||||||
118 | $processor->collectAttachments(); |
||||||
119 | } |
||||||
120 | |||||||
121 | if ($settings['includeOldUploads']) { |
||||||
122 | $processor->collectOldUploads(); |
||||||
123 | } |
||||||
124 | |||||||
125 | $processor->createZipFile(); |
||||||
126 | $disk = Storage::disk('export'); |
||||||
0 ignored issues
–
show
The method
Illuminate\Support\Facades\Storage::disk() is not static, but was called statically.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
127 | $fileName = sprintf('export-%s.zip', date('Y-m-d_H-i-s')); |
||||||
128 | $disk->move($job->key . '.zip', $fileName); |
||||||
129 | |||||||
130 | $this->line('The export has finished! You can find the ZIP file in this location:'); |
||||||
131 | $this->line(storage_path(sprintf('export/%s', $fileName))); |
||||||
132 | |||||||
133 | return 0; |
||||||
134 | } |
||||||
135 | } |
||||||
136 |