Code Duplication    Length = 23-38 lines in 2 locations

src/SilverStripe/BehatExtension/Context/BasicContext.php 2 locations

@@ 140-177 (lines=38) @@
137
     *
138
     * @BeforeStep
139
     */
140
    public function handleAjaxBeforeStep(StepEvent $event)
141
    {
142
        try {
143
            $ajaxEnabledSteps = $this->getMainContext()->getAjaxSteps();
144
            $ajaxEnabledSteps = implode('|', array_filter($ajaxEnabledSteps));
145
146
            if (empty($ajaxEnabledSteps) || !preg_match('/(' . $ajaxEnabledSteps . ')/i', $event->getStep()->getText())) {
147
                return;
148
            }
149
150
            $javascript = <<<JS
151
if ('undefined' !== typeof window.jQuery && 'undefined' !== typeof window.jQuery.fn.on) {
152
    window.jQuery(document).on('ajaxStart.ss.test.behaviour', function(){
153
        window.__ajaxStatus = function() {
154
            return 'waiting';
155
        };
156
    });
157
    window.jQuery(document).on('ajaxComplete.ss.test.behaviour', function(e, jqXHR){
158
        if (null === jqXHR.getResponseHeader('X-ControllerURL')) {
159
            window.__ajaxStatus = function() {
160
                return 'no ajax';
161
            };
162
        }
163
    });
164
    window.jQuery(document).on('ajaxSuccess.ss.test.behaviour', function(e, jqXHR){
165
        if (null === jqXHR.getResponseHeader('X-ControllerURL')) {
166
            window.__ajaxStatus = function() {
167
                return 'success';
168
            };
169
        }
170
    });
171
}
172
JS;
173
            $this->getSession()->wait(500); // give browser a chance to process and render response
174
            $this->getSession()->executeScript($javascript);
175
        } catch (\WebDriver\Exception $e) {
176
            $this->logException($e);
177
        }
178
    }
179
180
    /**
@@ 188-210 (lines=23) @@
185
     *
186
     * @AfterStep ~@modal
187
     */
188
    public function handleAjaxAfterStep(StepEvent $event)
189
    {
190
        try {
191
            $ajaxEnabledSteps = $this->getMainContext()->getAjaxSteps();
192
            $ajaxEnabledSteps = implode('|', array_filter($ajaxEnabledSteps));
193
194
            if (empty($ajaxEnabledSteps) || !preg_match('/(' . $ajaxEnabledSteps . ')/i', $event->getStep()->getText())) {
195
                return;
196
            }
197
198
            $this->handleAjaxTimeout();
199
200
            $javascript = <<<JS
201
if ('undefined' !== typeof window.jQuery && 'undefined' !== typeof window.jQuery.fn.off) {
202
window.jQuery(document).off('ajaxStart.ss.test.behaviour');
203
window.jQuery(document).off('ajaxComplete.ss.test.behaviour');
204
window.jQuery(document).off('ajaxSuccess.ss.test.behaviour');
205
}
206
JS;
207
            $this->getSession()->executeScript($javascript);
208
        } catch (\WebDriver\Exception $e) {
209
            $this->logException($e);
210
        }
211
    }
212
213
    public function handleAjaxTimeout()