SettingsController::testTicket()   B
last analyzed

Complexity

Conditions 3
Paths 16

Size

Total Lines 96
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 72
c 1
b 0
f 0
nc 16
nop 0
dl 0
loc 96
rs 8.6109

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Event;
6
use App\Http\Controllers\Controller;
7
use App\Location;
8
use App\PriceCategory;
9
use App\PriceList;
10
use App\Project;
11
use App\Purchase;
12
use App\SeatMap;
13
use App\Setting;
14
use App\Ticket;
15
use App\User;
16
use Illuminate\Http\Request;
17
use Illuminate\Support\Facades\App;
18
use Illuminate\Support\Facades\DB;
19
use Illuminate\Support\Facades\Storage;
20
use Illuminate\Support\Str;
21
use Mews\Purifier\Facades\Purifier;
22
use Spipu\Html2Pdf\Exception\Html2PdfException;
23
use Spipu\Html2Pdf\Html2Pdf;
24
25
class SettingsController extends Controller
26
{
27
    // Display all available settings
28
    public function index()
29
    {
30
        $terms = Setting::where('name', 'terms')->where('lang', App::getLocale())->first();
31
        $termsHtml = $terms ? $terms->value : view('components.default-texts.terms')->render();
32
33
        $privacy = Setting::where('name', 'privacy')->where('lang', App::getLocale())->first();
34
        $privacyHtml = $privacy ? $privacy->value : view('components.default-texts.privacy')->render();
35
36
        return view('admin.settings', [
37
            'terms' => $termsHtml,
38
            'privacy' => $privacyHtml
39
        ]);
40
    }
41
42
    /**
43
     * Receives HTML-input
44
     * 
45
     * Attention: Function might be target for XSS attacks. Handle input carfully!!!
46
     */
47
    public function updateTerms(Request $request)
48
    {
49
        Setting::updateOrCreate(
50
            ['name' => 'terms', 'lang' => App::getLocale()],
51
            ['value' => Purifier::clean($request->input('terms'))]
52
        );
53
        return redirect()->route('admin.settings.dashboard')->with('state', 'Success - Terms and Conditions updated.');
54
    }
55
56
    /**
57
     * Receives HTML-input
58
     * 
59
     * Attention: Function might be target for XSS attacks. Handle input carfully!!!
60
     */
61
    public function updatePrivacy(Request $request)
62
    {
63
        Setting::updateOrCreate(
64
            ['name' => 'privacy', 'lang' => App::getLocale()],
65
            ['value' => Purifier::clean($request->input('privacy'))]
66
        );
67
        return redirect()->route('admin.settings.dashboard')->with('state', 'Success - Privacy statement updated.');
68
    }
69
70
    /**
71
     * File-Upload
72
     */
73
    public function updateLogo(Request $request)
74
    {
75
        $validatedFile = $request->validate([
76
            'file' => 'file|max:30000|mimes:jpeg,bmp,png,svg,jpg'
77
        ]);
78
79
        // Only extract file extension of the new logo picture
80
        $extension = $validatedFile['file']->extension();
81
        // set it to a generic name to overwrite any existing logo with the same extension
82
        $logoStoreName = 'logo.' . $extension;
83
84
        // Store file as new logo and update the corresponding setting
85
        $validatedFile['file']->storeAs('images', $logoStoreName);
86
        Setting::updateOrCreate(
87
            ['name' => 'logo', 'lang' => 'en'],
88
            ['value' => 'images/' . $logoStoreName]
89
        );
90
91
        // Redirect to source page with success message
92
        return redirect()->route('admin.settings.dashboard')->with('state', 'Success - Logo updated.');
93
    }
94
95
    // Purge the current logo
96
    public function deleteLogo(Request $request)
97
    {
98
        $logo = Setting::where([
99
            ['name', 'logo'],
100
            ['lang', 'en']
101
        ])->first();
102
103
        Storage::delete($logo->value);
104
        $logo->delete();
105
106
        // Redirect to source page with success message
107
        return redirect()->route('admin.settings.dashboard')->with('state', 'Success - Logo deleted.');
108
    }
109
110
    /**
111
     * Returns a ticket filled with dummy data to check the
112
     * correct processing of the logo in the layout
113
     */
114
    public function testTicket()
115
    {
116
        // Wrap dummy data creation in a transaction in order
117
        // to not actually store it in the production database.
118
        //
119
        // We have to use eloquent models and cannot use factories,
120
        // because factories are not available on prod installations.
121
        DB::beginTransaction();
122
        $vendor = User::create([
123
            'name'     => 'TestVendor FamilynameOfVendor',
124
            'email'    => '[email protected]',
125
            'password' => ''
126
        ]);
127
        $customer = User::create([
128
            'name'     => 'Avery LongName ButItShouldWork',
129
            'email'    => '[email protected]',
130
            'password' => ''
131
        ]);
132
133
        $priceList = PriceList::create([
134
            'name' => 'Testlist'
135
        ]);
136
        $priceCategory = PriceCategory::create([
137
            'name'        => 'TestCategory',
138
            'price'       => 450,
139
            'description' => 'Just for testing the ticket layout'
140
        ]);
141
        $priceList->categories()->save($priceCategory);
142
143
        $location = Location::create([
144
            'name'    => 'Some Test Location anywhere',
145
            'address' => 'Somewhere over the rainbox street 42, 424242 Kummerland, Wilde13'
146
        ]);
147
        $project = Project::create([
148
            'name'        => 'A Test project',
149
            'description' => 'Something something testing',
150
            'is_archived' => false
151
        ]);
152
        $seatMap = SeatMap::create([
153
            'name'        => 'TestSeatMap',
154
            'seats'       => 2000,
155
            'description' => 'Some test description that does not really matter',
156
            'layout'      => null
157
        ]);
158
        $now = now();
159
160
        $purchase = new Purchase();
161
        $purchase->state = 'paid';
162
        $purchase->state_updated = $now;
163
        $purchase->random_id = Str::random(20);
164
        $purchase->payment_secret = Str::random(20);
165
        $purchase->customer_id = $customer->id;
166
        $purchase->vendor_id = $vendor->id;
167
        $purchase->payment_id = 'dummy-reference';
168
        $purchase->save();
169
170
        $event = new Event();
171
        $event->second_name        = 'First event';
172
        $event->customer_sell_stop = $now->add(1, 'day');
173
        $event->retailer_sell_stop = $now->add(2, 'day');
174
        $event->start_date         = $now->add(1, 'day');
175
        $event->end_date           = $now->add(10, 'day');
176
        $event->project_id         = $project->id;
177
        $event->location_id        = $location->id;
178
        $event->seat_map_id        = $seatMap->id;
179
        $event->price_list_id      = $priceList->id;
180
        $event->state              = 'open';
181
        $event->save();
182
183
        for ($i = 0; $i < 8; $i++) {
184
            $ticket = new Ticket();
185
            $ticket->random_id         = Str::random(20);
186
            $ticket->seat_number       = $i + 1000;
187
            $ticket->event_id          = $event->id;
188
            $ticket->purchase_id       = $purchase->id;
189
            $ticket->price_category_id = $priceCategory->id;
190
            $ticket->state             = 'consumed';
191
            $ticket->save();
192
        }
193
        try {
194
            $html2pdf = new HTML2PDF('P', 'A4', 'de', true, 'UTF-8', 0);
195
            $html2pdf->pdf->SetDisplayMode('fullpage');
196
            $html2pdf->pdf->SetAuthor(config('app.name'));
197
            $html2pdf->pdf->SetTitle('Purchase #' . $purchase->id);
198
199
            // Generate pdf-content by passing the tickets to the view
200
            $content = view('pdfs.ticket-v2', ['tickets' => $purchase->tickets])->render();
201
            $html2pdf->writeHTML($content);
202
203
            $html2pdf->output('tickets-' . $purchase->id . '.pdf');
204
        } catch (Html2PdfException $e) {
205
            $html2pdf->clean();
206
            DB::rollBack();
207
            return redirect()->route('ticket.purchase', ['purchase' => $purchase])->with('state', $e->getMessage());
208
        }
209
        DB::rollBack();
210
    }
211
}
212