Skip to content

Commit

Permalink
Beautify codestyle + better maintability.
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek committed May 20, 2021
1 parent 76a2439 commit b73fa2a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 29 deletions.
16 changes: 6 additions & 10 deletions src/Order/DefaultOrderVariableLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,10 @@

final class DefaultOrderVariableLoader implements VariableLoader
{
private EntityManagerInterface $entityManager;

private string $entityClassName;


public function __construct(EntityManagerInterface $entityManager, string $entityClassName)
{
$this->entityManager = $entityManager;
$this->entityClassName = $entityClassName;
public function __construct(
private EntityManagerInterface $entityManager,
private string $entityClassName,
) {
}


Expand All @@ -46,7 +41,8 @@ public function getCurrent(?\DateTime $findFromDate = null): ?string

try {
return (string) $selector->getQuery()->getSingleScalarResult();
} catch (\Throwable $e) {
} catch (\Throwable) {
// Silence is golden.
}

return null;
Expand Down
3 changes: 1 addition & 2 deletions src/Order/OrderEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

interface OrderEntity
{
/** @return string|int|null */
public function getId();
public function getId(): string|int|null;

public function getNumber(): string;
}
15 changes: 12 additions & 3 deletions src/Strategy/SimpleIncrementStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,25 @@

final class SimpleIncrementStrategy implements FormatStrategy
{
public function __construct(
private int $length = 8,
) {
if ($length < 4) {
throw new \InvalidArgumentException('Minimal length is 4, but "' . $length . '" given.');
}
}


public function generate(string $last): string
{
return \strlen($last) > 2
? str_pad((string) (((int) $last) + 1), 8, '0', STR_PAD_LEFT)
return strlen($last) > 2
? str_pad((string) (((int) $last) + 1), $this->length, '0', STR_PAD_LEFT)
: $this->getFirst();
}


public function getFirst(): string
{
return date('y') . '000001';
return date('y') . str_repeat('0', $this->length - 3) . '1';
}
}
28 changes: 15 additions & 13 deletions src/Strategy/YearPrefixIncrementStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,28 @@

final class YearPrefixIncrementStrategy implements FormatStrategy
{
private ?int $length;

private int $preferredLength;


public function __construct(?int $length = null, int $preferredLength = 8)
{
$this->length = $length;
$this->preferredLength = $preferredLength;
public function __construct(
private ?int $length = null,
private int $preferredLength = 8,
) {
}


public function generate(string $last): string
{
if (preg_match('/^(?<year>\d{2})(?<count>\d+)$/', $last, $variableParser)) {
if (($year = date('y')) === $variableParser['year']) {
$length = $this->length ?? \strlen($variableParser['count']);
$new = $variableParser['year'] . str_pad((string) ($variableParser['count'] + 1), $length, '0', STR_PAD_LEFT);
$year = date('y');
if ($year === $variableParser['year']) {
$length = $this->length ?? strlen($variableParser['count']);
$new = $variableParser['year']
. str_pad(
string: (string) ($variableParser['count'] + 1),
length: $length,
pad_string: '0',
pad_type: STR_PAD_LEFT,
);
} else {
$new = $year . str_repeat('0', \strlen($variableParser['count']) - 1) . '1';
$new = $year . str_repeat('0', strlen($variableParser['count']) - 1) . '1';
}
} else {
$new = $this->getFirst();
Expand Down
6 changes: 5 additions & 1 deletion src/VariableGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ public function generate(?string $last = null): int

public function getCurrent(bool $findReal = true): int
{
return (int) (($findReal === true ? $this->variableLoader->getCurrent() : null) ?? $this->strategy->getFirst());
$currentValue = $findReal === true
? $this->variableLoader->getCurrent()
: null;

return (int) ($currentValue ?? $this->strategy->getFirst());
}


Expand Down

0 comments on commit b73fa2a

Please sign in to comment.