Completed
Push — master ( d7f072...e80481 )
by Shcherbak
05:37
created

File::getContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
  namespace Funivan\Cs\FileFinder;
4
5
  /**
6
   * @author Ivan Shcherbak <[email protected]> 2016
7
   */
8
  class File {
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
    /**
51
     * @var FileContent
52
     */
53
    private $content;
54
55
56
    /**
57
     * Initializes a new instance of the File class.
58
     *
59
     * @param string $status
60
     * @param string $filePath
61
     */
62 24
    public function __construct($filePath, $status) {
63 24
      if (!isset(self::$statusMap[$status])) {
64
        throw new \InvalidArgumentException('Invalid status');
65
      }
66
67 24
      $this->status = (int) $status;
68 24
      $this->path = $filePath;
69 24
    }
70
71
72
    /**
73
     * @return string
74
     */
75
    public function getName() {
76
      return basename($this->path);
77
    }
78
79
80
    /**
81
     * @return string
82
     */
83
    public function getExtension() {
84
      return pathinfo($this->path, PATHINFO_EXTENSION);
85
    }
86
87
88
    /**
89
     * @return string
90
     */
91
    public function getStatus() {
92
      return $this->status;
93
    }
94
95
96
    /**
97
     * @return string
98
     */
99
    public function getStatusName() {
100
      return self::$statusMap[$this->status];
101
    }
102
103
104
    /**
105
     * Return empty mime type on deleted file
106
     *
107
     * @return string
108
     */
109
    public function getMimeType() {
110
      if ($this->status === self::STATUS_DELETED) {
111
        return '';
112
      }
113
      return finfo_file(finfo_open(FILEINFO_MIME), $this->path);
114
    }
115
116
117
    /**
118
     * @return string
119
     */
120
    public function getPath() {
121
      return $this->path;
122
    }
123
124
125
    /**
126
     * @return FileContent
127
     */
128 24
    public function getContent() {
129
130 24
      if ($this->content === null) {
131 24
        $this->content = new FileContent(file_get_contents($this->path));
132 24
      }
133
134 24
      return $this->content;
135
    }
136
137
138
    /**
139
     * Save content to the file
140
     * Perform this action only if content was changed
141
     * @void
142
     */
143
    public function save() {
144
      $content = $this->getContent();
145
      if ($content->isChanged()) {
146
        file_put_contents($this->path, $content->get());
147
      }
148
    }
149
150
  }
151