Skip to content

Commit 6c7c301

Browse files
pjcdawkinsclaude
andcommitted
Fix service access patterns for PHP 5.6 compatibility.
Replace dependency injection with service locator pattern: - Change $this->io->method() to $this->method() calls - Change $this->api->method() to $this->api()->method() calls - Use $this->getService('property_formatter') with local variable - Fix Format::format() static method call (was trying to call instance method on string) - Fix SourceField property access (remove ->value as they're now strings, not enums) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 0ff89f3 commit 6c7c301

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

src/Command/Metrics/MetricsCommandBase.php

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Platformsh\Cli\Command\Metrics;
44

55
use Platformsh\Cli\Model\Metrics\Field;
6+
use Platformsh\Cli\Model\Metrics\Format;
67
use Platformsh\Cli\Model\Metrics\SourceField;
78
use Platformsh\Cli\Model\Metrics\SourceFieldPercentage;
89
use Platformsh\Cli\Service\Io;
@@ -115,20 +116,20 @@ protected function processQuery(InputInterface $input, array $metricTypes, array
115116

116117
$selectedServiceNames = $this->getServices($input, $environment);
117118
if (!empty($selectedServiceNames)) {
118-
$this->io->debug('Selected service(s): ' . implode(', ', $selectedServiceNames));
119+
$this->debug('Selected service(s): ' . implode(', ', $selectedServiceNames));
119120
$query->setServices($selectedServiceNames);
120121
}
121-
$this->io->debug('Selected type(s): ' . implode(', ', $metricTypes));
122+
$this->debug('Selected type(s): ' . implode(', ', $metricTypes));
122123
$query->setTypes($metricTypes);
123-
$this->io->debug('Selected agg(s): ' . implode(', ', $metricAggs));
124+
$this->debug('Selected agg(s): ' . implode(', ', $metricAggs));
124125
$query->setAggs($metricAggs);
125126

126127
if ($this->stdErr->isDebug()) {
127-
$this->io->debug('Metrics query: ' . json_encode($query->asArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
128+
$this->debug('Metrics query: ' . json_encode($query->asArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
128129
}
129130

130131
// Perform the metrics query.
131-
$client = $this->api->getHttpClient();
132+
$client = $this->api()->getHttpClient();
132133
$request = $client->createRequest('GET', $metricsQueryUrl . $query->asString());
133134

134135
try {
@@ -183,7 +184,7 @@ protected function processQuery(InputInterface $input, array $metricTypes, array
183184
private function getServices(InputInterface $input, Environment $environment)
184185
{
185186
// Select services based on the --service or --type options.
186-
$deployment = $this->api->getCurrentDeployment($environment);
187+
$deployment = $this->api()->getCurrentDeployment($environment);
187188
$allServices = array_merge($deployment->webapps, $deployment->services, $deployment->workers);
188189
$servicesInput = ArrayArgument::getOption($input, 'service');
189190
$selectedServiceNames = array();
@@ -230,7 +231,7 @@ private function getServices(InputInterface $input, Environment $environment)
230231
*/
231232
protected function validateTimeInput(InputInterface $input)
232233
{
233-
$this->io->warnAboutDeprecatedOptions(array('interval'));
234+
$this->warnAboutDeprecatedOptions(['interval']);
234235

235236
if ($to = $input->getOption('to')) {
236237
$endTime = \strtotime($to);
@@ -272,6 +273,8 @@ protected function validateTimeInput(InputInterface $input)
272273
*/
273274
protected function buildRows(array $values, array $fieldMapping, Environment $environment)
274275
{
276+
/** @var \Platformsh\Cli\Service\PropertyFormatter $formatter */
277+
$formatter = $this->getService('property_formatter');
275278
$sortServices = $this->getSortedServices($environment);
276279
$serviceTypes = array();
277280

@@ -290,7 +293,7 @@ protected function buildRows(array $values, array $fieldMapping, Environment $en
290293
$rows[] = new TableSeparator();
291294
}
292295
$startCount = count($rows);
293-
$formattedTimestamp = $this->propertyFormatter->formatDate($timestamp);
296+
$formattedTimestamp = $formatter->formatDate($timestamp);
294297

295298
uksort($byService, $sortServices);
296299
foreach ($byService as $service => $byDimension) {
@@ -301,10 +304,10 @@ protected function buildRows(array $values, array $fieldMapping, Environment $en
301304
$row = array();
302305
$row['timestamp'] = new AdaptiveTableCell($formattedTimestamp, array('wrap' => false));
303306
$row['service'] = $service;
304-
$row['type'] = $this->propertyFormatter->format($serviceTypes[$service], 'service_type');
307+
$row['type'] = $formatter->format($serviceTypes[$service], 'service_type');
305308
foreach ($fieldMapping as $field => $fieldDefinition) {
306309
/* @var Field $fieldDefinition */
307-
$row[$field] = $fieldDefinition->format->format($this->getValueFromSource($byDimension, $fieldDefinition->value), $fieldDefinition->warn);
310+
$row[$field] = Format::format($fieldDefinition->format, $this->getValueFromSource($byDimension, $fieldDefinition->value), $fieldDefinition->warn);
308311
}
309312
$rows[] = $row;
310313
}
@@ -321,7 +324,7 @@ protected function buildRows(array $values, array $fieldMapping, Environment $en
321324
*/
322325
private function getServiceType(Environment $environment, $service)
323326
{
324-
$deployment = $this->api->getCurrentDeployment($environment);
327+
$deployment = $this->api()->getCurrentDeployment($environment);
325328

326329
if (isset($deployment->services[$service])) {
327330
$type = $deployment->services[$service]->type;
@@ -392,24 +395,24 @@ private function extractValue(array $point, SourceField $sourceField)
392395
if (!isset($point['mountpoints'][$sourceField->mountpoint])) {
393396
return null;
394397
}
395-
if (!isset($point['mountpoints'][$sourceField->mountpoint][$sourceField->source->value])) {
396-
throw new \RuntimeException(\sprintf('Source "%s" not found in the mountpoint "%s".', $sourceField->source->value, $sourceField->mountpoint));
398+
if (!isset($point['mountpoints'][$sourceField->mountpoint][$sourceField->source])) {
399+
throw new \RuntimeException(\sprintf('Source "%s" not found in the mountpoint "%s".', $sourceField->source, $sourceField->mountpoint));
397400
}
398-
if (!isset($point['mountpoints'][$sourceField->mountpoint][$sourceField->source->value][$sourceField->aggregation->value])) {
399-
throw new \RuntimeException(\sprintf('Aggregation "%s" not found for source "%s" in mountpoint "%s".', $sourceField->aggregation->value, $sourceField->source->value, $sourceField->mountpoint));
401+
if (!isset($point['mountpoints'][$sourceField->mountpoint][$sourceField->source][$sourceField->aggregation])) {
402+
throw new \RuntimeException(\sprintf('Aggregation "%s" not found for source "%s" in mountpoint "%s".', $sourceField->aggregation, $sourceField->source, $sourceField->mountpoint));
400403
}
401404

402-
return $point['mountpoints'][$sourceField->mountpoint][$sourceField->source->value][$sourceField->aggregation->value];
405+
return $point['mountpoints'][$sourceField->mountpoint][$sourceField->source][$sourceField->aggregation];
403406
}
404407

405-
if (!isset($point[$sourceField->source->value])) {
406-
throw new \RuntimeException(\sprintf('Source "%s" not found in the data point.', $sourceField->source->value));
408+
if (!isset($point[$sourceField->source])) {
409+
throw new \RuntimeException(\sprintf('Source "%s" not found in the data point.', $sourceField->source));
407410
}
408-
if (!isset($point[$sourceField->source->value][$sourceField->aggregation->value])) {
409-
throw new \RuntimeException(\sprintf('Aggregation "%s" not found for source "%s".', $sourceField->aggregation->value, $sourceField->source->value));
411+
if (!isset($point[$sourceField->source][$sourceField->aggregation])) {
412+
throw new \RuntimeException(\sprintf('Aggregation "%s" not found for source "%s".', $sourceField->aggregation, $sourceField->source));
410413
}
411414

412-
return $point[$sourceField->source->value][$sourceField->aggregation->value];
415+
return $point[$sourceField->source][$sourceField->aggregation];
413416
}
414417

415418
/**

0 commit comments

Comments
 (0)