Completed
Pull Request — master (#101)
by Ankit
02:06
created

AbstractTus::setCache()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.128

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 6
nop 1
dl 0
loc 17
ccs 8
cts 10
cp 0.8
crap 4.128
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace TusPhp\Tus;
4
5
use TusPhp\Cache\Cacheable;
6
use TusPhp\Cache\CacheFactory;
7
8
abstract class AbstractTus
9
{
10
    /** @const string Tus protocol version. */
11
    const TUS_PROTOCOL_VERSION = '1.0.0';
12
13
    /** @const string Name separator for partial upload. */
14
    const PARTIAL_UPLOAD_NAME_SEPARATOR = '_';
15
16
    /** @const string Upload type normal. */
17
    const UPLOAD_TYPE_NORMAL = 'normal';
18
19
    /** @const string Upload type partial. */
20
    const UPLOAD_TYPE_PARTIAL = 'partial';
21
22
    /** @const string Upload type final. */
23
    const UPLOAD_TYPE_FINAL = 'final';
24
25
    /** @const string Header Content Type */
26
    const HEADER_CONTENT_TYPE = 'application/offset+octet-stream';
27
28
    /** @var Cacheable */
29
    protected $cache;
30
31
    /** @var string */
32
    protected $apiPath = '/files';
33
34
    /**
35
     * Set cache.
36
     *
37
     * @param mixed $cache
38
     *
39
     * @return self
40
     */
41 1
    public function setCache($cache) : self
42
    {
43 1
        if (is_string($cache)) {
44 1
            $this->cache = CacheFactory::make($cache);
45 1
        } elseif ($cache instanceof Cacheable) {
46 1
            $this->cache = $cache;
47
        }
48
49
        try {
50 1
            $prefix = 'tus:' . strtolower((new \ReflectionClass(static::class))->getShortName()) . ':';
51
        } catch (\ReflectionException $e) {
52
            $prefix = 'tus:';
53
        }
54
55 1
        $this->cache->setPrefix($prefix);
56
57 1
        return $this;
58
    }
59
60
    /**
61
     * Get cache.
62
     *
63
     * @return Cacheable
64
     */
65 1
    public function getCache() : Cacheable
66
    {
67 1
        return $this->cache;
68
    }
69
70
    /**
71
     * Set API path.
72
     *
73
     * @param string $path
74
     *
75
     * @return self
76
     */
77 1
    public function setApiPath(string $path) : self
78
    {
79 1
        $this->apiPath = $path;
80
81 1
        return $this;
82
    }
83
84
    /**
85
     * Get API path.
86
     *
87
     * @return string
88
     */
89 1
    public function getApiPath() : string
90
    {
91 1
        return $this->apiPath;
92
    }
93
}
94