Completed
Push — master ( 206c33...9e77c2 )
by Oscar
05:44
created

FileTrait::getFilename()   B

Complexity

Conditions 6
Paths 20

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 22
rs 8.6738
cc 6
eloc 13
nc 20
nop 1
1
<?php
2
3
namespace Psr7Middlewares\Utils;
4
5
use Psr\Http\Message\RequestInterface;
6
7
/**
8
 * Common methods used by middlewares that read/write files.
9
 */
10
trait FileTrait
11
{
12
    use BasePathTrait;
13
    use StorageTrait;
14
15
    /**
16
     * Returns the filename of the response file.
17
     *
18
     * @param RequestInterface $request
19
     *
20
     * @return string
21
     */
22
    protected function getFilename(RequestInterface $request)
23
    {
24
        $path = $this->getBasePath($request->getUri()->getPath());
25
26
        $parts = pathinfo($path);
27
        $path = '/'.(isset($parts['dirname']) ? $parts['dirname'] : '');
28
        $filename = isset($parts['basename']) ? $parts['basename'] : '';
29
30
        //if it's a directory, append "/index.html"
31
        if (empty($parts['extension'])) {
32
            if ($path === '/') {
33
                $path .= $filename;
34
            } else {
35
                $path .= "/{$filename}";
36
            }
37
38
            $extension = strtolower(pathinfo($request->getUri()->getPath(), PATHINFO_EXTENSION)) ?: 'html';
39
            $filename = "index.{$extension}";
40
        }
41
42
        return "{$this->storage}{$path}/{$filename}";
43
    }
44
}
45