Created
January 17, 2021 05:31
-
-
Save coreymcmahon/36f4d9a0877813c8bb3b13570f095a46 to your computer and use it in GitHub Desktop.
Check the current version of an iOS app in the iTunes store
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Corey Mcmahon <[email protected]> | |
* @date: 15 January 2021 | |
*/ | |
declare(strict_types=1); | |
namespace Foodkit\AppStores; | |
use Illuminate\Support\Arr; | |
use GuzzleHttp\Client; | |
class ITunes | |
{ | |
private $client; | |
public function __construct(Client $client) | |
{ | |
$this->client = $client; | |
} | |
public function get(string $appId, string $market = '*') :? string | |
{ | |
$response = $this->client->get($this->endpoint($appId, $market)); | |
$json = json_decode('' . $response->getBody(), true); | |
$results = Arr::get($json, 'results', []); | |
if (($count = count($results)) !== 1) { | |
throw new \RuntimeException("ITunes has {$count} results for app ID {$appId}, should have 1!"); | |
} | |
if (!($version = Arr::get($json, 'results.0.version'))) { | |
throw new \RuntimeException("ITunes does not have a version # for for app ID {$appId}!"); | |
} | |
return "{$version}"; | |
} | |
private function endpoint(string $appId, string $market): string | |
{ | |
$url = "https://itunes.apple.com/lookup?bundleId={$appId}"; | |
if ($market && $market !== '*') { | |
$url .= "&country={$market}"; | |
} | |
return $url; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment