Completed
Push — master ( 2855be...490a20 )
by Shcherbak
02:34
created

FileInfo   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 48%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 148
rs 10
c 2
b 0
f 0
ccs 12
cts 25
cp 0.48

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A getName() 0 3 1
A getExtension() 0 3 1
A getStatus() 0 3 1
A getStatusName() 0 3 1
A getMimeType() 0 6 2
A getPath() 0 3 1
A getContent() 0 8 2
A save() 0 6 2
1
<?php
2
3
  namespace Funivan\Cs\FileFinder;
4
5
  /**
6
   * @author Ivan Shcherbak <[email protected]> 2016
7
   */
8
  class FileInfo {
9
10
    const STATUS_UNKNOWN = 0;
11
12
    const STATUS_ADDED = 1;
13
14
    const STATUS_COPIED = 2;
15
16
    const STATUS_MODIFIED = 3;
17
18
    const STATUS_RENAMED = 4;
19
20
    const STATUS_DELETED = 5;
21
22
23
    /**
24
     * @var array
25
     */
26
    private static $statusMap = [
27
      self::STATUS_UNKNOWN => 'unknown',
28
      self::STATUS_ADDED => 'added',
29
      self::STATUS_COPIED => 'copied',
30
      self::STATUS_MODIFIED => 'modified',
31
      self::STATUS_RENAMED => 'renamed',
32
      self::STATUS_DELETED => 'deleted',
33
    ];
34
35
    /**
36
     * The full path to the file
37
     *
38
     * @var string
39
     */
40
    private $path;
41
42
    /**
43
     * The files status
44
     *
45
     * @var int
46
     */
47
    private $status;
48
49
    /**
50
     * @var \Funivan\PhpTokenizer\File
51
     */
52
    private $tokenizer;
0 ignored issues
show
Unused Code introduced by
The property $tokenizer is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
53
54
    /**
55
     * @var FileContent
56
     */
57
    private $content;
58
59
60
    /**
61
     * Initializes a new instance of the File class.
62 24
     *
63 24
     * @param string $status
64
     * @param string $filePath
65
     */
66
    public function __construct($filePath, $status) {
67 24
      if (!isset(self::$statusMap[$status])) {
68 24
        throw new \InvalidArgumentException('Invalid status');
69 24
      }
70
71
      $this->status = (int) $status;
72
      $this->path = $filePath;
73
    }
74
75
76
    /**
77
     * @return string
78
     */
79
    public function getName() {
80
      return basename($this->path);
81
    }
82
83
84
    /**
85
     * @return string
86
     */
87
    public function getExtension() {
88
      return pathinfo($this->path, PATHINFO_EXTENSION);
89
    }
90
91
92
    /**
93
     * @return string
94
     */
95
    public function getStatus() {
96
      return $this->status;
97
    }
98
99
100
    /**
101
     * @return string
102
     */
103
    public function getStatusName() {
104
      return self::$statusMap[$this->status];
105
    }
106
107
108
    /**
109
     * Return empty mime type on deleted file
110
     *
111
     * @return string
112
     */
113
    public function getMimeType() {
114
      if ($this->status === self::STATUS_DELETED) {
115
        return '';
116
      }
117
      return finfo_file(finfo_open(FILEINFO_MIME), $this->path);
118 24
    }
119 24
120
121
    /**
122
     * @return string
123
     */
124
    public function getPath() {
125
      return $this->path;
126 24
    }
127 24
128 24
129 24
    /**
130
     * @return FileContent
131 24
     */
132
    public function getContent() {
133
134
      if ($this->content === null) {
135
        $this->content = new FileContent(file_get_contents($this->path));
136
      }
137
138
      return $this->content;
139
    }
140
141
142
    /**
143
     * Save content to the file
144
     * Perform this action only if content was changed
145
     * @void
146
     */
147
    public function save() {
148
      $content = $this->getContent();
149
      if ($content->isChanged()) {
150
        file_put_contents($this->path, $content->get());
151
      }
152
    }
153
154
155
  }
156