5
5
use Doctrine \ORM \EntityManager ;
6
6
use Doctrine \ORM \EntityManagerInterface ;
7
7
use Doctrine \ORM \ORMException ;
8
- use NTI \SyncBundle \Entity \SyncNewItemState ;
9
8
use NTI \SyncBundle \Interfaces \SyncServiceInterface ;
10
- use Symfony \Bundle \FrameworkBundle \ Controller \ Controller ;
9
+ use Sensio \Bundle \FrameworkExtraBundle \ Configuration \ Method ;
11
10
use Sensio \Bundle \FrameworkExtraBundle \Configuration \Route ;
11
+ use Symfony \Bundle \FrameworkBundle \Controller \AbstractController ;
12
12
use Symfony \Component \HttpFoundation \JsonResponse ;
13
13
use Symfony \Component \HttpFoundation \Request ;
14
- use Sensio \Bundle \FrameworkExtraBundle \Configuration \Method ;
15
- use Symfony \Component \Serializer \Serializer ;
16
14
17
15
/**
18
- * Class SyncController
19
- * @package NTI\SyncBundle\Controller
16
+ * Class SyncController.
20
17
*/
21
- class SyncController extends Controller
18
+ class SyncController extends AbstractController
22
19
{
23
20
/**
24
- * @param Request $request
25
21
* @return JsonResponse
26
22
* @Route("/summary", name="nti_sync_get_summary")
27
23
*/
28
- public function getChangesSummaryAction (Request $ request ) {
29
-
24
+ public function getChangesSummaryAction (Request $ request )
25
+ {
30
26
$ em = $ this ->getDoctrine ()->getManager ();
31
27
32
- $ syncStates = $ em ->getRepository ('NTISyncBundle:SyncState ' )->findBy (array (), array ( " mapping " => " asc " ) );
33
- $ syncStatesArray = json_decode ($ this ->get (" jms_serializer " )->serialize ($ syncStates , 'json ' ), true );
28
+ $ syncStates = $ em ->getRepository ('NTISyncBundle:SyncState ' )->findBy ([], [ ' mapping ' => ' asc ' ] );
29
+ $ syncStatesArray = json_decode ($ this ->get (' jms_serializer ' )->serialize ($ syncStates , 'json ' ), true );
34
30
35
31
return new JsonResponse ($ syncStatesArray , 200 );
36
32
}
37
33
38
34
/**
39
- * @param Request $request
40
35
* @return JsonResponse
41
36
* @Route("/pull", name="nti_sync_pull")
42
37
* @Method("GET|POST")
43
38
*/
44
- public function pullAction (Request $ request ) {
39
+ public function pullAction (Request $ request )
40
+ {
41
+ $ mappings = [];
45
42
46
- $ mappings = array ();
47
-
48
- if ($ request ->getMethod () == "GET " ) {
49
- $ mappings = ($ request ->get ('mappings ' ) && is_array ($ request ->get ('mappings ' ))) ? $ request ->get ('mappings ' ) : array ();
50
- } elseif ($ request ->getMethod () == "POST " ) {
43
+ if ('GET ' == $ request ->getMethod ()) {
44
+ $ mappings = ($ request ->get ('mappings ' ) && is_array ($ request ->get ('mappings ' ))) ? $ request ->get ('mappings ' ) : [];
45
+ } elseif ('POST ' == $ request ->getMethod ()) {
51
46
$ data = json_decode ($ request ->getContent (), true );
52
- $ mappings = (isset ($ data [" mappings " ])) ? $ data [" mappings " ] : array () ;
47
+ $ mappings = (isset ($ data [' mappings ' ])) ? $ data [' mappings ' ] : [] ;
53
48
}
54
49
55
50
$ resultData = $ this ->get ('nti.sync ' )->getFromMappings ($ mappings );
@@ -60,24 +55,22 @@ public function pullAction(Request $request) {
60
55
}
61
56
62
57
/**
63
- * @param Request $request
64
58
* @return JsonResponse
65
59
* @Route("/push", name="nti_sync_push", methods="POST")
66
60
* @Method("POST")
67
61
*/
68
- public function pushAction (Request $ request ) {
69
-
62
+ public function pushAction (Request $ request )
63
+ {
70
64
$ data = json_decode ($ request ->getContent (), true );
71
65
72
- $ mappings = (isset ($ data [" mappings " ])) ? $ data [" mappings " ] : array () ;
66
+ $ mappings = (isset ($ data [' mappings ' ])) ? $ data [' mappings ' ] : [] ;
73
67
74
68
/** @var EntityManagerInterface $em */
75
69
$ em = $ this ->getDoctrine ()->getManager ();
76
70
77
- $ results = array ();
78
-
79
- foreach ($ mappings as $ entry ) {
71
+ $ results = [];
80
72
73
+ foreach ($ mappings as $ entry ) {
81
74
if (!$ em ->isOpen ()) {
82
75
try {
83
76
$ em = EntityManager::create (
@@ -86,17 +79,21 @@ public function pushAction(Request $request) {
86
79
$ em ->getEventManager ()
87
80
);
88
81
} catch (ORMException $ e ) {
89
- return new JsonResponse (array ( " error " => " An unknown error occurred while reopening the database connection. " ) , 500 );
82
+ return new JsonResponse ([ ' error ' => ' An unknown error occurred while reopening the database connection. ' ] , 500 );
90
83
}
91
84
}
92
85
93
- if (!isset ($ entry ["mapping " ]) || !isset ($ entry ["data " ])) { continue ; }
86
+ if (!isset ($ entry ['mapping ' ]) || !isset ($ entry ['data ' ])) {
87
+ continue ;
88
+ }
94
89
95
- $ mappingName = $ entry [" mapping " ];
90
+ $ mappingName = $ entry [' mapping ' ];
96
91
97
- $ mapping = $ em ->getRepository ('NTISyncBundle:SyncMapping ' )->findOneBy (array ( " name " => $ mappingName) );
92
+ $ mapping = $ em ->getRepository ('NTISyncBundle:SyncMapping ' )->findOneBy ([ ' name ' => $ mappingName] );
98
93
99
- if (!$ mapping ) { continue ; }
94
+ if (!$ mapping ) {
95
+ continue ;
96
+ }
100
97
101
98
$ syncClass = $ mapping ->getSyncService ();
102
99
@@ -106,28 +103,26 @@ public function pushAction(Request $request) {
106
103
$ em ->beginTransaction ();
107
104
108
105
try {
109
- $ result = $ service ->sync ($ entry [" data " ], $ em , $ mapping );
106
+ $ result = $ service ->sync ($ entry [' data ' ], $ em , $ mapping );
110
107
} catch (\Exception $ ex ) {
111
-
112
-
113
- $ additionalErrors = array ();
108
+ $ additionalErrors = [];
114
109
115
110
try {
116
111
$ additionalErrors = $ service ->onSyncException ($ ex , $ this ->container );
117
- $ additionalErrors = (is_array ($ additionalErrors )) ? $ additionalErrors : array () ;
112
+ $ additionalErrors = (is_array ($ additionalErrors )) ? $ additionalErrors : [] ;
118
113
} catch (\Exception $ ex ) {
119
-
120
114
// TBD
121
115
}
122
116
123
- $ result = array (
124
- " error " => " An unknown error occurred while processing the synchronization for this mapping " ,
125
- " additional_errors " => $ additionalErrors ,
126
- ) ;
117
+ $ result = [
118
+ ' error ' => ' An unknown error occurred while processing the synchronization for this mapping ' ,
119
+ ' additional_errors ' => $ additionalErrors ,
120
+ ] ;
127
121
128
122
$ results [$ mappingName ] = $ result ;
129
123
130
124
$ em ->clear ();
125
+
131
126
continue ;
132
127
}
133
128
@@ -138,28 +133,26 @@ public function pushAction(Request $request) {
138
133
} catch (\Exception $ ex ) {
139
134
$ em ->rollback ();
140
135
141
- $ additionalErrors = array ();
136
+ $ additionalErrors = [];
137
+
142
138
try {
143
139
$ additionalErrors = $ service ->onSyncException ($ ex , $ this ->container );
144
- $ additionalErrors = (is_array ($ additionalErrors )) ? $ additionalErrors : array () ;
140
+ $ additionalErrors = (is_array ($ additionalErrors )) ? $ additionalErrors : [] ;
145
141
} catch (\Exception $ ex ) {
146
142
// TBD
147
143
}
148
144
149
- $ result = array (
150
- " error " => " An unknown error occurred while processing the synchronization for this mapping " ,
151
- " additional_errors " => $ additionalErrors ,
152
- ) ;
145
+ $ result = [
146
+ ' error ' => ' An unknown error occurred while processing the synchronization for this mapping ' ,
147
+ ' additional_errors ' => $ additionalErrors ,
148
+ ] ;
153
149
154
150
$ results [$ mappingName ] = $ result ;
155
151
}
156
-
157
152
}
158
153
159
- return new JsonResponse (array (
160
- "mappings " => $ results
161
- ));
162
-
154
+ return new JsonResponse ([
155
+ 'mappings ' => $ results ,
156
+ ]);
163
157
}
164
-
165
158
}
0 commit comments