Test Setup Failed
Push — master ( c4eec8...60a8b6 )
by Martin
21:46
created

AnalyticController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 29
dl 0
loc 51
rs 10
c 2
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A downloadCSV() 0 5 1
A dashboard() 0 33 3
A downloadHelgaMetrics() 0 5 1
1
<?php
2
3
namespace App\Http\Controllers\Supervision;
4
5
use Illuminate\Http\Request;
6
use App\Http\Controllers\Controller;
7
use App\Event;
8
use App\Purchase;
9
use App\Project;
10
use App\Ticket;
11
12
class AnalyticController extends Controller
13
{
14
15
    public function dashboard()
16
    {
17
        $projects = Project::where('is_archived', 0)->get();
18
19
        $events = Event::whereIn('project_id', $projects->pluck('id'))->get();
20
        $totalEvents = $events->count();
21
22
        $totalSeats = 0;
23
        foreach ($events as $event) {
24
            $totalSeats += $event->seatMap->seats;
25
        }
26
        // Only fetch the purchase_ids for filtering the later query for relevant purchases
27
        $tickets = Ticket::whereIn('event_id', $events->pluck('id'))->pluck('purchase_id');
28
        $soldTickets = $tickets->count();
29
        $load = round(($soldTickets / $totalSeats) * 100, 0);
30
31
        // Prefilter the purchases on the filtered tickets
32
        $distinctPurchaseIds = $tickets->toArray();
33
        $distinctPurchaseIds = array_unique($distinctPurchaseIds, SORT_NUMERIC);
0 ignored issues
show
Unused Code introduced by
The assignment to $distinctPurchaseIds is dead and can be removed.
Loading history...
34
        $sales = Purchase::whereIn('id', $tickets)->where('state', 'paid')->get();
35
        $totalSales = $sales->count();
36
37
        $turnover = 0;
38
        foreach ($sales as $purchase) {
39
            $turnover += $purchase->total();
40
        }
41
42
        return view('supervision.dashboard', [
43
            'projects' => $projects,
44
            'totalEvents' => $totalEvents,
45
            'totalSales' => $totalSales,
46
            'turnover' => $turnover,
47
            'load' => $load,
48
        ]);
49
    }
50
51
    public function downloadCSV(Project $project)
52
    {
53
        return response()->streamDownload(function () use ($project) {
54
            echo view('csvs.tickets', ['events' => $project->events])->render();
1 ignored issue
show
Bug introduced by
Are you sure view('csvs.tickets', arr...ect->events))->render() of type array|string can be used in echo? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
            echo /** @scrutinizer ignore-type */ view('csvs.tickets', ['events' => $project->events])->render();
Loading history...
55
        }, 'export.csv');
56
    }
57
58
    public function downloadHelgaMetrics(Project $project)
59
    {
60
        return response()->streamDownload(function() use ($project) {
61
            echo view('csvs.helga-metrics', ['data' => $project->getHelgaMetrics()])->render();
2 ignored issues
show
Bug introduced by
Are you sure the usage of $project->getHelgaMetrics() targeting App\Project::getHelgaMetrics() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
Are you sure view('csvs.helga-metrics...gaMetrics()))->render() of type array|string can be used in echo? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
            echo /** @scrutinizer ignore-type */ view('csvs.helga-metrics', ['data' => $project->getHelgaMetrics()])->render();
Loading history...
62
        }, 'helgaMetrics.csv');
63
    }
64
}
65