/
UserApplication.php
98 lines (72 loc) · 2.52 KB
/
UserApplication.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
namespace My\Application;
use My\Service\UserService;
use My\Service\ValidatorService;
session_start();
/* include the APIs */
require_once "Dao/BaseDao.php";
require_once "Dao/UserDao.php";
require_once "Service/ValidatorService.php";
require_once "Service/UserService.php";
/**
* UserApplication - This application class serves as end-user application.
* User interacts with this application via user interface and
* this class corresponds with service classes.
* This application will serve & process user login, registration, profile update etc.
*/
class UserApplication {
public function __construct(UserService $userService, ValidatorService $validator) {
$this->userService = $userService;
$this->validator = $validator;
if (isset($_POST['login'])) {
$this->login();
}
else if (isset($_POST['register'])) {
$this->register();
}
else if (isset($_POST['update'])) {
$this->update();
}
else if ( isset($_GET['logout']) ) {
$this->logout();
}
}
public function login() {
$success = $this->userService->login($_POST);
if ($success) {
$_SESSION['statusMsg'] = "Successful login!";
} else {
$_SESSION['value_array'] = $_POST;
$_SESSION['error_array'] = $this->validator->getErrorArray();
}
header("Location: index.php");
}
public function register() {
$success = $this->userService->register($_POST);
if ($success) {
$_SESSION['statusMsg'] = "Registration was successful!";
header("Location: index.php");
} else {
$_SESSION['value_array'] = $_POST;
$_SESSION['error_array'] = $this->validator->getErrorArray();
header("Location: register.php");
}
}
public function update() {
$success = $this->userService->update($_POST);
if ($success) {
$_SESSION['statusMsg'] = "Successfully Updated!";
header("Location: profile.php");
} else {
$_SESSION['value_array'] = $_POST;
$_SESSION['error_array'] = $this->validator->getErrorArray();
header("Location: profile-edit.php");
}
}
public function logout(){
$success = $this->userService->logout();
header("Location: index.php");
}
}
$userApp = new \My\Application\UserApplication($userService, $validator);
?>