Completed
Push — master ( b2aa64...db9a1b )
by Alexey
05:01
created

File   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 8
c 2
b 1
f 0
lcom 0
cbo 2
dl 0
loc 52
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeSave() 0 7 4
A beforeDelete() 0 7 2
A getRealPath() 0 5 1
A relations() 0 13 1
1
<?php
2
3
/**
4
 * File
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
12
namespace Files;
13
14
class File extends \Model
15
{
16
    public static $cols = [
17
        'code' => ['type' => 'text'],
18
        'type_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'type'],
19
        'folder_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'foler'],
20
        'upload_code' => ['type' => 'text'],
21
        'path' => ['type' => 'textarea'],
22
        'name' => ['type' => 'text'],
23
        'about' => ['type' => 'html'],
24
        'original_name' => ['type' => 'text'],
25
        'md5' => ['type' => 'text'],
26
        'date_create' => ['type' => 'dateTime'],
27
    ];
28
29
    public function beforeSave()
30
    {
31
        $path = $this->getRealPath();
32
        if (!$this->md5 && $this->path && file_exists($path)) {
33
            $this->md5 = md5_file($path);
34
        }
35
    }
36
37
    public function beforeDelete()
38
    {
39
        $path = $this->getRealPath();
40
        if (file_exists($path)) {
41
            unlink($path);
42
        }
43
    }
44
45
    public function getRealPath()
46
    {
47
        $sitePath = \App::$primary->path;
48
        return "{$sitePath}{$this->path}";
49
    }
50
51
    public static function relations()
52
    {
53
        return [
54
            'type' => [
55
                'model' => 'Files\Type',
56
                'col' => 'type_id'
57
            ],
58
            'folder' => [
59
                'model' => 'Files\Folder',
60
                'col' => 'folder_id'
61
            ],
62
        ];
63
    }
64
65
}
66