magallanes/src/Utils.php

105 lines
2.3 KiB
PHP
Raw Normal View History

2016-12-31 07:52:25 +01:00
<?php
2022-04-10 06:20:03 +02:00
2016-12-31 07:52:25 +01:00
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage;
2017-01-01 05:33:34 +01:00
use Mage\Runtime\Runtime;
2016-12-31 07:52:25 +01:00
/**
* Utility class for resolving trivial operations
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class Utils
{
/**
* Given a stage code it will resolve a human friendly name
*/
2022-04-10 06:20:03 +02:00
public function getStageName(string $stage): string
2016-12-31 07:52:25 +01:00
{
switch ($stage) {
2017-01-01 05:33:34 +01:00
case Runtime::PRE_DEPLOY:
2017-01-07 05:53:57 +01:00
return 'Pre Deploy';
2016-12-31 07:52:25 +01:00
2017-01-01 05:33:34 +01:00
case Runtime::ON_DEPLOY:
2017-01-07 05:53:57 +01:00
return 'On Deploy';
2016-12-31 07:52:25 +01:00
2017-01-01 05:33:34 +01:00
case Runtime::POST_DEPLOY:
2017-01-07 05:53:57 +01:00
return 'Post Deploy';
2016-12-31 07:52:25 +01:00
2017-01-01 05:33:34 +01:00
case Runtime::ON_RELEASE:
2016-12-31 07:52:25 +01:00
return 'On Release';
2017-01-01 05:33:34 +01:00
case Runtime::POST_RELEASE:
2016-12-31 07:52:25 +01:00
return 'Post Release';
}
return $stage;
}
/**
* Given a Release ID, convert it to a DateTime instance
*/
2022-04-10 06:20:03 +02:00
public function getReleaseDate(string $releaseId): \DateTime
2016-12-31 07:52:25 +01:00
{
2022-04-10 06:20:03 +02:00
$formatted = sprintf(
'%d%d%d%d-%d%d-%d%d %d%d:%d%d:%d%d',
$releaseId[0],
$releaseId[1],
$releaseId[2],
$releaseId[3],
$releaseId[4],
$releaseId[5],
$releaseId[6],
$releaseId[7],
$releaseId[8],
$releaseId[9],
$releaseId[10],
$releaseId[11],
$releaseId[12],
$releaseId[13]
2017-01-01 07:23:27 +01:00
);
2016-12-31 07:52:25 +01:00
2022-04-10 06:20:03 +02:00
return new \DateTime($formatted);
2016-12-31 07:52:25 +01:00
}
/**
* Given a Date, calculate friendly how much time has passed
*/
2022-04-10 06:20:03 +02:00
public function getTimeDiff(\DateTime $releaseDate): string
2016-12-31 07:52:25 +01:00
{
2022-04-10 06:20:03 +02:00
$now = new \DateTime();
2016-12-31 07:52:25 +01:00
$diff = $now->diff($releaseDate);
2017-01-01 07:23:27 +01:00
if ($diff->days > 7) {
return '';
}
if ($diff->days == 7) {
return 'a week ago';
}
if ($diff->days >= 1) {
return sprintf('%d day(s) ago', $diff->days);
}
if ($diff->h >= 1) {
return sprintf('%d hour(s) ago', $diff->h);
}
if ($diff->i >= 1) {
return sprintf('%d minute(s) ago', $diff->i);
2016-12-31 07:52:25 +01:00
}
2017-01-01 07:23:27 +01:00
return 'just now';
2016-12-31 07:52:25 +01:00
}
}