Conditions | 5 |
Paths | 9 |
Total Lines | 84 |
Code Lines | 46 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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 |
||
35 | public function getHelgaMetrics() |
||
36 | { |
||
37 | // Challenges on generating these stats: |
||
38 | // 1. variable amount of vendors and price categories used per event |
||
39 | // 2. Substract box office sales from regular sales |
||
40 | |||
41 | $result = []; |
||
42 | |||
43 | // Get all event-ids of the project |
||
44 | $eventIds = $this->events()->pluck('id'); |
||
45 | |||
46 | // base query that joins all required tables to one big table of tickets |
||
47 | $viewQuery = DB::table('tickets') |
||
48 | ->join('price_categories', 'tickets.price_category_id', '=', 'price_categories.id') |
||
49 | ->join('purchases', 'tickets.purchase_id', '=', 'purchases.id') |
||
50 | ->join('users', 'purchases.vendor_id', '=', 'users.id'); |
||
51 | |||
52 | // First we fetch the result-table-headers. By using "with()" we can reuse the base query for all other queries. |
||
53 | // + Group by price categories |
||
54 | // + Group by vendors |
||
55 | $priceCategories = with(clone $viewQuery)->whereIn('tickets.event_id', $eventIds) |
||
56 | ->groupBy('price_categories.id') |
||
57 | ->select('price_categories.id as id', 'price_categories.name as name') |
||
58 | ->get(); |
||
59 | |||
60 | $vendors = with(clone $viewQuery)->whereIn('tickets.event_id', $eventIds) |
||
61 | ->groupBy('purchases.vendor_id') |
||
62 | ->select('users.id as id', 'users.name as name') |
||
63 | ->get(); |
||
64 | |||
65 | // Iterate over all events and fill in the values for each fetched header |
||
66 | foreach( $this->events as $event ) { |
||
67 | $eventResults = []; |
||
68 | |||
69 | $eventResults['date'] = $event->start_date; |
||
70 | |||
71 | // Set free tickets |
||
72 | $eventResults['free_tickets'] = with(clone $viewQuery)->where('tickets.event_id', $event->id) |
||
73 | ->where('purchases.state', 'free') |
||
74 | ->count(); |
||
75 | |||
76 | // Get ticket category stats |
||
77 | foreach( $priceCategories as $cat) { |
||
78 | // Get the number of tickets sold of this category |
||
79 | $eventResults[$cat->name . '_total'] = with(clone $viewQuery)->where('tickets.event_id', $event->id) |
||
80 | ->where('purchases.state', 'paid') |
||
81 | ->where('price_categories.id', $cat->id) |
||
82 | ->count(); |
||
83 | // Get the amount of money by sold tickets of this category |
||
84 | $eventResults[$cat->name . '_sum'] = with(clone $viewQuery)->where('tickets.event_id', $event->id) |
||
85 | ->where('purchases.state', 'paid') |
||
86 | ->where('price_categories.id', $cat->id) |
||
87 | ->sum('price_categories.price'); |
||
88 | // Get the number of not consumed tickets (=no shows) |
||
89 | $eventResults[$cat->name . '_no-show'] = with(clone $viewQuery)->where('tickets.event_id', $event->id) |
||
90 | ->where('purchases.state', 'paid') |
||
91 | ->where('price_categories.id', $cat->id) |
||
92 | ->where('tickets.state', '<>', 'consumed') |
||
93 | ->count(); |
||
94 | } |
||
95 | |||
96 | $eventResults['total_visits'] = with(clone $viewQuery)->where('tickets.event_id', $event->id)->count(); |
||
97 | $eventResults['total_sum'] = with(clone $viewQuery)->where('tickets.event_id', $event->id)->sum('price_categories.price'); |
||
98 | |||
99 | // Get the sales for each vendor |
||
100 | foreach( $vendors as $vendor ) { |
||
101 | $eventResults[$vendor->name . '_sum'] = with(clone $viewQuery)->where('tickets.event_id', $event->id) |
||
102 | ->where('purchases.vendor_id', $vendor->id) |
||
103 | ->sum('price_categories.price'); |
||
104 | } |
||
105 | |||
106 | // if a box office purchase exists, write the result, else 0 |
||
107 | $eventResults['boxoffice'] = $event->boxoffice ? $eventResults->boxoffice->total() : 0; |
||
108 | // Amount of money gained without people showing up |
||
109 | $eventResults['no-shows_sum'] = with(clone $viewQuery)->where('tickets.event_id', $event->id) |
||
110 | ->where('tickets.state', '<>', 'consumed') |
||
111 | ->sum('price_categories.price'); |
||
112 | |||
113 | |||
114 | |||
115 | // add the results to the end of the |
||
116 | array_push($result, $eventResults); |
||
117 | } |
||
118 | return $result; |
||
119 | } |
||
121 |