Completed
Push — master ( 84c5f3...696e93 )
by Mauro
02:28
created

TaskController::createTask()   A

Complexity

Conditions 2
Paths 5

Size

Total Lines 12
Code Lines 8

Duplication

Lines 12
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 12
loc 12
rs 9.4285
cc 2
eloc 8
nc 5
nop 3
1
<?php
2
3
namespace App\Controller\Task;
4
5
use App\Controller\BaseController;
6
7
/**
8
 * Tasks Controller.
9
 */
10 View Code Duplication
class TaskController extends BaseController
11
{
12
    /**
13
     * @param \Slim\Container $container
14
     */
15
    public function __construct(\Slim\Container $container)
16
    {
17
        $this->logger = $container->get('logger');
18
        $this->database = $container->get('db');
19
    }
20
21
    /**
22
     * Get all tasks.
23
     *
24
     * @param Request $request
25
     * @param Response $response
26
     * @param array $args
27
     * @return array
28
     */
29
    public function getTasks($request, $response, $args)
30
    {
31
        $this->setParams($request, $response, $args);
32
        $result = $this->getTaskService()->getTasks();
33
34
        return $this->jsonResponse('success', $result, 200);
35
    }
36
37
    /**
38
     * Get one task by id.
39
     *
40
     * @param Request $request
41
     * @param Response $response
42
     * @param array $args
43
     * @return array
44
     */
45
    public function getTask($request, $response, $args)
46
    {
47
        try {
48
            $this->setParams($request, $response, $args);
49
            $result = $this->getTaskService()->getTask($this->args['id']);
50
51
            return $this->jsonResponse('success', $result, 200);
52
        } catch (\Exception $ex) {
53
            return $this->jsonResponse('error', $ex->getMessage(), $ex->getCode());
54
        }
55
    }
56
57
    /**
58
     * Search tasks by name.
59
     *
60
     * @param Request $request
61
     * @param Response $response
62
     * @param array $args
63
     * @return array
64
     */
65
    public function searchTasks($request, $response, $args)
66
    {
67
        try {
68
            $this->setParams($request, $response, $args);
69
            $result = $this->getTaskService()->searchTasks($this->args['query']);
70
71
            return $this->jsonResponse('success', $result, 200);
72
        } catch (\Exception $ex) {
73
            return $this->jsonResponse('error', $ex->getMessage(), $ex->getCode());
74
        }
75
    }
76
77
    /**
78
     * Create a task.
79
     *
80
     * @param Request $request
81
     * @param Response $response
82
     * @param array $args
83
     * @return array
84
     */
85
    public function createTask($request, $response, $args)
86
    {
87
        try {
88
            $this->setParams($request, $response, $args);
89
            $input = $this->request->getParsedBody();
90
            $result = $this->getTaskService()->createTask($input);
91
92
            return $this->jsonResponse('success', $result, 201);
93
        } catch (\Exception $ex) {
94
            return $this->jsonResponse('error', $ex->getMessage(), $ex->getCode());
95
        }
96
    }
97
98
    /**
99
     * Update a task.
100
     *
101
     * @param Request $request
102
     * @param Response $response
103
     * @param array $args
104
     * @return array
105
     */
106
    public function updateTask($request, $response, $args)
107
    {
108
        try {
109
            $this->setParams($request, $response, $args);
110
            $input = $this->request->getParsedBody();
111
            $result = $this->getTaskService()->updateTask($input, $this->args['id']);
112
113
            return $this->jsonResponse('success', $result, 200);
114
        } catch (\Exception $ex) {
115
            return $this->jsonResponse('error', $ex->getMessage(), $ex->getCode());
116
        }
117
    }
118
119
    /**
120
     * Delete a task.
121
     *
122
     * @param Request $request
123
     * @param Response $response
124
     * @param array $args
125
     * @return array
126
     */
127
    public function deleteTask($request, $response, $args)
128
    {
129
        try {
130
            $this->setParams($request, $response, $args);
131
            $result = $this->getTaskService()->deleteTask($this->args['id']);
132
133
            return $this->jsonResponse('success', $result, 200);
134
        } catch (\Exception $ex) {
135
            return $this->jsonResponse('error', $ex->getMessage(), $ex->getCode());
136
        }
137
    }
138
}
139