Passed
Push — master ( 5d308c...196a6a )
by Stone
05:53 queued 03:20
created

Home::sendContactForm()   B

Complexity

Conditions 7
Paths 64

Size

Total Lines 56
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 34
nc 64
nop 0
dl 0
loc 56
rs 8.4426
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
3
namespace App\Controllers;
4
5
use App\Models\PostModel;
6
use Core\Container;
7
use Core\Traits\StringFunctions;
8
9
/**
10
 * Class Home
11
 *
12
 * The home page
13
 *
14
 * @package App\Controllers
15
 */
16
class Home extends \Core\Controller
17
{
18
19
    use StringFunctions;
0 ignored issues
show
Bug introduced by
The trait Core\Traits\StringFunctions requires the property $childNodes which is not provided by App\Controllers\Home.
Loading history...
20
21
    protected $siteConfig;
22
    protected $sendMail;
23
24
    private $config;
25
26
    public function __construct(Container $container)
27
    {
28
        $this->loadModules[] = 'SiteConfig';
29
        $this->loadModules[] = 'SendMail';
30
        parent::__construct($container);
31
32
        $this->config = $this->siteConfig->getSiteConfig();
33
    }
34
35
    /**
36
     * Show the front page
37
     * @throws \ErrorException
38
     * @throws \ReflectionException
39
     * @throws \Twig_Error_Loader
40
     * @throws \Twig_Error_Runtime
41
     * @throws \Twig_Error_Syntax
42
     */
43
    public function index()
44
    {
45
        $frontPostModel = new PostModel($this->container);
46
47
        $frontPosts = $frontPostModel->getFrontPosts();
48
49
        $this->data['configs'] = $this->config;
50
        $this->data['navigation'] = $this->siteConfig->getMenu();
51
        $this->data['jumbotron'] = true;
52
        $this->data['front_posts'] = $frontPosts;
53
54
55
        //check if have prefilled form data and error messages
56
        $this->data["contactInfo"] = $this->session->get("contactInfo");
57
        $this->data["contactErrors"] = $this->session->get("contactErrors");
58
59
        //remove the set data as it is now sent to the template
60
        $this->session->remove("contactInfo");
61
        $this->session->remove("contactErrors");
62
63
64
        $this->renderView('Home');
65
    }
66
67
68
    /**
69
     * Send the contact form with error checking
70
     * @throws \Exception
71
     */
72
    public function sendContactForm()
73
    {
74
        $this->onlyPost();
75
76
        //verify input values (html special chars ?)
77
        $to = $this->config["admin_email_address"];
78
        $message = $this->request->getDataFull();
79
80
        //Error checking
81
82
        //check all the fields
83
        $error = false;
84
        $contactErrors = new \stdClass();
85
86
        if ($message["contactName"] == "") {
87
            $error = true;
88
            $contactErrors->contactName = "Name must not be empty";
89
        }
90
        if ($message["contactEmail"] == "") {
91
            $error = true;
92
            $contactErrors->contactEmail = "Email must not be empty";
93
        }
94
        if ($message["contactSubject"] == "") {
95
            $error = true;
96
            $contactErrors->contactSubject = "Subject must not be empty";
97
        }
98
        if ($message["contactMessage"] == "") {
99
            $error = true;
100
            $contactErrors->contactMessage = "Message must not be empty";
101
        }
102
        if (!$this->isEmail($message["contactEmail"])) {
103
            $error = true;
104
            $contactErrors->contactEmail = "email is not valid";
105
        }
106
107
        //If we found an error, return data to the register form and no create
108
        if ($error) {
109
            $this->session->set("contactInfo", $message);
110
            $this->session->set("contactErrors", $contactErrors);
111
            $this->response->redirect();
112
        }
113
114
        $config = $this->siteConfig->getSiteConfig();
115
116
        //from here all is good, send mail
117
        $userName = htmlspecialchars($message["contactName"]);
118
        $subject = "Contact from ".$config["site_name"]." : ";
119
        $subject .= htmlspecialchars($message["contactSubject"]);
120
        $textMessage = "<h1>message sent by ".$userName."</h1>";
121
        $textMessage .= htmlspecialchars($message["contactMessage"]);
122
        $from = $message["contactEmail"];
123
124
        $this->sendMail->send($to, $subject, $textMessage, $from);
125
126
        $this->alertBox->setAlert('Email sent');
127
        $this->response->redirect();
128
    }
129
}