Completed
Push — develop ( a737ee...e18187 )
by Alejandro
17s queued 12s
created

determineTableName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
nc 2
nop 2
dl 0
loc 10
c 0
b 0
f 0
cc 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shlinkio\Shlink\Core;
6
7
use Cake\Chronos\Chronos;
8
use DateTimeInterface;
9
use PUGX\Shortid\Factory as ShortIdFactory;
10
11
use function sprintf;
12
13
function generateRandomShortCode(int $length = 5): string
14
{
15
    static $shortIdFactory;
16
    if ($shortIdFactory === null) {
17
        $shortIdFactory = new ShortIdFactory();
18
    }
19
20
    $alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
21
    return $shortIdFactory->generate($length, $alphabet)->serialize();
22
}
23
24
function parseDateFromQuery(array $query, string $dateName): ?Chronos
25
{
26
    return ! isset($query[$dateName]) || empty($query[$dateName]) ? null : Chronos::parse($query[$dateName]);
27
}
28
29
/**
30
 * @param string|DateTimeInterface|Chronos|null $date
31
 */
32
function parseDateField($date): ?Chronos
33
{
34
    if ($date === null || $date instanceof Chronos) {
35
        return $date;
36
    }
37
38
    if ($date instanceof DateTimeInterface) {
39
        return Chronos::instance($date);
40
    }
41
42
    return Chronos::parse($date);
43
}
44
45
function determineTableName(string $tableName, array $emConfig = []): string
46
{
47
    $schema = $emConfig['connection']['schema'] ?? null;
48
//    $tablePrefix = $emConfig['connection']['table_prefix'] ?? null; // TODO
49
50
    if ($schema === null) {
51
        return $tableName;
52
    }
53
54
    return sprintf('%s.%s', $schema, $tableName);
55
}
56