Skip to content

Commit

Permalink
Merge pull request #184 from bold-commerce/Q1-661
Browse files Browse the repository at this point in the history
Expose Bold modules installed and their versions through an api endpoint
  • Loading branch information
nmalevanec authored Nov 23, 2023
2 parents df9ff0f + 8c3ce44 commit 8bf69d0
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 1 deletion.
25 changes: 25 additions & 0 deletions Api/Data/ModuleVersion/ModuleVersionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Bold\Checkout\Api\Data\ModuleVersion;

/**
* Installed module and it's version.
*/
interface ModuleVersionInterface
{
/**
* Get module name.
*
* @return string
*/
public function getName(): string;

/**
* Get module version.
*
* @return string
*/
public function getVersion(): string;
}
18 changes: 18 additions & 0 deletions Api/Data/ModuleVersion/ResultInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Bold\Checkout\Api\Data\ModuleVersion;

/**
* Installed modules and their versions.
*/
interface ResultInterface
{
/**
* Installed modules and their versions.
*
* @return \Bold\Checkout\Api\Data\ModuleVersion\ModuleVersionInterface[]
*/
public function getModules(): array;
}
3 changes: 3 additions & 0 deletions Api/GetVersionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

namespace Bold\Checkout\Api;

/**
* @deprecated
*/
interface GetVersionInterface
{
/**
Expand Down
19 changes: 19 additions & 0 deletions Api/ModuleVersionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Bold\Checkout\Api;

/**
* Get all installed modules and their versions.
*/
interface ModuleVersionInterface
{
/**
* Get all installed modules and their versions..
*
* @param string $shopId
* @return \Bold\Checkout\Api\Data\ModuleVersion\ResultInterface
*/
public function getModuleVersions(string $shopId): Data\ModuleVersion\ResultInterface;
}
49 changes: 49 additions & 0 deletions Model/Data/ModuleVersion/ModuleVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Bold\Checkout\Model\Data\ModuleVersion;

use Bold\Checkout\Api\Data\ModuleVersion\ModuleVersionInterface;

/**
* @inheritDoc
*/
class ModuleVersion implements ModuleVersionInterface
{
/**
* @var string
*/
private $name;

/**
* @var string
*/
private $version;

/**
* @param string $name
* @param string $version
*/
public function __construct(string $name = '', string $version = '')
{
$this->name = $name;
$this->version = $version;
}

/**
* @inheritDoc
*/
public function getName(): string
{
return $this->name;
}

/**
* @inheritDoc
*/
public function getVersion(): string
{
return $this->version;
}
}
35 changes: 35 additions & 0 deletions Model/Data/ModuleVersion/Result.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Bold\Checkout\Model\Data\ModuleVersion;

use Bold\Checkout\Api\Data\ModuleVersion\ModuleVersionInterface;
use Bold\Checkout\Api\Data\ModuleVersion\ResultInterface;

/**
* @inheritDoc
*/
class Result implements ResultInterface
{
/**
* @var array
*/
private $modules;

/**
* @param ModuleVersionInterface[] $modules
*/
public function __construct(array $modules = [])
{
$this->modules = $modules;
}

/**
* @inheritDoc
*/
public function getModules(): array
{
return $this->modules;
}
}
2 changes: 1 addition & 1 deletion Model/ModuleInfo/ModuleComposerVersionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function getVersion(string $module): string
* @param string $module
* @return string
*/
public function parseVersion(string $module): string
private function parseVersion(string $module): string
{
try {
$directoryPath = $this->reader->getModuleDir('', $module);
Expand Down
87 changes: 87 additions & 0 deletions Model/ModuleVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace Bold\Checkout\Model;

use Bold\Checkout\Api\Data\ModuleVersion\ModuleVersionInterfaceFactory;
use Bold\Checkout\Api\Data\ModuleVersion\ResultInterface;
use Bold\Checkout\Api\Data\ModuleVersion\ResultInterfaceFactory;
use Bold\Checkout\Api\ModuleVersionInterface;
use Bold\Checkout\Model\ModuleInfo\InstalledModulesProvider;
use Bold\Checkout\Model\ModuleInfo\ModuleComposerNameProvider;
use Bold\Checkout\Model\ModuleInfo\ModuleComposerVersionProvider;

/**
* @inheritDoc
*/
class ModuleVersion implements ModuleVersionInterface
{
/**
* @var InstalledModulesProvider
*/
private $installedModulesProvider;

/**
* @var ModuleComposerVersionProvider
*/
private $composerVersionProvider;

/**
* @var ModuleComposerNameProvider
*/
private $composerNameProvider;

/**
* @var ResultInterfaceFactory
*/
private $resultFactory;

/**
* @var ModuleVersionInterfaceFactory
*/
private $moduleVersionFactory;

/**
* @param InstalledModulesProvider $installedModulesProvider
* @param ModuleComposerVersionProvider $composerVersionProvider
* @param ModuleComposerNameProvider $composerNameProvider
* @param ResultInterfaceFactory $resultFactory
* @param ModuleVersionInterfaceFactory $moduleVersionFactory
*/
public function __construct(
InstalledModulesProvider $installedModulesProvider,
ModuleComposerVersionProvider $composerVersionProvider,
ModuleComposerNameProvider $composerNameProvider,
ResultInterfaceFactory $resultFactory,
ModuleVersionInterfaceFactory $moduleVersionFactory
) {
$this->installedModulesProvider = $installedModulesProvider;
$this->composerVersionProvider = $composerVersionProvider;
$this->composerNameProvider = $composerNameProvider;
$this->resultFactory = $resultFactory;
$this->moduleVersionFactory = $moduleVersionFactory;
}

/**
* @inheritDoc
*/
public function getModuleVersions(string $shopId): ResultInterface
{
return $this->resultFactory->create(
[
'modules' => array_map(
function (string $moduleName) {
return $this->moduleVersionFactory->create(
[
'name' => $this->composerNameProvider->getName($moduleName),
'version' => $this->composerVersionProvider->getVersion($moduleName)
]
);
},
$this->installedModulesProvider->getModuleList()
),
]
);
}
}
3 changes: 3 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
<preference for="Bold\Checkout\Api\GetVersionInterface" type="Bold\Checkout\Model\GetVersion"/>
<preference for="Bold\Checkout\Api\Data\DiscountDataInterface" type="Bold\Checkout\Model\Data\DiscountData"/>
<preference for="Bold\Checkout\Api\Data\RuleDiscountInterface" type="Bold\Checkout\Model\Data\RuleDiscount"/>
<preference for="Bold\Checkout\Api\ModuleVersionInterface" type="Bold\Checkout\Model\ModuleVersion"/>
<preference for="Bold\Checkout\Api\Data\ModuleVersion\ResultInterface" type="Bold\Checkout\Model\Data\ModuleVersion\Result"/>
<preference for="Bold\Checkout\Api\Data\ModuleVersion\ModuleVersionInterface" type="Bold\Checkout\Model\Data\ModuleVersion\ModuleVersion"/>
<type name="Bold\Checkout\Observer\Order\Shipment\FulfillOrderItemsObserver">
<arguments>
<argument name="client" xsi:type="object">Bold\Checkout\Model\Http\BoldClient</argument>
Expand Down
6 changes: 6 additions & 0 deletions etc/webapi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,10 @@
<resource ref="anonymous" />
</resources>
</route>
<route method="GET" url="/V1/shops/:shopId/modules">
<service class="Bold\Checkout\Api\ModuleVersionInterface" method="getModuleVersions"/>
<resources>
<resource ref="Bold_Checkout::integration" />
</resources>
</route>
</routes>

0 comments on commit 8bf69d0

Please sign in to comment.