Skip to content

Commit

Permalink
Process an additional parameter on core update
Browse files Browse the repository at this point in the history
  • Loading branch information
VicDeo committed Oct 23, 2018
1 parent aa06b27 commit b9b901e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
10 changes: 9 additions & 1 deletion lib/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@ public function __construct(array $urlParams = array()) {
IRepairStep::class . '::upgradeAppStoreApp',
function ($event) use ($listener) {
if ($event instanceof GenericEvent) {
$listener->upgradeAppStoreApp($event->getSubject());
try {
$isMajorUpdate = $event->getArgument('isMajorUpdate');
} catch (\InvalidArgumentException $e) {
$isMajorUpdate = false;
}
$listener->upgradeAppStoreApp(
$event->getSubject(),
$isMajorUpdate
);
}
}
);
Expand Down
9 changes: 6 additions & 3 deletions lib/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ public function __construct(MarketService $marketService) {
$this->marketService = $marketService;
}

public function upgradeAppStoreApp($app){
$updateVersion = $this->marketService->getAvailableUpdateVersion($app);
public function upgradeAppStoreApp($app, $isMajorUpdate) {
$updateVersion = $this->marketService->getAvailableUpdateVersion(
$app,
$isMajorUpdate
);
if ($updateVersion !== false) {
$this->marketService->updateApp($app);
$this->marketService->updateApp($app, $isMajorUpdate);
} else {
throw new AppUpdateNotFoundException();
}
Expand Down
35 changes: 28 additions & 7 deletions lib/MarketService.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function installApp($appId, $skipMigrations = false) {
}

// download package
$package = $this->downloadPackage($appId);
$package = $this->downloadPackage($appId, true);
$this->installPackage($package, $skipMigrations);
$this->appManager->enableApp($appId);
}
Expand All @@ -140,15 +140,22 @@ public function readAppPackage($path){
return $this->appManager->readAppPackage($path);
}

private function downloadPackage($appId) {
private function downloadPackage($appId, $isMajor, $currentVersion = '0.0.0.0') {
$this->checkInternetConnection();
$data = $this->getAppInfo($appId);
if (empty($data)) {
throw new AppNotFoundException($this->l10n->t('Unknown app (%s)', $appId));
}

$version = $this->getPlatformVersion();
$release = array_filter($data['releases'], function($element) use ($version) {
$release = array_filter($data['releases'], function($element) use ($version, $isMajor, $currentVersion) {
if ($isMajor === false) {
$marketVersionMajor = $this->getMajorVersion($element['version']);
$currentVersionMajor = $this->getMajorVersion($currentVersion);
if ($marketVersionMajor > $currentVersionMajor) {
return false;
}
}
$platformMin = $element['platformMin'];
$platformMax = $element['platformMax'];
$tooSmall = $this->compare($version, $platformMin, '<');
Expand Down Expand Up @@ -187,11 +194,12 @@ public function isAppInstalled($appId) {
* Returns the version for the app if an update is available
*
* @param string $appId
* @param bool $isMajorUpdate are major app updates allowed
* @return bool|string
* @throws AppNotFoundException
* @throws AppNotInstalledException
*/
public function getAvailableUpdateVersion($appId) {
public function getAvailableUpdateVersion($appId, $isMajorUpdate = false) {
$info = $this->getInstalledAppInfo($appId);
if (is_null($info)) {
throw new AppNotInstalledException($this->l10n->t('App (%s) is not installed', $appId));
Expand All @@ -202,8 +210,15 @@ public function getAvailableUpdateVersion($appId) {
}
$releases = $marketInfo['releases'];
$currentVersion = (string) $info['version'];
$releases = array_filter($releases, function($r) use ($currentVersion) {
$releases = array_filter($releases, function($r) use ($currentVersion, $isMajorUpdate) {
$marketVersion = $r['version'];
$marketVersionMajor = $this->getMajorVersion($marketVersion);
$currentVersionMajor = $this->getMajorVersion($currentVersion);
if ($isMajorUpdate === false
&& $marketVersionMajor > $currentVersionMajor
) {
return false;
}
return version_compare($marketVersion, $currentVersion, '>');
});
usort($releases, function ($a, $b) {
Expand All @@ -215,6 +230,12 @@ public function getAvailableUpdateVersion($appId) {
return false;
}

public function getMajorVersion($version) {
$versionArray = \explode('.', $version);
return $versionArray[0];
}


public function getAppInfo($appId) {
$data = $this->getApps();
$data = array_filter($data, function($element) use ($appId) {
Expand Down Expand Up @@ -249,7 +270,7 @@ public function getInstalledAppInfo($appId) {
* @throws AppManagerException
* @throws AppNotInstalledException
*/
public function updateApp($appId) {
public function updateApp($appId, $isMajorUpdate = false) {
if (!$this->canInstall()) {
throw new \Exception("Installing apps is not supported because the app folder is not writable.");
}
Expand All @@ -260,7 +281,7 @@ public function updateApp($appId) {
}

// download package
$package = $this->downloadPackage($appId);
$package = $this->downloadPackage($appId, $isMajorUpdate, $info['version']);
$this->updatePackage($package);
}

Expand Down

0 comments on commit b9b901e

Please sign in to comment.