Completed
Push — master ( 1f427e...eb5029 )
by Ron
01:13
created

DateTimeHelper::getNextRunDateFromCronExpression()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 4
nop 2
1
<?php
2
namespace Kir\Services\Cmd\Dispatcher\Common;
3
4
use Cron\CronExpression;
5
use DateTime;
6
use DateTimeImmutable;
7
use DateTimeInterface;
8
use Exception;
9
use RuntimeException;
10
11
abstract class DateTimeHelper {
12 View Code Duplication
	public static function create($init = null): DateTime {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
13
		try {
14
			if($init instanceof DateTimeInterface) {
15
				return new DateTime($init->format('c'));
16
			}
17
			return new DateTime($init);
18
		} catch (Exception $e) {
19
			throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
20
		}
21
	}
22
	
23 View Code Duplication
	public static function createImmutable($init = null): DateTimeImmutable {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
		try {
25
			if($init instanceof DateTimeInterface) {
26
				return new DateTimeImmutable($init->format('c'));
27
			}
28
			return new DateTimeImmutable($init);
29
		} catch (Exception $e) {
30
			throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
31
		}
32
	}
33
	
34
	/**
35
	 * @param string $interval
36
	 * @param DateTimeInterface $now
37
	 * @return DateTimeImmutable
38
	 */
39
	public static function getNextRunDateFromCronExpression(string $interval, DateTimeInterface $now): DateTimeImmutable {
40
		try {
41
			$expr = new CronExpression($interval);
42
			$dt = $expr->getNextRunDate($now);
43
			return self::createImmutable($dt);
44
		} catch (Exception $e) {
45
			throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
46
		}
47
	}
48
}