FormSubmitAjax   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 7
Bugs 1 Features 3
Metric Value
wmc 4
c 7
b 1
f 3
lcom 1
cbo 2
dl 0
loc 44
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A ajax() 0 18 3
1
<?php
2
3
/*
4
 * This file is part of the WPSymfonyForm plugin.
5
 *
6
 * Copyright (c) 2015-2016 LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LIN3S\WPSymfonyForm\Ajax;
13
14
use LIN3S\WPSymfonyForm\Controller\AjaxController;
15
use LIN3S\WPSymfonyForm\Registry\FormWrapperRegistry;
16
17
/**
18
 * Form submit ajax class.
19
 *
20
 * @author Gorka Laucirica <[email protected]>
21
 */
22
class FormSubmitAjax
23
{
24
    /**
25
     * The form wrapper registry.
26
     *
27
     * @var FormWrapperRegistry
28
     */
29
    private $formWrapperRegistry;
30
31
    /**
32
     * Constructor.
33
     *
34
     * @param FormWrapperRegistry $formWrapperRegistry The form wrapper registry
35
     * @param string              $action              Action used by WordPress to identify this AJAX request
36
     */
37
    public function __construct(FormWrapperRegistry $formWrapperRegistry, $action = 'form_submit')
38
    {
39
        add_action('wp_ajax_nopriv_' . $action, [$this, 'ajax']);
0 ignored issues
show
Unused Code introduced by
The call to the function add_action() seems unnecessary as the function has no side-effects.
Loading history...
40
        add_action('wp_ajax_' . $action, [$this, 'ajax']);
0 ignored issues
show
Unused Code introduced by
The call to the function add_action() seems unnecessary as the function has no side-effects.
Loading history...
41
        $this->formWrapperRegistry = $formWrapperRegistry;
42
    }
43
44
    /**
45
     * Call to be executed on AJAX request.
46
     */
47
    public function ajax()
0 ignored issues
show
Coding Style introduced by
ajax 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...
48
    {
49
        try {
50
            unset($_POST['action']);
51
            if (count($_POST) !== 1) {
52
                throw new \InvalidArgumentException();
53
            }
54
55
            $formType = key($_POST);
56
            $formWrapper = $this->formWrapperRegistry->get($formType);
57
            echo(new AjaxController())->ajaxAction($formWrapper);
58
            die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method ajax() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
59
        } catch (\InvalidArgumentException $exception) {
60
            echo json_encode(['errors' => ['unknown form_type']]);
61
            http_response_code(400);
62
            die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method ajax() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
63
        }
64
    }
65
}
66