diff --git a/src/Command/WebCommand.php b/src/Command/WebCommand.php index 9ee1507ea..68d403f34 100644 --- a/src/Command/WebCommand.php +++ b/src/Command/WebCommand.php @@ -2,7 +2,6 @@ namespace Platformsh\Cli\Command; -use GuzzleHttp\Exception\BadResponseException; use Platformsh\Cli\Service\Url; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -41,31 +40,11 @@ protected function execute(InputInterface $input, OutputInterface $output) if ($this->hasSelectedProject()) { $project = $this->getSelectedProject(); - if ($this->config()->has('service.console_url') && $this->config()->get('api.organizations')) { - // Load the organization name if possible. - $firstSegment = $organizationId = $project->getProperty('organization'); - try { - $organization = $this->api()->getClient()->getOrganizationById($organizationId); - if ($organization) { - $firstSegment = $organization->name; - } - } catch (BadResponseException $e) { - if ($e->getResponse() && $e->getResponse()->getStatusCode() === 403) { - trigger_error($e->getMessage(), E_USER_WARNING); - } else { - throw $e; - } - } - - $isConsole = true; - $url = ltrim($this->config()->get('service.console_url'), '/') . '/' . rawurlencode($firstSegment) . '/' . rawurlencode($project->id); - } else { - $subscription = $this->api()->getClient()->getSubscription($project->getSubscriptionId()); - $url = $subscription->project_ui; - $isConsole = $this->config()->has('detection.console_domain') && parse_url($url, PHP_URL_HOST) === $this->config()->get('detection.console_domain'); - } + $url = $this->api()->getConsoleURL($project); if ($environmentId !== null) { // Console links lack the /environments path component. + $isConsole = $this->config()->has('detection.console_domain') + && parse_url($url, PHP_URL_HOST) === $this->config()->get('detection.console_domain'); if ($isConsole) { $url .= '/' . rawurlencode($environmentId); } else { diff --git a/src/Command/WelcomeCommand.php b/src/Command/WelcomeCommand.php index 1740664ea..98ce5f0f6 100644 --- a/src/Command/WelcomeCommand.php +++ b/src/Command/WelcomeCommand.php @@ -66,10 +66,9 @@ private function defaultWelcome() */ private function welcomeForLocalProjectDir(Project $project) { - $projectUri = $project->getLink('#ui'); $this->stdErr->writeln("Project title: {$project->title}"); $this->stdErr->writeln("Project ID: {$project->id}"); - $this->stdErr->writeln("Project dashboard: $projectUri\n"); + $this->stdErr->writeln("Project dashboard: " . $this->api()->getConsoleURL($project) . "\n"); // Show the environments. $this->runOtherCommand('environments', [ diff --git a/src/Service/Api.php b/src/Service/Api.php index 220334dd3..8c5c036d1 100644 --- a/src/Service/Api.php +++ b/src/Service/Api.php @@ -1555,4 +1555,59 @@ public function getUserRefLabel(UserRef $userRef, $tag = 'info') } return \sprintf($pattern, $tag, $name, $userRef->email); } + + /** + * Loads an organization by ID, with caching. + * + * @param string $id + * @param bool $reset + * @return Organization|false + */ + public function getOrganizationById($id, $reset = false) + { + $cacheKey = 'organization:' . $id; + if (!$reset && ($cached = $this->cache->fetch($cacheKey))) { + return new Organization($cached, $cached['_url'], $this->getHttpClient()); + } + $organization = $this->getClient()->getOrganizationById($id); + if ($organization) { + $data = $organization->getData(); + $data['_url'] = $organization->getUri(); + $this->cache->save($cacheKey, $data, $this->config->getWithDefault('api.orgs_ttl', 3600)); + } + return $organization; + } + + /** + * Returns the Console URL for a project, with caching. + * + * @param Project $project + * @param bool $reset + * + * @return false|string + */ + public function getConsoleURL(Project $project, $reset = false) + { + if ($this->config->has('service.console_url') && $this->config->get('api.organizations')) { + // Load the organization name if possible. + $firstSegment = $organizationId = $project->getProperty('organization'); + try { + $organization = $this->getOrganizationById($organizationId, $reset); + if ($organization) { + $firstSegment = $organization->name; + } + } catch (BadResponseException $e) { + if ($e->getResponse() && $e->getResponse()->getStatusCode() === 403) { + trigger_error($e->getMessage(), E_USER_WARNING); + } else { + throw $e; + } + } + + return ltrim($this->config->get('service.console_url'), '/') . '/' . rawurlencode($firstSegment) . '/' . rawurlencode($project->id); + } + $subscription = $this->loadSubscription($project->getSubscriptionId(), $project); + return $subscription ? $subscription->project_ui : false; + } + }