2
2
3
3
namespace Sugarcrm \REST \Endpoint ;
4
4
5
+ use GuzzleHttp \Psr7 \Response ;
6
+ use MRussell \REST \Endpoint \ModelEndpoint ;
7
+
5
8
class MLPackage extends SugarBean
6
9
{
7
10
public const ACTION_INSTALL = 'install ' ;
@@ -14,6 +17,8 @@ class MLPackage extends SugarBean
14
17
15
18
public const ACTION_INSTALL_STATUS = 'installation-status ' ;
16
19
20
+ public const MLP_FIELD_PROP = 'upgrade_zip ' ;
21
+
17
22
protected static array $ _DEFAULT_PROPERTIES = [
18
23
self ::PROPERTY_URL => 'Administration/packages/$id/$:action ' ,
19
24
self ::PROPERTY_AUTH => true ,
@@ -25,8 +30,86 @@ class MLPackage extends SugarBean
25
30
self ::ACTION_ENABLE => 'GET ' ,
26
31
self ::ACTION_DISABLE => 'GET ' ,
27
32
self ::ACTION_INSTALL_STATUS => 'GET ' ,
33
+ self ::BEAN_ACTION_ATTACH_FILE => 'POST '
28
34
];
29
35
36
+ protected bool $ _installing = false ;
37
+
38
+ protected array $ _installOutput = [];
39
+
40
+ public function setUrlArgs (array $ args ): static
41
+ {
42
+ if (isset ($ args [0 ])) {
43
+ $ this ->set ($ this ->getKeyProperty (), $ args [0 ]);
44
+ unset($ args [0 ]);
45
+ }
46
+ return ModelEndpoint::setUrlArgs ($ args );
47
+ }
48
+
49
+ public function install (array $ options = [], bool $ async = false ): static
50
+ {
51
+ $ this ->_installing = true ;
52
+ $ this ->setCurrentAction (self ::ACTION_INSTALL );
53
+ if ($ async ) {
54
+ return $ this ->asyncExecute ($ options );
55
+ } else {
56
+ return $ this ->execute ($ options );
57
+ }
58
+ }
59
+
60
+ public function isInstalling (): bool
61
+ {
62
+ return $ this ->_installing ;
63
+ }
64
+
65
+ public function checkInstallStatus (): array
66
+ {
67
+ $ this ->setCurrentAction (self ::ACTION_INSTALL_STATUS );
68
+ $ this ->execute ();
69
+ return $ this ->_installOutput ;
70
+ }
71
+
72
+ public function upload (string $ filePath ): static
73
+ {
74
+ $ this ->setCurrentAction (self ::MODEL_ACTION_CREATE );
75
+ $ this ->_upload = true ;
76
+ $ this ->setFile (self ::MLP_FIELD_PROP , $ filePath );
77
+ return $ this ->execute ();
78
+ }
79
+
80
+ protected function configurePayload (): mixed
81
+ {
82
+ $ data = $ this ->getData ();
83
+ //If someone set field of ZIP, instead of using upload Method
84
+ if (isset ($ data [self ::MLP_FIELD_PROP ]) && !$ this ->_upload && $ this ->getCurrentAction () !== self ::MODEL_ACTION_CREATE ) {
85
+ $ this ->setFile (self ::MLP_FIELD_PROP , $ data [self ::MLP_FIELD_PROP ]);
86
+ $ this ->_upload = true ;
87
+ }
88
+ return parent ::configurePayload ();
89
+ }
90
+
91
+ protected function parseResponse (Response $ response ): void
92
+ {
93
+ parent ::parseResponse ($ response );
94
+ if ($ response ->getStatusCode () == 200 ) {
95
+ $ data = $ this ->getResponseBody ();
96
+ switch ($ this ->getCurrentAction ()) {
97
+ case self ::ACTION_INSTALL :
98
+ case self ::ACTION_UNINSTALL :
99
+ $ this ->_installing = false ;
100
+ break ;
101
+ case self ::ACTION_INSTALL_STATUS :
102
+ if (!empty ($ data ['message ' ])){
103
+ $ this ->_installOutput = $ data ['message ' ] ?? [];
104
+ }
105
+ break ;
106
+ }
107
+ if (($ data ['status ' ] ?? "" ) == 'installed ' ) {
108
+ $ this ->_installing = false ;
109
+ }
110
+ }
111
+ }
112
+
30
113
/**
31
114
* Setup the query params passed during File Uploads
32
115
* @codeCoverageIgnore
0 commit comments