-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubscriberModeViewController.php
More file actions
172 lines (154 loc) · 5.16 KB
/
SubscriberModeViewController.php
File metadata and controls
172 lines (154 loc) · 5.16 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/*
* Copyright (c) 2021.
* Marc Concepcion
* marcanthonyconcepcion@gmail.com
*/
const SQLDuplicateKeyErrorCode = 23000;
use JetBrains\PhpStorm\ArrayShape;
require_once 'ModelViewController.php';
require_once 'DatabaseRecords.php';
require_once 'PDODatabaseRecords.php';
class SubscriberModel implements Model
{
private DatabaseRecords $records;
public function __construct(DatabaseRecords $records)
{
$this->records = $records;
}
function create(StdClass $model)
{
$this->records->edit('insert into `subscribers` (`email_address`, `last_name`, `first_name`) '
.'values (?, ?, ?)', [$model->email_address, $model->last_name, $model->first_name]);
}
function retrieve(int $id)
{
return $this->records->fetch('select * from `subscribers` where `index`= :index',[':index'=>$id])->current();
}
function list() : Generator
{
return $this->records->fetch('select * from `subscribers`');
}
function update(int $id, StdClass $model)
{
$parameters = [];
$variables = [];
foreach($model as $variable=>$value)
{
array_push($variables,'`'.$variable.'`= :'.$variable);
$parameters[':'.$variable.''] = $value;
}
$parameters[':index'] = $id;
$query = 'update `subscribers` set '.implode ( ', ', $variables).' where `index`= :index';
$this->records->edit($query, $parameters);
}
function delete(int $id)
{
$this->records->edit('delete from `subscribers` where `index`= :index', [':index'=>$id]);
}
function checkExistence(int $id) : bool
{
return 0 < current($this->records->fetch(
'select count(*) from `subscribers` where `index`= :index',[':index'=>$id])->current());
}
}
class SubscriberController extends Controller
{
function __construct(SubscriberModel $model)
{
$this->register($model, 'subscribers');
}
/**
* @param int|null $id
* @return object
* @throws HTTPNotFoundError
*/
function get(?int $id = null): object
{
$model = [];
if (is_null($id))
{
$records = $this->models['subscribers']->list();
foreach($records as $record)
{
array_push($model, $record);
}
if (0 === count($model))
{
return (object)['status'=>(object)HTTP_NO_CONTENT];
}
}
else
{
$model = $this->models['subscribers']->retrieve($id);
if (false === $this->models['subscribers']->checkExistence($id))
{
throw new HTTPNotFoundError('Subscriber does not exist.');
}
}
return (object)['status'=>(object)HTTP_OK, 'body'=>json_encode($model)];
}
/**
* @param string $json_parameters
* @return object
* @throws HTTPConflictError
*/
#[ArrayShape(['status_header' => "string", 'status_code' => "int", 'body' => "mixed"])]
function post(string $json_parameters): object
{
try
{
$model = json_decode($json_parameters);
$this->models['subscribers']->create($model);
return (object)['status' => (object)HTTP_CREATED, 'body' => json_decode(json_encode(
'{"success": "Record created.", "subscriber":' . json_encode((array)$model) . '}'))];
}
catch (Exception $error)
{
if(SQLDuplicateKeyErrorCode === $error->getCode())
{
throw new HTTPConflictError('Posting/creating an already existing record. '
.'Please put/update an existing record or post/create a totally new record.');
}
else
{
throw $error;
}
}
}
/**
* @param int $id
* @param string $json_parameters
* @return object
* @throws HTTPNotFoundError
*/
#[ArrayShape(['status_header' => "string", 'status_code' => "int", 'body' => "mixed"])]
function put(int $id, string $json_parameters): object
{
if (false === $this->models['subscribers']->checkExistence($id))
{
throw new HTTPNotFoundError('Subscriber does not exist.');
}
$model = json_decode($json_parameters);
$this->models['subscribers']->update($id, $model);
return (object)['status'=>(object)HTTP_OK, 'body'=> json_decode(json_encode(
'{"success": "Record of subscriber # '.$id.' updated.", "updates":'
.json_encode((array)$model).'}'))];
}
/**
* @param int $id
* @return object
* @throws HTTPNotFoundError
*/
#[ArrayShape(['status_header' => "string", 'status_code' => "int", 'body' => "mixed"])]
function delete(int $id): object
{
if (false === $this->models['subscribers']->checkExistence($id))
{
throw new HTTPNotFoundError('Subscriber does not exist.');
}
$this->models['subscribers']->delete($id);
return (object)['status'=>(object)HTTP_OK, 'body'=> json_decode(json_encode(
'{"success": "Record of subscriber # '.$id.' deleted."}'))];
}
}