Completed
Branch FET-10766-extract-activation-d... (a650cc)
by
unknown
116:57 queued 106:13
created

Checkin::createCheckinCheckoutObject()   C

Complexity

Conditions 8
Paths 12

Size

Total Lines 101
Code Lines 64

Duplication

Lines 12
Ratio 11.88 %

Importance

Changes 0
Metric Value
cc 8
eloc 64
nc 12
nop 2
dl 12
loc 101
rs 5.2676
c 0
b 0
f 0

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
namespace EventEspresso\core\libraries\rest_api\controllers\rpc;
3
4
use WP_Error;
5
use WP_REST_Response;
6
use WP_REST_Request;
7
use EE_Registration;
8
use EEM_Registration;
9
use EE_Capabilities;
10
use EE_Checkin;
11
use EEM_Checkin;
12
use EED_Core_Rest_Api;
13
use EventEspresso\core\libraries\rest_api\controllers\Base as Base;
14
use EventEspresso\core\libraries\rest_api\controllers\model\Read;
15
16
if (! defined('EVENT_ESPRESSO_VERSION')) {
17
    exit('No direct script access allowed');
18
}
19
20
21
22
/**
23
 * Controller for handling checkin/checkout requests
24
 * Handles the RPC-style requests to check a registrant into a datetime
25
 * or check them out
26
 *
27
 * @package               Event Espresso
28
 * @subpackage
29
 * @author                Mike Nelson
30
 */
31
class Checkin extends Base
32
{
33
34
    /**
35
     * @param WP_REST_Request $request
36
     * @param string           $version
37
     * @return WP_Error|WP_REST_Response
38
     */
39
    public static function handleRequestToggleCheckin(WP_REST_Request $request, $version)
40
    {
41
        $controller = new Checkin();
42
        return $controller->createCheckinCheckoutObject($request, $version);
43
    }
44
45
46
47
    /**
48
     * Toggles whether the user is checked in or not.
49
50
     *
51
     * @param WP_REST_Request $request
52
     * @param string           $version
53
     * @return WP_Error|WP_REST_Response
54
     */
55
    protected function createCheckinCheckoutObject(WP_REST_Request $request, $version)
56
    {
57
        $reg_id = $request->get_param('REG_ID');
58
        $dtt_id = $request->get_param('DTT_ID');
59
        $force = $request->get_param('force');
60
        if ($force == 'true') {
61
            $force = true;
62
        } else {
63
            $force = false;
64
        }
65
        $reg = EEM_Registration::instance()->get_one_by_ID($reg_id);
66
        if (! $reg instanceof EE_Registration) {
67
            return $this->sendResponse(
68
                new WP_Error(
69
                    'rest_registration_toggle_checkin_invalid_id',
70
                    sprintf(
71
                        __(
72
                            'You cannot checkin registration with ID %1$s because it doesn\'t exist.',
73
                            'event_espresso'
74
                        ),
75
                        $reg_id
76
                    ),
77
                    array('status' => 422)
78
                )
79
            );
80
        }
81 View Code Duplication
        if (! EE_Capabilities::instance()->current_user_can('ee_edit_checkin', 'rest_api_checkin_endpoint', $reg_id)) {
82
            return $this->sendResponse(
83
                new WP_Error(
84
                    'rest_user_cannot_toggle_checkin',
85
                    sprintf(
86
                        __('You are not allowed to checkin registration with ID %1$s.', 'event_espresso'),
87
                        $reg_id
88
                    ),
89
                    array('status' => 403)
90
                )
91
            );
92
        }
93
        $success = $reg->toggle_checkin_status($dtt_id, ! $force);
94
        if ($success === false) {
95
            //check if we know they can't check in because they're not approved and we aren't forcing
96
            if (! $reg->is_approved() && ! $force) {
97
                //rely on EE_Error::add_error messages to have been added to give more data about why it failed
98
                return $this->sendResponse(
99
                    new WP_Error(
100
                        'rest_toggle_checkin_failed',
101
                        __(
102
                            // @codingStandardsIgnoreStart
103
                            'Registration check-in failed because the registration is not approved. You may attempt to force checking in though.',
104
                            // @codingStandardsIgnoreEnd
105
                            'event_espresso'
106
                        )
107
                    )
108
                );
109
            }
110
            return $this->sendResponse(
111
                new WP_Error(
112
                    'rest_toggle_checkin_failed_not_forceable',
113
                    __('Registration checkin failed. Please see additional error data.', 'event_espresso')
114
                )
115
            );
116
        }
117
        $checkin = EEM_Checkin::instance()->get_one(
118
            array(
119
                array(
120
                    'REG_ID' => $reg_id,
121
                    'DTT_ID' => $dtt_id,
122
                ),
123
                'order_by' => array(
124
                    'CHK_timestamp' => 'DESC',
125
                ),
126
            )
127
        );
128
        if (! $checkin instanceof EE_Checkin) {
129
            return $this->sendResponse(
130
                new WP_Error(
131
                    'rest_toggle_checkin_error',
132
                    sprintf(
133
                        __(
134
                            // @codingStandardsIgnoreStart
135
                            'Supposedly we created a new checkin object for registration %1$s at datetime %2$s, but we can\'t find it.',
136
                            // @codingStandardsIgnoreEnd
137
                            'event_espresso'
138
                        ),
139
                        $reg_id,
140
                        $dtt_id
141
                    )
142
                )
143
            );
144
        }
145
        $get_request = new WP_REST_Request(
146
            'GET',
147
            '/' . EED_Core_Rest_Api::ee_api_namespace . 'v' . $version . '/checkins/' . $checkin->ID()
148
        );
149
        $get_request->set_url_params(
150
            array(
151
                'id' => $checkin->ID(),
152
            )
153
        );
154
        return Read::handleRequestGetOne($get_request, $version, 'Checkin');
155
    }
156
}
157