Completed
Branch master (bb48cc)
by vijay
148:50 queued 92:39
created

CheckoutController::checkoutAction()   B

Complexity

Conditions 2
Paths 11

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 8.8571
cc 2
eloc 21
nc 11
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Front;
4
5
use App\Http\Controllers\Common\TemplateController;
6
use App\Http\Controllers\Controller;
7
use App\Http\Requests\Front\CheckoutRequest;
8
use App\Model\Common\Setting;
9
use App\Model\Common\Template;
10
use App\Model\Order\Invoice;
11
use App\Model\Order\InvoiceItem;
12
use App\Model\Order\Order;
13
use App\Model\Payment\Plan;
14
use App\Model\Product\Addon;
15
use App\Model\Product\Price;
16
use App\Model\Product\Product;
17
use App\Model\Product\Subscription;
18
use App\User;
19
use Cart;
20
use Illuminate\Http\Request;
21
22
class CheckoutController extends Controller {
23
24
    public $subscription;
25
    public $plan;
26
    public $templateController;
27
    public $product;
28
    public $price;
29
    public $user;
30
    public $setting;
31
    public $template;
32
    public $order;
33
    public $addon;
34
    public $invoice;
35
    public $invoiceItem;
36
37
    public function __construct() {
38
        $subscription = new Subscription();
39
        $this->subscription = $subscription;
40
41
        $plan = new Plan();
42
        $this->plan = $plan;
43
44
        $templateController = new TemplateController();
45
        $this->templateController = $templateController;
46
47
        $product = new Product();
48
        $this->product = $product;
49
50
        $price = new Price();
51
        $this->price = $price;
52
53
        $user = new User();
54
        $this->user = $user;
55
56
        $setting = new Setting();
57
        $this->setting = $setting;
58
59
        $template = new Template();
60
        $this->template = $template;
61
62
        $order = new Order();
63
        $this->order = $order;
64
65
        $invoice = new Invoice();
66
        $this->invoice = $invoice;
67
68
        $invoiceItem = new InvoiceItem();
69
        $this->invoiceItem = $invoiceItem;
70
    }
71
72
    public function CheckoutForm() {
73
        try {
74
            $content = Cart::getContent();
75
            foreach ($content as $item) {
76
                $attributes[] = $item->attributes;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$attributes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $attributes = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
77
            }
78
79
            return view('themes.default1.front.checkout', compact('content', 'attributes'));
80
        } catch (\Exception $ex) {
81
            return redirect()->back()->with('fails', $ex->getMessage());
82
        }
83
    }
84
85
    public function postCheckout(CheckoutRequest $request) {
86
        try {
87
            if (\Cart::getSubTotal() > 0) {
88
                $v = $this->validate($request, [
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $v is correct as $this->validate($request...ateway' => 'required')) (which targets Illuminate\Foundation\Va...tesRequests::validate()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

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

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

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

Loading history...
Unused Code introduced by
$v is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
89
                    'payment_gateway' => 'required',
90
                ]);
91
            }
92
            if (!$this->setting->where('id', 1)->first()) {
93
                return redirect()->back()->with('fails', 'Complete your settings');
94
            }
95
96
            $httpMethod = $request->method();
97
            //dd($httpMethod);
98
            if ($httpMethod == 'PATCH') {
99
                //do update the auth user
100
101
                \Auth::user()->fill($request->input())->save();
102
            } elseif ($httpMethod == 'POST') {
103
                
104
                
105
                //do saving user
106
107
                $str = str_random(8);
108
                $password = \Hash::make($str);
109
                $this->user->password = $password;
0 ignored issues
show
Documentation introduced by
The property password does not exist on object<App\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
110
                $this->user->fill($request->input())->save();
0 ignored issues
show
Bug introduced by
It seems like $request->input() targeting Illuminate\Http\Request::input() can also be of type string; however, Illuminate\Database\Eloquent\Model::fill() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
111
                
112
                $token = str_random(40);
113
                $activate = new \App\Model\User\AccountActivate();
114
                $activate->create(['email' => $this->user->email, 'token' => $token]);
0 ignored issues
show
Documentation introduced by
The property email does not exist on object<App\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
115
                $url = url("activate/$token");
116
117
                //send welcome note
118
                $settings = $this->setting->where('id', 1)->first();
119
                $from = $settings->email;
120
                $to = $this->user->email;
0 ignored issues
show
Documentation introduced by
The property email does not exist on object<App\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
121
                $data = $this->template->where('id', $settings->where('id', 1)->first()->welcome_mail)->first()->data;
122
                $replace = ['name' => $this->user->first_name . ' ' . $this->user->last_name, 'username' => $this->user->email, 'password' => $str,'url'=>$url];
0 ignored issues
show
Documentation introduced by
The property first_name does not exist on object<App\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property last_name does not exist on object<App\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property email does not exist on object<App\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
123
                $this->templateController->Mailing($from, $to, $data, 'Welcome Email', $replace);
124
125
                \Auth::login($this->user);
126
            }
127
128
            /*
129
             * Do order, invoicing etc
130
             */
131
            $invoice_controller = new \App\Http\Controllers\Order\InvoiceController();
132
            $invoice = $invoice_controller->GenerateInvoice();
133
            $payment_method = $request->input('payment_gateway');
134
            if (!$payment_method) {
135
                $payment_method = 'free';
136
            }
137
            $invoiceid = $invoice->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<App\Model\Order\Invoice>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
138
            $amount = $invoice->grand_total;
0 ignored issues
show
Documentation introduced by
The property grand_total does not exist on object<App\Model\Order\Invoice>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
139
            $payment = $invoice_controller->doPayment($payment_method, $invoiceid, $amount);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $payment is correct as $invoice_controller->doP...d, $invoiceid, $amount) (which targets App\Http\Controllers\Ord...Controller::doPayment()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

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

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

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

Loading history...
Unused Code introduced by
$payment is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
140
141
            //trasfer the control to event if cart price is not equal 0
142
            if (Cart::getSubTotal() != 0) {
143
                \Event::fire(new \App\Events\PaymentGateway(['request' => $request, 'cart' => Cart::getContent(), 'order' => []]));
144
            } else {
145
                $this->checkoutAction($invoice);
146
            }
147
            return redirect()->back()->with('success', \Lang::get('message.check-your-mail-for-further-datails'));
148
        } catch (\Exception $ex) {
149
            dd($ex);
150
            return redirect()->back()->with('fails', $ex->getMessage());
151
        }
152
    }
153
154
    public function checkoutAction($invoice) {
155
        try {
156
            //dd($invoice);
157
            //get elements from invoice
158
            $invoice_number = $invoice->number;
159
            $invoice_id = $invoice->id;
160
            $invoice_items = $this->invoiceItem->findOrFail($invoice_id);
161
            $product = $invoice_items->product_name;
162
            
163
            $user_id = \Auth::user()->id;
164
            $url = url("download/$user_id/$invoice_number");
165
            
166
            //get system values
167
            $settings = new Setting();
168
            $settings = $settings->findOrFail(1);
169
            $name = \Auth::user()->first_name.' '.\Auth::user()->last_name;
170
            $from = $settings->email;
171
            $to = \Auth::user()->email;
172
            $data = $this->template->where('name','download link')->first()->data;
173
            $subject = "download";
174
            $replace = ['url'=>$url,'name'=>$name,'product'=>$product];
175
            
176
            //send mail
177
            $template_controller = new TemplateController();
178
            $template_controller->Mailing($from, $to, $data, $subject,$replace);
179
        } catch (\Exception $ex) {
180
            dd($ex);
181
            return redirect()->back()->with('fails', $ex->getMessage());
182
        }
183
    }
184
185
//    public function GenerateOrder() {
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
186
//        try {
187
//
188
//            $products = [];
189
//            $items = \Cart::getContent();
190
//            foreach ($items as $item) {
191
//
192
//                //this is product
193
//                $id = $item->id;
194
//                $this->AddProductToOrder($id);
195
//            }
196
//        } catch (\Exception $ex) {
197
//            dd($ex);
198
//            throw new \Exception('Can not Generate Order');
199
//        }
200
//    }
201
//
202
//    public function AddProductToOrder($id) {
203
//        try {
204
//            $cart = \Cart::get($id);
205
//            $client = \Auth::user()->id;
206
//            $payment_method = \Input::get('payment_gateway');
207
//            $promotion_code = '';
208
//            $order_status = 'pending';
209
//            $serial_key = '';
210
//            $product = $id;
211
//            $addon = '';
212
//            $domain = '';
213
//            $price_override = $cart->getPriceSumWithConditions();
214
//            $qty = $cart->quantity;
215
//
216
//            $planid = $this->price->where('product_id', $id)->first()->subscription;
217
//
218
//            $or = $this->order->create(['client' => $client, 'payment_method' => $payment_method, 'promotion_code' => $promotion_code, 'order_status' => $order_status, 'serial_key' => $serial_key, 'product' => $product, 'addon' => $addon, 'domain' => $domain, 'price_override' => $price_override, 'qty' => $qty]);
219
//
220
//            $this->AddSubscription($or->id, $planid);
221
//        } catch (\Exception $ex) {
222
//            dd($ex);
223
//            throw new \Exception('Can not Generate Order for Product');
224
//        }
225
//    }
226
//
227
//    public function AddSubscription($orderid, $planid) {
228
//        try {
229
//            $days = $this->plan->where('id', $planid)->first()->days;
230
//            //dd($days);
231
//            if ($days > 0) {
232
//                $dt = \Carbon\Carbon::now();
233
//                //dd($dt);
234
//                $user_id = \Auth::user()->id;
235
//                $ends_at = $dt->addDays($days);
236
//            } else {
237
//                $ends_at = '';
238
//            }
239
//            $this->subscription->create(['user_id' => \Auth::user()->id, 'plan_id' => $planid, 'order_id' => $orderid, 'ends_at' => $ends_at]);
240
//        } catch (\Exception $ex) {
241
//            dd($ex);
242
//            throw new \Exception('Can not Generate Subscription');
243
//        }
244
//    }
245
//
246
//    public function GenerateInvoice() {
247
//        try {
248
//
249
//            $user_id = \Auth::user()->id;
250
//            $number = rand(11111111, 99999999);
251
//            $date = \Carbon\Carbon::now();
252
//            $grand_total = \Cart::getSubTotal();
253
//
254
//            $invoice = $this->invoice->create(['user_id' => $user_id, 'number' => $number, 'date' => $date, 'grand_total' => $grand_total]);
255
//            foreach (\Cart::getContent() as $cart) {
256
//                $this->CreateInvoiceItems($invoice->id, $cart);
257
//            }
258
//        } catch (\Exception $ex) {
259
//            dd($ex);
260
//            throw new \Exception('Can not Generate Invoice');
261
//        }
262
//    }
263
//
264
//    public function CreateInvoiceItems($invoiceid, $cart) {
265
//        try {
266
//
267
//            $product_name = $cart->name;
268
//            $regular_price = $cart->price;
269
//            $quantity = $cart->quantity;
270
//            $subtotal = $cart->getPriceSumWithConditions();
271
//
272
//            $tax_name = '';
273
//            $tax_percentage = '';
274
//
275
//            foreach ($cart->attributes['tax'] as $tax) {
276
//                //dd($tax['name']);
277
//                $tax_name .= $tax['name'] . ',';
278
//                $tax_percentage .= $tax['rate'] . ',';
279
//            }
280
//
281
//            //dd($tax_name);
282
//
283
//            $invoiceItem = $this->invoiceItem->create(['invoice_id' => $invoiceid, 'product_name' => $product_name, 'regular_price' => $regular_price, 'quantity' => $quantity, 'tax_name' => $tax_name, 'tax_percentage' => $tax_percentage, 'subtotal' => $subtotal]);
284
//        } catch (\Exception $ex) {
285
//            dd($ex);
286
//            throw new \Exception('Can not create Invoice Items');
287
//        }
288
//    }
289
}
290