FileFilter   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 115
c 0
b 0
f 0
wmc 12
lcom 1
cbo 1
ccs 0
cts 51
cp 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A mimeType() 0 6 1
A path() 0 7 1
A name() 0 6 1
A extension() 0 6 1
A status() 0 6 1
A notDeleted() 0 10 1
A isValid() 0 10 3
A processMatch() 0 8 3
1
<?php
2
3
  namespace Funivan\Cs\Fs;
4
5
  /**
6
   * @author Ivan Shcherbak <[email protected]> 2016
7
   */
8
  class FileFilter {
9
10
    /**
11
     * @var callable[]
12
     */
13
    private $callback = [];
14
15
16
    /**
17
     * @param string[] $regex
18
     * @return $this
19
     */
20
    public function mimeType($regex) {
21
      $this->callback[] = function (File $file) use ($regex) {
22
        return $this->processMatch($regex, $file->getMimeType());
23
      };
24
      return $this;
25
    }
26
27
28
    /**
29
     * @param string[] $regex
30
     * @return $this
31
     */
32
    public function path(array $regex) {
33
      $this->callback[] = function (File $file) use ($regex) {
34
        return $this->processMatch($regex, $file->getPath());
35
      };
36
37
      return $this;
38
    }
39
40
41
    /**
42
     * @param string[] $regex
43
     * @return $this
44
     */
45
    public function name(array $regex) {
46
      $this->callback[] = function (File $file) use ($regex) {
47
        return $this->processMatch($regex, $file->getName());
48
      };
49
      return $this;
50
    }
51
52
53
    /**
54
     * @param string[] $ext
55
     * @return $this
56
     */
57
    public function extension(array $ext) {
58
      $this->callback[] = function (File $file) use ($ext) {
59
        return in_array($file->getExtension(), $ext);
60
      };
61
      return $this;
62
    }
63
64
65
    /**
66
     * @param int[] $status
67
     * @return $this
68
     */
69
    public function status(array $status) {
70
      $this->callback[] = function (File $file) use ($status) {
71
        return in_array($file->getStatus(), $status);
72
      };
73
      return $this;
74
    }
75
76
77
    /**
78
     * @return $this
79
     */
80
    public function notDeleted() {
81
      $this->status([
82
        File::STATUS_ADDED,
83
        File::STATUS_COPIED,
84
        File::STATUS_RENAMED,
85
        File::STATUS_MODIFIED,
86
        File::STATUS_UNKNOWN,
87
      ]);
88
      return $this;
89
    }
90
91
92
    /**
93
     * @param File $fileInfo
94
     * @return bool
95
     */
96
    public function isValid(File $fileInfo) {
97
98
      foreach ($this->callback as $callback) {
99
        if ($callback($fileInfo) === false) {
100
          return false;
101
        }
102
      }
103
104
      return true;
105
    }
106
107
108
    /**
109
     * @param string[] $regex
110
     * @param string $value
111
     * @return bool
112
     */
113
    private function processMatch(array $regex, $value) {
114
      foreach ($regex as $reg) {
115
        if (preg_match($reg, $value) === 1) {
116
          return true;
117
        }
118
      }
119
      return false;
120
    }
121
122
  }