Completed
Push — master ( cb5fa1...8201f5 )
by Vojta
06:45
created

ReservationForm::onRun()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 47
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 8.5125
c 0
b 0
f 0
cc 6
eloc 26
nc 18
nop 0
1
<?php namespace VojtaSvoboda\Reservations\Components;
2
3
use App;
4
use Cms\Classes\ComponentBase;
5
use Config;
6
use Exception;
7
use Flash;
8
use Illuminate\Support\Facades\Log;
9
use Input;
10
use Lang;
11
use October\Rain\Exception\ApplicationException;
12
use October\Rain\Exception\ValidationException;
13
use Redirect;
14
use Session;
15
use VojtaSvoboda\Reservations\Facades\ReservationsFacade;
16
17
/**
18
 * Reservation Form component.
19
 *
20
 * @package VojtaSvoboda\Reservations\Components
21
 */
22
class ReservationForm extends ComponentBase
23
{
24
    public function componentDetails()
25
	{
26
		return [
27
			'name' => 'vojtasvoboda.reservations::lang.reservationform.name',
28
			'description' => 'vojtasvoboda.reservations::lang.reservationform.description',
29
		];
30
	}
31
32
    /**
33
     * AJAX form submit handler.
34
     */
35
    public function onSubmit()
36
    {
37
        // check CSRF token
38
        if (Session::token() != Input::get('_token')) {
39
            throw new ApplicationException('Form session expired! Please refresh the page.');
40
        }
41
42
        /** @var ReservationsFacade $facade */
43
        $facade = App::make('vojtasvoboda.reservations.facade');
44
        $data = Input::all();
45
        $facade->storeReservation($data);
46
    }
47
48
    /**
49
     * Fallback for non-ajax POST request.
50
     */
51
	public function onRun()
0 ignored issues
show
Coding Style introduced by
onRun uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
52
	{
53
        /** @var ReservationsFacade $facade */
54
        $facade = App::make('vojtasvoboda.reservations.facade');
55
56
		$error = false;
57
		if (Input::get('submit')) {
58
59
            // check CSRF token
60
            if (Session::token() != Input::get('_token')) {
61
                $error = 'Form session expired! Please refresh the page.';
62
63
            } else {
64
65
                try {
66
                    $data = Input::all();
67
                    $facade->storeReservation($data);
68
                    $msg = Lang::get('vojtasvoboda.reservations::lang.reservationform.success');
69
                    Flash::success($msg);
70
71
                    return Redirect::to($this->page->url . '#' . $this->alias, 303);
72
73
                } catch(ValidationException $e) {
1 ignored issue
show
Bug introduced by
The class October\Rain\Exception\ValidationException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
74
                    $error = $e->getMessage();
75
76
                } catch(ApplicationException $e) {
1 ignored issue
show
Bug introduced by
The class October\Rain\Exception\ApplicationException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
77
                    $error = $e->getMessage();
78
79
                } catch(Exception $e) {
80
                    Log::error($e->getMessage());
81
                    $error = 'We\'re sorry, but something went wrong and the page cannot be displayed.';
82
                }
83
            }
84
		}
85
86
		// inject assets
87
        $this->injectAssets();
88
89
		// load booked dates
90
        $dates = $facade->getReservedDates();
91
92
        // template data
93
		$this->page['sent'] = Flash::check();
94
		$this->page['post'] = $_POST;
95
		$this->page['error'] = $error;
96
        $this->page['dates'] = json_encode($dates);
97
	}
98
99
    /**
100
     * Inject components assets.
101
     */
102
	private function injectAssets()
103
    {
104
        $this->addCss('/plugins/vojtasvoboda/reservations/assets/vendor/pickadate/lib/compressed/themes/classic.css');
105
        $this->addCss('/plugins/vojtasvoboda/reservations/assets/vendor/pickadate/lib/compressed/themes/classic.date.css');
106
        $this->addCss('/plugins/vojtasvoboda/reservations/assets/vendor/pickadate/lib/compressed/themes/classic.time.css');
107
        $this->addJs('/plugins/vojtasvoboda/reservations/assets/vendor/pickadate/lib/compressed/picker.js');
108
        $this->addJs('/plugins/vojtasvoboda/reservations/assets/vendor/pickadate/lib/compressed/picker.date.js');
109
        $this->addJs('/plugins/vojtasvoboda/reservations/assets/vendor/pickadate/lib/compressed/picker.time.js');
110
        $this->addJs('/plugins/vojtasvoboda/reservations/assets/js/reservationform.js');
111
    }
112
}
113