Passed
Push — Auth ( c6ffd2...76831f )
by Stone
02:02
created

Container::getCookie()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Core;
4
5
use Core\Dependency\Cookie;
6
use Core\Dependency\Request;
7
use Core\Dependency\Response;
8
use Core\Dependency\Session;
9
use PDO;
10
11
12
/**
13
 * Class Container for dependency injection
14
 * we take care of setting our template and database connections
15
 * We also call our Request and Session objects for the SuperGlobals access
16
 * @package Core
17
 *
18
 * PHP version 7
19
 */
20
class Container
21
{
22
23
    //used for the model connection
24
    /**
25
     * @var PDO this is to store the pdo connection. We only need to set once
26
     */
27
    private $dbh = null;
28
29
    /**
30
     * @var Dependency\Request object
31
     */
32
    private $request;
33
34
    /**
35
     * @var Dependency\Session object
36
     */
37
    private $session;
38
39
40
    private $response;
41
42
    private $cookie;
43
44
    /**
45
     * gets the twig template environment
46
     * @return \Twig_Environment
47
     */
48
    public function getTemplate(): \Twig_Environment
49
    {
50
        $twigOptions = [];
51
        if (!Config::DEV_ENVIRONMENT) {
52
            $twigOptions = [
53
                'cache' => dirname(__DIR__) . '/Cache'
54
            ];
55
        }
56
        $loader = new \Twig_Loader_Filesystem(dirname(__DIR__) . '/App/Views');
57
        $twig = new \Twig_Environment($loader, $twigOptions);
58
59
        return $twig;
60
    }
61
62
    /**
63
     * create the database connection via PDO
64
     * @return PDO
65
     */
66
    public function setPdo(): \PDO
67
    {
68
        if ($this->dbh) {
69
            return $this->dbh;
70
        }
71
        $dsn = "mysql:host=" . Config::DB_HOST . ";dbname=" . Config::DB_NAME . ";charset=utf8"; //Creating the Data Source name
72
        $opt = [
73
            PDO::ATTR_PERSISTENT => true,
74
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
75
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ
76
        ];
77
        $this->dbh = new PDO($dsn, Config::DB_USER, Config::DB_PASSWORD, $opt);;
78
        return $this->dbh;
79
    }
80
81
82
    /**
83
     * Creates the request object if not already present and returns it
84
     * @return Dependency\Request|Request
85
     */
86
    public function getRequest(): Dependency\Request
87
    {
88
        if (!$this->request) {
89
            $this->request = new Request();
90
        }
91
        return $this->request;
92
    }
93
94
    /**
95
     * Creates the response object if not already present and returns it
96
     * @return Response
97
     */
98
    public function getResponse(): Dependency\Response
99
    {
100
        if (!$this->response) {
101
            $this->response = new Response();
102
        }
103
        return $this->response;
104
    }
105
106
    /**
107
     * Creates the session object if not already present and returns it
108
     * @return Dependency\Session|session
109
     */
110
    public function getSession(): Dependency\Session
111
    {
112
        if (!$this->session) {
113
            $this->session = new Session();
114
        }
115
        return $this->session;
116
    }
117
118
    /**
119
     * @return Cookie
120
     */
121
    public function getCookie(): Dependency\Cookie
122
    {
123
        if(!$this->cookie){
124
            $this->cookie = new Cookie();
125
        }
126
        return $this->cookie;
127
    }
128
129
}