Completed
Push — master ( 784ac4...dbc548 )
by John
01:47
created

Form   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 38.98%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 135
ccs 23
cts 59
cp 0.3898
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B submit() 0 37 4
B prepareRequest() 0 35 6
A getUrl() 0 4 1
A getId() 0 4 1
1
<?php
2
3
namespace Escopecz\MauticFormSubmit\Mautic;
4
5
use Escopecz\MauticFormSubmit\Mautic;
6
7
/**
8
 * Mautic form
9
 */
10
class Form
11
{
12
    /**
13
     * @var Mautic
14
     */
15
    protected $mautic;
16
17
    /**
18
     * Form ID
19
     *
20
     * @var int
21
     */
22
    protected $id;
23
24
    /**
25
     * Constructor
26
     *
27
     * @param Mautic $mautic
28
     * @param int    $id
29
     */
30 10
    public function __construct(Mautic $mautic, $id)
31
    {
32 10
        $this->mautic = $mautic;
33 10
        $this->id = (int) $id;
34 10
    }
35
36
    /**
37
     * Submit the $data array to the Mautic form
38
     * Returns array containing info about the request, response and cookie
39
     *
40
     * @param  array  $data
41
     *
42
     * @return array
43
     */
44
    public function submit(array $data)
0 ignored issues
show
Coding Style introduced by
submit uses the super-global variable $_COOKIE 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...
45
    {
46
        $response = [];
47
        $request = $this->prepareRequest($data);
48
49
        $ch = curl_init($request['url']);
50
        curl_setopt($ch, CURLOPT_POST, 1);
51
        curl_setopt($ch, CURLOPT_POSTFIELDS, $request['query']);
52
53
        if (isset($request['header'])) {
54
            curl_setopt($ch, CURLOPT_HTTPHEADER, $request['header']);
55
        }
56
57
        if (isset($request['referer'])) {
58
            curl_setopt($ch, CURLOPT_REFERER, $request['referer']);
59
        }
60
61
        if (isset($request['cookie'])) {
62
            $ckfile = tempnam (sys_get_temp_dir(), 'mauticcookie');
63
            curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
64
        }
65
66
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
67
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
68
        curl_setopt($ch, CURLOPT_HEADER, 1);
69
        list($header, $content) = explode("\r\n\r\n", curl_exec($ch), 2);
70
        $response['header'] = $header;
71
        $response['content'] = htmlentities($content);
72
        $response['info'] = curl_getinfo($ch);
73
        curl_close($ch);
74
75
        return [
76
            '$_COOKIE' => $_COOKIE,
77
            'request' => $request,
78
            'response' => $response,
79
        ];
80
    }
81
82
    /**
83
     * Prepares data for CURL request based on provided form data, $_COOKIE and $_SERVER
84
     *
85
     * @param  array $data
86
     *
87
     * @return array
88
     */
89 2
    public function prepareRequest(array $data)
0 ignored issues
show
Coding Style introduced by
prepareRequest uses the super-global variable $_SERVER 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...
90
    {
91 2
        $contact = $this->mautic->getContact();
92 2
        $request = ['header'];
93
94 2
        $data['formId'] = $this->id;
95
96
        // return has to be part of the form data array so Mautic would accept the submission
97 2
        if (!isset($data['return'])) {
98 2
            $data['return'] = '';
99 1
        }
100
101 2
        $request['url'] = $this->getUrl();
102 2
        $request['data'] = ['mauticform' => $data];
103
104 2
        if ($contactId = $contact->getId()) {
105
            $request['data']['mtc_id'] = $contactId;
106
        }
107
108 2
        if ($contactIp = $contact->getIp()) {
109
            $request['header'][] = "X-Forwarded-For: $contactIp";
110
        }
111
112 2
        if ($sessionId = $contact->getMauticSessionIdFromCookie()) {
113
            $request['header'][] = "Cookie: mautic_session_id=$sessionId";
114
        }
115
116 2
        if (isset($_SERVER['HTTP_REFERER'])) {
117
            $request['referer'] = $_SERVER["HTTP_REFERER"];
118
        }
119
120 2
        $request['query'] = http_build_query($request['data']);
121
122 2
        return $request;
123
    }
124
125
    /**
126
     * Builds the form URL
127
     *
128
     * @return string
129
     */
130 4
    public function getUrl()
131
    {
132 4
        return sprintf('%s/form/submit?formId=%d', $this->mautic->getBaseUrl(), $this->id);
133
    }
134
135
    /**
136
     * Returns the Form ID
137
     *
138
     * @return int
139
     */
140 6
    public function getId()
141
    {
142 6
        return $this->id;
143
    }
144
}
145