-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonController.php
More file actions
154 lines (135 loc) · 3.55 KB
/
JsonController.php
File metadata and controls
154 lines (135 loc) · 3.55 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
<?php
namespace SyncEngine\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use SyncEngine\Model\ConnectionModel;
use SyncEngine\Service\Preferences;
use SyncEngine\Service\Locator\Blueprints;
use SyncEngine\Service\Locator\Codecs;
use SyncEngine\Service\Locator\Columns;
use SyncEngine\Service\Locator\Tasks;
use SyncEngine\Service\Locator\Webservices;
class JsonController extends DefaultController
{
#[Route( '/json/preferences', name: 'json_preferences' )]
public function handlePreferences( Request $request, Preferences $preferences ): JsonResponse
{
if ( ! $preferences->exists() ) {
return $this->json(
[
'success' => false,
]
);
}
$action = $request->get( 'action' );
if ( 'update' === $action ) {
$setting = $request->get( 'setting' );
$value = $request->get( 'value' );
$preferences->update( $setting, $value );
}
return $this->json(
[
'success' => true,
'data' => $preferences->fetch(),
]
);
}
#[Route( '/json/blueprints', name: 'json_blueprints' )]
public function getBlueprints( Blueprints $blueprintsService ): JsonResponse
{
return $this->json(
[
'success' => true,
'data' => $blueprintsService->getNormalized(),
]
);
}
#[Route( '/json/codecs', name: 'json_codecs' )]
public function getCodecs( Codecs $codecsService ): JsonResponse
{
return $this->json(
[
'success' => true,
'data' => $codecsService->getNormalized(),
]
);
}
#[Route( '/json/columns', name: 'json_columns' )]
public function getColumns( Columns $columnsService ): JsonResponse
{
return $this->json(
[
'success' => true,
'data' => $columnsService->getNormalized(),
]
);
}
#[Route( '/json/tasks', name: 'json_tasks' )]
public function getTasks( Tasks $tasksService ): JsonResponse
{
return $this->json(
[
'success' => true,
'data' => $tasksService->getNormalized(),
]
);
}
#[Route( '/json/webservices', name: 'json_webservices' )]
public function getWebservices( Webservices $webservicesService ): JsonResponse
{
return $this->json(
[
'success' => true,
'data' => $webservicesService->getNormalized(),
]
);
}
#[Route( '/json/webservice/retrieve', name: 'json_webservice_retrieve' )]
public function handleWebservice( Request $request ): JsonResponse
{
$id = $request->get( 'id' );
if ( ! $id ) {
$id = $request->get( 'connection' );
if ( ! $id ) {
$config = $request->get( 'config' );
if ( is_string( $config ) ) {
$config = json_decode( $config, true );
}
$id = $config['id'] ?? $config['connection'] ?? null;
}
}
if ( ! $id ) {
return $this->json(
[
'success' => false,
'error' => $this->trans( 'Connection required' ),
],
Response::HTTP_BAD_REQUEST
);
}
$connection = ConnectionModel::get( $id );
if ( ! $connection ) {
return $this->json(
[
'success' => false,
'error' => $this->trans( 'Connection not found' ),
],
Response::HTTP_NOT_FOUND
);
}
$webservice = $connection->getWebservice();
if ( ! $webservice ) {
return $this->json(
[
'success' => false,
'error' => $this->trans( 'Webservice not found' ),
],
Response::HTTP_NOT_FOUND
);
}
$response = $webservice->handleRequest( $request, $connection );
return $response instanceof JsonResponse ? $response : new JsonResponse( $response->getContent(), $response->getStatusCode() );
}
}