AbstractResource   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 50
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSha1() 0 4 1
A moveToUpload() 0 22 3
A clear() 0 4 1
1
<?php
2
/**
3
 * AbstractResource.php
4
 * @author Revin Roman http://phptime.ru
5
 */
6
7
namespace rmrevin\yii\module\File\resources;
8
9
use rmrevin\yii\module\File;
10
use yii\helpers\FileHelper;
11
use yii\helpers\StringHelper;
12
13
/**
14
 * Class AbstractResource
15
 * @package rmrevin\yii\module\File\resources
16
 */
17
abstract class AbstractResource implements ResourceInterface
18
{
19
20
    /**
21
     * @param string|boolean $source
22
     */
23 8
    public function __construct($source)
24
    {
25 8
        $this->setSource($source);
26 6
    }
27
28
    /**
29
     * @return string
30
     */
31 6
    public function getSha1()
32
    {
33 6
        return sha1_file($this->getTemp());
34
    }
35
36
    /**
37
     * @return string|false
38
     */
39 5
    public function moveToUpload()
40
    {
41 5
        $filename = sha1(microtime());
42 5
        $ext = pathinfo($this->getName(), PATHINFO_EXTENSION);
43
44 5
        $upload_path = File\Module::module()->upload_path;
45 5
        $p1 = StringHelper::byteSubstr($filename, 0, 2);
46 5
        $p2 = StringHelper::byteSubstr($filename, 2, 2);
47 5
        $path = $upload_path . DIRECTORY_SEPARATOR . $p1 . DIRECTORY_SEPARATOR . $p2;
48 5
        if (!file_exists($path)) {
49 5
            FileHelper::createDirectory($path);
50 5
        }
51
52 5
        $file_path = $path . DIRECTORY_SEPARATOR . $filename . '.' . $ext;
53
54 5
        $result = copy($this->getTemp(), $file_path);
55 5
        $this->clear();
56
57 5
        chmod($file_path, 0664);
58
59 5
        return $result === true ? $file_path : false;
60
    }
61
62 2
    public function clear()
63
    {
64 2
        unlink($this->getTemp());
65
    }
66
}