Passed
Push — master ( be1785...591f13 )
by Ajit
07:10
created

DataMigrationController::migrateExcel()   F

Complexity

Conditions 21
Paths > 20000

Size

Total Lines 138
Code Lines 106

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 106
dl 0
loc 138
rs 0
c 0
b 0
f 0
cc 21
nc 21086
nop 0

How to fix   Long Method    Complexity   

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;
4
5
use DB;
6
use Auth;
7
use App\Member;
8
use App\Invoice;
9
use App\InvoiceDetail;
10
11
class DataMigrationController extends Controller
12
{
13
    public function __construct()
14
    {
15
        $this->middleware('auth');
16
    }
17
18
    public function migrate()
19
    {
20
        DB::beginTransaction();
21
        try {
22
            //From: InvoiceDetail->tax To: Invoice->tax
23
            $oldInvoiceDetailsTax = InvoiceDetail::where('item_name', '=', 'Taxes')->get();
24
25
            foreach ($oldInvoiceDetailsTax as $taxDetail) {
26
                Invoice::where('id', $taxDetail->invoice_id)->update(['tax' => $taxDetail->item_amount]);
27
            }
28
29
            //From: InvoiceDetail->admission To: Invoice->additional_fees
30
            $oldInvoiceDetailsAdmission = InvoiceDetail::where('item_name', '=', 'Admission')->get();
31
32
            foreach ($oldInvoiceDetailsAdmission as $admissionDetail) {
33
                Invoice::where('id', $admissionDetail->invoice_id)->update(['additional_fees' => $admissionDetail->item_amount]);
34
            }
35
36
            //Now delete the admission and tax rows from InvoiceDetails
37
            InvoiceDetail::where('item_name', '=', 'Admission')->delete();
38
            InvoiceDetail::where('item_name', '=', 'Taxes')->delete();
39
40
            //From: Member->plan_id To: InvoiceDetail->plan_id
41
            $invoices = Invoice::all();
42
43
            foreach ($invoices as $invoice) {
44
                InvoiceDetail::where('invoice_id', $invoice->id)->update(['plan_id' => $invoice->member->plan_id]);
0 ignored issues
show
Bug introduced by
The property plan_id does not seem to exist on App\Member. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
45
            }
46
47
            DB::commit();
48
        } catch (\Exception $e) {
49
            DB::rollback();
50
        }
51
    }
52
53
    public function migrateMedia()
54
    {
55
        DB::beginTransaction();
56
57
        try {
58
            $members = Member::all();
59
60
            foreach ($members as $member) {
61
                $profileImage = base_path('public/assets/img/profile/profile_'.$member->id.'.jpg');
62
                $proofImage = base_path('public/assets/img/proof/proof_'.$member->id.'.jpg');
63
64
                if (file_exists($profileImage)) {
65
                    $member->addMedia($profileImage)->usingFileName('profile_'.$member->id.'.jpg')->toCollection('profile');
66
                }
67
68
                if (file_exists($proofImage)) {
69
                    $member->addMedia($proofImage)->usingFileName('proof_'.$member->id.'.jpg')->toCollection('proof');
70
                }
71
72
                DB::commit();
73
            }
74
        } catch (\Exception $e) {
75
            DB::rollback();
76
        }
77
    }
78
}
79