1 | <?php |
||
2 | declare(strict_types=1); |
||
3 | /** |
||
4 | * Controller.php |
||
5 | * Copyright (c) 2018 [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\Api\V1\Controllers; |
||
24 | |||
25 | use Carbon\Carbon; |
||
26 | use Carbon\Exceptions\InvalidDateException; |
||
27 | use FireflyConfig; |
||
28 | use FireflyIII\Exceptions\FireflyException; |
||
29 | use Illuminate\Foundation\Auth\Access\AuthorizesRequests; |
||
30 | use Illuminate\Foundation\Bus\DispatchesJobs; |
||
31 | use Illuminate\Foundation\Validation\ValidatesRequests; |
||
32 | use Illuminate\Routing\Controller as BaseController; |
||
33 | use Log; |
||
34 | use Symfony\Component\HttpFoundation\ParameterBag; |
||
35 | |||
36 | /** |
||
37 | * Class Controller. |
||
38 | * |
||
39 | * @codeCoverageIgnore |
||
40 | */ |
||
41 | class Controller extends BaseController |
||
42 | { |
||
43 | use AuthorizesRequests, DispatchesJobs, ValidatesRequests; |
||
44 | |||
45 | /** @var ParameterBag */ |
||
46 | protected $parameters; |
||
47 | |||
48 | /** |
||
49 | * Controller constructor. |
||
50 | * |
||
51 | * @throws FireflyException |
||
52 | */ |
||
53 | public function __construct() |
||
54 | { |
||
55 | // is site a demo site? |
||
56 | $isDemoSite = FireflyConfig::get('is_demo_site', config('firefly.configuration.is_demo_site'))->data; |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
57 | |||
58 | // do not expose API on demo site: |
||
59 | if (true === $isDemoSite) { |
||
60 | throw new FireflyException('The API is not available on the demo site.'); |
||
61 | } |
||
62 | |||
63 | // get global parameters |
||
64 | $this->parameters = $this->getParameters(); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @return string |
||
69 | */ |
||
70 | protected function buildParams(): string |
||
71 | { |
||
72 | $return = '?'; |
||
73 | $params = []; |
||
74 | foreach ($this->parameters as $key => $value) { |
||
75 | if ($key === 'page') { |
||
76 | continue; |
||
77 | } |
||
78 | if ($value instanceof Carbon) { |
||
79 | $params[$key] = $value->format('Y-m-d'); |
||
80 | } |
||
81 | if (!$value instanceof Carbon) { |
||
82 | $params[$key] = $value; |
||
83 | } |
||
84 | } |
||
85 | $return .= http_build_query($params); |
||
86 | if (strlen($return) === 1) { |
||
87 | return ''; |
||
88 | } |
||
89 | |||
90 | return $return; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @return ParameterBag |
||
95 | */ |
||
96 | private function getParameters(): ParameterBag |
||
97 | { |
||
98 | $bag = new ParameterBag; |
||
99 | $page = (int)request()->get('page'); |
||
100 | if ($page === 0) { |
||
101 | $page = 1; |
||
102 | } |
||
103 | $bag->set('page', $page); |
||
104 | |||
105 | // some date fields: |
||
106 | $dates = ['start', 'end', 'date']; |
||
107 | foreach ($dates as $field) { |
||
108 | $date = request()->get($field); |
||
109 | $obj = null; |
||
110 | if (null !== $date) { |
||
111 | try { |
||
112 | $obj = new Carbon($date); |
||
113 | } catch (InvalidDateException $e) { |
||
114 | // don't care |
||
115 | Log::error(sprintf('Invalid date exception in API controller: %s', $e->getMessage())); |
||
116 | } |
||
117 | } |
||
118 | $bag->set($field, $obj); |
||
119 | } |
||
120 | |||
121 | return $bag; |
||
122 | |||
123 | } |
||
124 | } |
||
125 |