summaryrefslogtreecommitdiff
path: root/public/store/index.php
blob: c4d24fa5ff3e3eb48cfed36b207bfb6a99b9293b (plain)
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
<?php

declare(strict_types=1);

use DTS\Todo;
use DTS\TodoRepository;
use DTS\Old;
use DTS\Session;
use DTS\Validator;

use function DTS\Functions\redirectAndExit;
use function DTS\Functions\respondAndExit;

require_once(__DIR__.'/../../autoload.php');

$config = require_once(__DIR__.'/../../config.php');

$session = Session::getInstance();

if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') !== 'POST') {
    respondAndExit(405, 'Method Not Allowed');
}

$old = new Old($_REQUEST);

$session->set('old', $old);

$validator = new Validator($_REQUEST);

if ($validator->errors->count()) {
    $session->set('errors', $validator->errors);

    redirectAndExit('/create');
}

$validated = $validator->validated;

$todos = new TodoRepository($config['path_to_repository']);

$todo = new Todo();

$todo->task = $validated->task;
$todo->tag = $validated->tag;
$todo->addedAt = date('Y-m-d H:i:s');

if (!$todos->add($todo)) {
    respondAndExit(500, 'Internal Server Error');
}

$session->set('message', 'Todo Added');

redirectAndExit('/');