Passed
Push — master ( daaf91...f437aa )
by Sam
05:31
created

Base::getBodyParam()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
cc 2
eloc 3
nc 2
nop 3
crap 6
1
<?php
2
3
namespace App\Controllers;
4
5
use App\Config;
6
use App\User;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Swift_Mailer;
9
use Swift_Message;
10
11
abstract class Base
12
{
13
14
    /**
15
     * The site configuration.
16
     *
17
     * @var \App\Config
18
     */
19
    protected $config;
20
21
    /**
22
     * The database.
23
     *
24
     * @var \App\Db
25
     */
26
    protected $db;
27
28
    /**
29
     * The current user.
30
     *
31
     * @var \App\User
32
     */
33
    protected $user;
34
35
    /**
36
     * Every controller gets the configuration, database, and a user. If the user has a session
37
     * in progress, the user is loaded from that.
38
     */
39
    public function __construct()
0 ignored issues
show
Coding Style introduced by
__construct uses the super-global variable $_SESSION 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...
40
    {
41
        $this->config = new Config();
42
        $this->db = new \App\Db();
43
44
        // User.
45
        $this->user = new User($this->db);
46
        if (isset($_SESSION['userid'])) {
47
            $this->user->load($_SESSION['userid']);
48
        }
49
    }
50
51
    /**
52
     * Get a parameter value out of a request's body.
53
     * @param ServerRequestInterface $request
54
     * @param string $param The name of the parameter to get.
55
     * @param mixed $default
56
     * @return mixed
57
     */
58
    protected function getBodyParam(ServerRequestInterface $request, $param, $default = null)
59
    {
60
        $params = $request->getParsedBody();
61
        return (isset($params[$param])) ? $params[$param] : $default;
62
    }
63
64
    /**
65
     * Get a parameter from the query string of this request.
66
     * @param ServerRequestInterface $request
67
     * @param string $param The name of the parameter to get.
68
     * @param mixed $default
69
     * @return mixed
70
     */
71
    protected function getQueryParam(ServerRequestInterface $request, $param, $default = null)
72
    {
73
        $params = $request->getQueryParams();
74
        return (isset($params[$param])) ? $params[$param] : $default;
75
    }
76
77
    /**
78
     * Send an email message.
79
     *
80
     * @param Swift_Message $message
81
     */
82
    public function email(Swift_Message $message)
83
    {
84
        $config = $this->config->mail();
85
        $transport_classname = '\\Swift_' . ucfirst($config['transport']) . 'Transport';
86
        $transport = $transport_classname::newInstance();
87
        $mailer = Swift_Mailer::newInstance($transport);
88
        $mailer->send($message);
89
    }
90
}
91