Skip to content

Commit cada52e

Browse files
committed
Fix up MLP Endpoint for Upload and Install handling
1 parent ec852a0 commit cada52e

File tree

2 files changed

+87
-5
lines changed

2 files changed

+87
-5
lines changed

src/Endpoint/Abstracts/AbstractSugarBeanEndpoint.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -543,12 +543,11 @@ protected function configureFileUploadQueryParams(): array
543543
];
544544

545545
if ($this->_deleteFileOnFail) {
546-
$Client = $this->getClient();
547-
if ($Client) {
548-
$data['platform'] = $Client->getPlatform();
549-
$token = $Client->getAuth()->getTokenProp('access_token');
546+
if (!empty($this->_client)) {
547+
$data['platform'] = $this->getClient()->getPlatform();
548+
$token = $this->getClient()->getAuth()->getTokenProp('access_token');
550549
if ($token) {
551-
$data['oauth_token'] = $Client->getAuth()->getTokenProp('access_token');
550+
$data['oauth_token'] = $this->getClient()->getAuth()->getTokenProp('access_token');
552551
}
553552
}
554553
}

src/Endpoint/MLPackage.php

+83
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Sugarcrm\REST\Endpoint;
44

5+
use GuzzleHttp\Psr7\Response;
6+
use MRussell\REST\Endpoint\ModelEndpoint;
7+
58
class MLPackage extends SugarBean
69
{
710
public const ACTION_INSTALL = 'install';
@@ -14,6 +17,8 @@ class MLPackage extends SugarBean
1417

1518
public const ACTION_INSTALL_STATUS = 'installation-status';
1619

20+
public const MLP_FIELD_PROP = 'upgrade_zip';
21+
1722
protected static array $_DEFAULT_PROPERTIES = [
1823
self::PROPERTY_URL => 'Administration/packages/$id/$:action',
1924
self::PROPERTY_AUTH => true,
@@ -25,8 +30,86 @@ class MLPackage extends SugarBean
2530
self::ACTION_ENABLE => 'GET',
2631
self::ACTION_DISABLE => 'GET',
2732
self::ACTION_INSTALL_STATUS => 'GET',
33+
self::BEAN_ACTION_ATTACH_FILE => 'POST'
2834
];
2935

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+
30113
/**
31114
* Setup the query params passed during File Uploads
32115
* @codeCoverageIgnore

0 commit comments

Comments
 (0)