Passed
Push — Security_and_bug_fixes ( b910f4...d592f0 )
by Stone
02:28
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 App\Models\UserModel;
7
use Core\Container;
8
use Core\Traits\StringFunctions;
9
10
/**
11
 * Class Home
12
 *
13
 * The home page
14
 *
15
 * @package App\Controllers
16
 */
17
class Home extends \Core\Controller
18
{
19
20
    use StringFunctions;
21
22
    protected $siteConfig;
23
    protected $sendMail;
24
25
    private $config;
26
    private $userModel;
27
28
    public function __construct(Container $container)
29
    {
30
        $this->loadModules[] = 'SiteConfig';
31
        $this->loadModules[] = 'SendMail';
32
        parent::__construct($container);
33
34
        $this->config = $this->siteConfig->getSiteConfig();
35
        $this->userModel = new UserModel($this->container);
36
        if($this->auth->isuser())
37
        {
38
            $this->data["user"] = $this->userModel->getUserDetailsById($this->session->get("userId"));
0 ignored issues
show
Bug introduced by
It seems like $this->session->get('userId') can also be of type null; however, parameter $userId of App\Models\UserModel::getUserDetailsById() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
            $this->data["user"] = $this->userModel->getUserDetailsById(/** @scrutinizer ignore-type */ $this->session->get("userId"));
Loading history...
39
        }
40
    }
41
42
    /**
43
     * Show the front page
44
     * @throws \ErrorException
45
     * @throws \ReflectionException
46
     * @throws \Twig_Error_Loader
47
     * @throws \Twig_Error_Runtime
48
     * @throws \Twig_Error_Syntax
49
     */
50
    public function index()
51
    {
52
        $frontPostModel = new PostModel($this->container);
53
54
        $frontPosts = $frontPostModel->getFrontPosts();
55
56
        $this->data['configs'] = $this->config;
57
        $this->data['navigation'] = $this->siteConfig->getMenu();
58
        $this->data['jumbotron'] = true;
59
        $this->data['front_posts'] = $frontPosts;
60
61
62
        //check if have prefilled form data and error messages
63
        $this->data["contactInfo"] = $this->session->get("contactInfo");
64
        $this->data["contactErrors"] = $this->session->get("contactErrors");
65
66
        //remove the set data as it is now sent to the template
67
        $this->session->remove("contactInfo");
68
        $this->session->remove("contactErrors");
69
70
71
        $this->renderView('Home');
72
    }
73
74
    public function contact()
75
    {
76
77
        $this->data['configs'] = $this->config;
78
        $this->data['navigation'] = $this->siteConfig->getMenu();
79
80
        //check if have prefilled form data and error messages
81
        $this->data["contactInfo"] = $this->session->get("contactInfo");
82
        $this->data["contactErrors"] = $this->session->get("contactErrors");
83
84
        //remove the set data as it is now sent to the template
85
        $this->session->remove("contactInfo");
86
        $this->session->remove("contactErrors");
87
88
        $this->renderView('Contact');
89
    }
90
91
92
    /**
93
     * Send the contact form with error checking
94
     * @throws \Exception
95
     */
96
    public function sendContactForm()
97
    {
98
        $this->onlyPost();
99
100
        //verify input values (html special chars ?)
101
        $to = $this->config["admin_email_address"];
102
        $message = $this->request->getDataFull();
103
104
        //Error checking
105
106
        //check all the fields
107
        $error = false;
108
        $contactErrors = new \stdClass();
109
110
        if ($message["contactName"] == "") {
111
            $error = true;
112
            $contactErrors->contactName = "Name must not be empty";
113
        }
114
        if ($message["contactEmail"] == "") {
115
            $error = true;
116
            $contactErrors->contactEmail = "Email must not be empty";
117
        }
118
        if ($message["contactSubject"] == "") {
119
            $error = true;
120
            $contactErrors->contactSubject = "Subject must not be empty";
121
        }
122
        if ($message["contactMessage"] == "") {
123
            $error = true;
124
            $contactErrors->contactMessage = "Message must not be empty";
125
        }
126
        if (!$this->isEmail($message["contactEmail"])) {
127
            $error = true;
128
            $contactErrors->contactEmail = "email is not valid";
129
        }
130
131
        //If we found an error, return data to the register form and no create
132
        if ($error) {
133
            $this->session->set("contactInfo", $message);
134
            $this->session->set("contactErrors", $contactErrors);
135
            $this->response->redirect("/home/contact");
136
        }
137
138
        $config = $this->siteConfig->getSiteConfig();
139
140
        //from here all is good, send mail
141
        $userName = htmlspecialchars($message["contactName"]);
142
        $subject = "Contact from ".$config["site_name"]." : ";
143
        $subject .= htmlspecialchars($message["contactSubject"]);
144
        $textMessage = "<h1>message sent by ".$userName."</h1>";
145
        $textMessage .= htmlspecialchars($message["contactMessage"]);
146
        $from = $message["contactEmail"];
147
148
        $this->sendMail->send($to, $subject, $textMessage, $from);
149
150
        $this->alertBox->setAlert('Email sent');
151
        $this->response->redirect();
152
    }
153
}