Passed
Push — master ( f437aa...c74702 )
by Sam
04:15
created

Base::getAnyParam()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
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
     * Get either a body or a query parameter.
79
     * @param ServerRequestInterface $request
80
     * @param string $param The name of the parameter to get.
81
     * @param mixed $default
82
     * @return mixed
83
     */
84
    public function getAnyParam(ServerRequestInterface $request, $param, $default = null)
85
    {
86
        $bodyParam = $this->getBodyParam($request, $param, $default);
87
        if ($bodyParam !== null) {
88
            return $bodyParam;
89
        }
90
        return $this->getQueryParam($request, $param, $default);
91
    }
92
93
    /**
94
     * Send an email message.
95
     *
96
     * @param Swift_Message $message
97
     */
98
    public function email(Swift_Message $message)
99
    {
100
        $config = $this->config->mail();
101
        $transport_classname = '\\Swift_' . ucfirst($config['transport']) . 'Transport';
102
        $transport = $transport_classname::newInstance();
103
        $mailer = Swift_Mailer::newInstance($transport);
104
        $mailer->send($message);
105
    }
106
}
107