1 | <?php |
||
2 | /** |
||
3 | * ConfigurationController.php |
||
4 | * Copyright (c) 2020 [email protected]. |
||
5 | * |
||
6 | * This file is part of the Firefly III bunq importer |
||
7 | * (https://github.com/firefly-iii/bunq-importer). |
||
8 | * |
||
9 | * This program is free software: you can redistribute it and/or modify |
||
10 | * it under the terms of the GNU Affero General Public License as |
||
11 | * published by the Free Software Foundation, either version 3 of the |
||
12 | * License, or (at your option) any later version. |
||
13 | * |
||
14 | * This program 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 Affero General Public License for more details. |
||
18 | * |
||
19 | * You should have received a copy of the GNU Affero General Public License |
||
20 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||
21 | */ |
||
22 | |||
23 | declare(strict_types=1); |
||
24 | |||
25 | namespace App\Http\Controllers\Import; |
||
26 | |||
27 | use App\Bunq\ApiContext\ApiContextManager; |
||
28 | use App\Bunq\Requests\MonetaryAccountList; |
||
29 | use App\Exceptions\ImportException; |
||
30 | use App\Http\Controllers\Controller; |
||
31 | use App\Http\Middleware\ConfigComplete; |
||
32 | use App\Http\Middleware\ConfigurationPostRequest; |
||
33 | use App\Services\Configuration\Configuration; |
||
34 | use App\Services\Session\Constants; |
||
35 | use App\Services\Storage\StorageService; |
||
36 | use GrumpyDictator\FFIIIApiSupport\Exceptions\ApiHttpException; |
||
37 | use GrumpyDictator\FFIIIApiSupport\Model\Account; |
||
38 | use GrumpyDictator\FFIIIApiSupport\Request\GetAccountsRequest; |
||
39 | use Illuminate\Contracts\Routing\ResponseFactory; |
||
40 | use Illuminate\Contracts\View\Factory; |
||
41 | use Illuminate\Http\RedirectResponse; |
||
42 | use Illuminate\Http\Response; |
||
43 | use Illuminate\View\View; |
||
44 | |||
45 | /** |
||
46 | * Class ConfigurationController. |
||
47 | */ |
||
48 | class ConfigurationController extends Controller |
||
49 | { |
||
50 | /** |
||
51 | * StartController constructor. |
||
52 | */ |
||
53 | public function __construct() |
||
54 | { |
||
55 | parent::__construct(); |
||
56 | app('view')->share('pageTitle', 'Import configuration'); |
||
57 | $this->middleware(ConfigComplete::class)->except('download'); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @return ResponseFactory|Response |
||
62 | */ |
||
63 | public function download() |
||
64 | { |
||
65 | // do something |
||
66 | $config = Configuration::fromArray(session()->get(Constants::CONFIGURATION))->toArray(); |
||
67 | $result = json_encode($config, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT, 512); |
||
68 | |||
69 | $response = response($result); |
||
70 | $name = sprintf('bunq_import_config_%s.json', date('Y-m-d')); |
||
71 | $response->header('Content-disposition', 'attachment; filename=' . $name) |
||
72 | ->header('Content-Type', 'application/json') |
||
73 | ->header('Content-Description', 'File Transfer') |
||
74 | ->header('Connection', 'Keep-Alive') |
||
75 | ->header('Expires', '0') |
||
76 | ->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0') |
||
77 | ->header('Pragma', 'public') |
||
78 | ->header('Content-Length', strlen($result)); |
||
79 | |||
80 | return $response; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @throws ApiHttpException |
||
85 | * @throws ImportException |
||
86 | * @return Factory|RedirectResponse|View |
||
87 | */ |
||
88 | public function index() |
||
89 | { |
||
90 | app('log')->debug(sprintf('Now at %s', __METHOD__)); |
||
91 | $mainTitle = 'Import from bunq'; |
||
92 | $subTitle = 'Configure your bunq import'; |
||
93 | |||
94 | $configuration = Configuration::fromArray([]); |
||
95 | if (session()->has(Constants::CONFIGURATION)) { |
||
96 | $configuration = Configuration::fromArray(session()->get(Constants::CONFIGURATION)); |
||
97 | } |
||
98 | // if config says to skip it, skip it: |
||
99 | if (null !== $configuration && true === $configuration->isSkipForm()) { |
||
100 | // skipForm |
||
101 | return redirect()->route('import.download.index'); |
||
102 | } |
||
103 | // get list of asset accounts in Firefly III |
||
104 | $uri = (string) config('bunq.uri'); |
||
105 | $token = (string) config('bunq.access_token'); |
||
106 | $request = new GetAccountsRequest($uri, $token); |
||
107 | $request->setType(GetAccountsRequest::ASSET); |
||
108 | $ff3Accounts = $request->get(); |
||
109 | |||
110 | // get the user's bunq accounts. |
||
111 | ApiContextManager::getApiContext(); |
||
112 | |||
113 | /** @var MonetaryAccountList $lister */ |
||
114 | $lister = app(MonetaryAccountList::class); |
||
115 | $bunqAccounts = $lister->listing(); |
||
116 | $combinedAccounts = []; |
||
117 | foreach ($bunqAccounts as $bunqAccount) { |
||
118 | $bunqAccount['ff3_id'] = null; |
||
119 | $bunqAccount['ff3_name'] = null; |
||
120 | $bunqAccount['ff3_type'] = null; |
||
121 | $bunqAccount['ff3_iban'] = null; |
||
122 | $bunqAccount['ff3_currency'] = null; |
||
123 | /** @var Account $ff3Account */ |
||
124 | foreach ($ff3Accounts as $ff3Account) { |
||
125 | if ($bunqAccount['currency'] === $ff3Account->currencyCode && $bunqAccount['iban'] === $ff3Account->iban |
||
126 | && 'CANCELLED' !== $bunqAccount['status'] |
||
127 | ) { |
||
128 | $bunqAccount['ff3_id'] = $ff3Account->id; |
||
129 | $bunqAccount['ff3_name'] = $ff3Account->name; |
||
130 | $bunqAccount['ff3_type'] = $ff3Account->type; |
||
131 | $bunqAccount['ff3_iban'] = $ff3Account->iban; |
||
132 | $bunqAccount['ff3_currency'] = $ff3Account->currencyCode; |
||
133 | $bunqAccount['ff3_uri'] = sprintf('%saccounts/show/%d', $uri, $ff3Account->id); |
||
134 | } |
||
135 | } |
||
136 | $combinedAccounts[] = $bunqAccount; |
||
137 | } |
||
138 | |||
139 | $mapping = '{}'; |
||
140 | if (null !== $configuration) { |
||
141 | $mapping = base64_encode(json_encode($configuration->getMapping(), JSON_THROW_ON_ERROR, 512)); |
||
142 | } |
||
143 | |||
144 | return view( |
||
145 | 'import.configuration.index', compact('mainTitle', 'subTitle', 'ff3Accounts', 'combinedAccounts', 'configuration', 'bunqAccounts', 'mapping') |
||
146 | ); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * @param ConfigurationPostRequest $request |
||
151 | * |
||
152 | * @return RedirectResponse |
||
153 | */ |
||
154 | public function postIndex(ConfigurationPostRequest $request) |
||
155 | { |
||
156 | app('log')->debug(sprintf('Now at %s', __METHOD__)); |
||
157 | // store config on drive. |
||
158 | |||
159 | $fromRequest = $request->getAll(); |
||
160 | $configuration = Configuration::fromRequest($fromRequest); |
||
161 | $config = StorageService::storeContent(json_encode($configuration, JSON_THROW_ON_ERROR, 512)); |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
162 | |||
163 | session()->put(Constants::CONFIGURATION, $configuration->toArray()); |
||
164 | |||
165 | // set config as complete. |
||
166 | session()->put(Constants::CONFIG_COMPLETE_INDICATOR, true); |
||
167 | |||
168 | // redirect to import things? |
||
169 | return redirect()->route('import.download.index'); |
||
170 | } |
||
171 | } |
||
172 |