ApiRouting::getRoutingCachePath()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
crap 6
1
<?php
2
3
namespace JobQueue\Application\Http;
4
5
use function FastRoute\cachedDispatcher;
6
use FastRoute\Dispatcher;
7
use FastRoute\RouteCollector;
8
9
final class ApiRouting
10
{
11
    /**
12
     *
13
     * @return Dispatcher
14
     */
15
    public static function create(): Dispatcher
16
    {
17
        return cachedDispatcher(function (RouteCollector $r)
18
        {
19
            $r->get(  '/tasks',             ListTasks::class );
20
            $r->post( '/tasks',             AddTask::class   );
21
            $r->get(  '/task/{identifier}', ShowTask::class  );
22
23
        }, [
24
            'cacheFile' => self::getRoutingCachePath(),
25
            'cacheDisabled' => 'prod' === getenv('JOBQUEUE_ENV') ? false : true,
26
        ]);
27
    }
28
29
    /**
30
     *
31
     * @return string
32
     */
33
    private static function getRoutingCachePath(): string
34
    {
35
        // Get dir path from environment variables
36
        if (!$dir = (string) getenv('JOBQUEUE_CACHE_PATH')) {
37
            $dir = sys_get_temp_dir();
38
        }
39
40
        return sprintf('%s/jobqueue_routing.php', $dir);
41
    }
42
}
43