Conditions | 8 |
Paths | 31 |
Total Lines | 78 |
Code Lines | 50 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php namespace App\Http\Controllers; |
||
26 | public function dashboard($requestedSnapshot = null) |
||
27 | { |
||
28 | if (isset($requestedSnapshot)) // Search snapshot in history |
||
29 | { |
||
30 | $snapshot = $this->snapshotRepository->get($requestedSnapshot); |
||
31 | } else // Grab the current snapshot, or redirect if no one exists |
||
32 | { |
||
33 | try { |
||
34 | $snapshot = $this->snapshotRepository->current(); |
||
35 | } catch (RepositoryException $e) { |
||
36 | if ($e->getCode() == RepositoryException::RESOURCE_NOT_FOUND) { |
||
37 | return view('cash.init'); |
||
38 | } |
||
39 | |||
40 | App::abort(500); |
||
41 | } |
||
42 | } |
||
43 | |||
44 | $snapshotDetails = $this->detailsRepository->fromSnapshot($snapshot->cs_id); |
||
45 | $allSnapshots = $this->snapshotRepository->all(); |
||
46 | |||
47 | $cashArray = [floatval($snapshot->amount)]; |
||
48 | $lastAmount = $snapshot->amount; |
||
49 | |||
50 | $lastOperation = 0; |
||
51 | $cashBySales = 0; |
||
52 | $salesCount = 0; |
||
53 | $operationsCount = 0; |
||
54 | |||
55 | foreach ($snapshotDetails as $detail) { |
||
56 | |||
57 | $lastAmount += $detail->sum; |
||
58 | array_push($cashArray, $lastAmount); |
||
59 | |||
60 | if ($detail->type == 'CASH') { |
||
61 | $lastOperation = $detail->sum; |
||
62 | $operationsCount++; |
||
63 | } |
||
64 | |||
65 | if ($detail->type == 'SALE') { |
||
66 | $cashBySales += $detail->sum; |
||
67 | $salesCount++; |
||
68 | } |
||
69 | } |
||
70 | |||
71 | // Enough data for current snapshot, |
||
72 | // return all data to the view |
||
73 | if (!$snapshot->is_closed) { |
||
74 | return view('cash.app')->with('snapshot', $snapshot) |
||
75 | ->with('details', $snapshotDetails) |
||
76 | ->with('amounts', $cashArray) |
||
77 | ->with('lastOperation', $lastOperation) |
||
78 | ->with('cashBySales', $cashBySales) |
||
79 | ->with('allSnapshots', $allSnapshots) |
||
80 | ->with('salesCount', $salesCount) |
||
81 | ->with('operationsCount', $operationsCount); |
||
82 | } |
||
83 | |||
84 | // Other statistics for closed snapshot |
||
85 | |||
86 | $nextSnapshot = $this->snapshotRepository->getNext($snapshot->cs_id); |
||
87 | |||
88 | $snapshotTime = new Carbon($snapshot->time); |
||
89 | $nextSnapshotTime = new Carbon($nextSnapshot->time); |
||
90 | $duration = $this->approximateDuration($snapshotTime, $nextSnapshotTime); |
||
91 | |||
92 | $delta = $nextSnapshot->amount - $snapshot->predicted_amount; |
||
93 | |||
94 | return view('cash.app')->with('snapshot', $snapshot) |
||
95 | ->with('details', $snapshotDetails) |
||
96 | ->with('amounts', $cashArray) |
||
97 | ->with('cashBySales', $cashBySales) |
||
98 | ->with('allSnapshots', $allSnapshots) |
||
99 | ->with('salesCount', $salesCount) |
||
100 | ->with('operationsCount', $operationsCount) |
||
101 | ->with('delta', $delta) |
||
102 | ->with('duration', $duration); |
||
103 | } |
||
104 | |||
225 |