SubscriptionManager.php
2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
namespace OTGS\Installer\Subscription;
use OTGS\Installer\Api\Exception\InvalidProductBucketUrl;
use OTGS\Installer\Api\InstallerApiClient;
use OTGS_Products_Config_Db_Storage;
class SubscriptionManager {
/**
* @var InstallerApiClient
*/
private $apiClient;
/**
* @var OTGS_Products_Config_Db_Storage
*/
private $productsConfigStorage;
/**
* @var string
*/
private $repositoryId;
/**
* @param string $repositoryId
* @param InstallerApiClient $apiClient
* @param OTGS_Products_Config_Db_Storage $productsConfigStorage
*/
public function __construct( $repositoryId, InstallerApiClient $apiClient, OTGS_Products_Config_Db_Storage $productsConfigStorage ) {
$this->repositoryId = $repositoryId;
$this->apiClient = $apiClient;
$this->productsConfigStorage = $productsConfigStorage;
}
/**
* @param string $siteKey
* @param int $source
*
* @throws \OTGS_Installer_Fetch_Subscription_Exception
* @throws InvalidProductBucketUrl
*/
public function fetch( $siteKey, $source ) {
$fetchSubscriptionResult = $this->apiClient->fetchSubscription( $siteKey, $source );
$this->maybeUpdateBuckets( $fetchSubscriptionResult, $siteKey );
do_action( 'installer_fetched_subscription_data', $fetchSubscriptionResult, $this->repositoryId );
return [ $fetchSubscriptionResult->subscription_data, $fetchSubscriptionResult->site_key ];
}
/**
* @param array $fetchSubscriptionResult
* @param string $siteKey
*
* @throws InvalidProductBucketUrl
*/
private function maybeUpdateBuckets( $fetchSubscriptionResult, $siteKey ) {
if ( isset ( $fetchSubscriptionResult->bucket_version )
&& $this->shouldRefetchProductUrl( $this->repositoryId, $fetchSubscriptionResult->bucket_version ) ) {
$productUrl = $this->apiClient->fetchProductUrl( $siteKey );
if ( $productUrl ) {
$this->productsConfigStorage->store_repository_products_url( $this->repositoryId, $productUrl );
$this->productsConfigStorage->update_repository_product_version( $this->repositoryId, $fetchSubscriptionResult->bucket_version );
}
}
}
/**
* @param string $repository_id
* @param int $fetchVersion
*
* @return bool
*/
public function shouldRefetchProductUrl( $repository_id, $fetchVersion ) {
$currentVersion = $this->productsConfigStorage->get_repository_product_version( $repository_id );
return ! $currentVersion || $currentVersion < $fetchVersion;
}
}