CallableFileTool::setProcessCallback()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
  namespace Funivan\Cs\FileTool;
4
5
  use Funivan\Cs\Fs\File;
6
  use Funivan\Cs\Report\Report;
7
8
  /**
9
   * @author Ivan Shcherbak <[email protected]> 2016
10
   */
11
  class CallableFileTool implements FileTool {
12
13
    /**
14
     * @var string
15
     */
16
    private $name;
17
18
    /**
19
     * @var string|null
20
     */
21
    private $description = null;
22
23
    /**
24
     * @var callable|null
25
     */
26
    private $processCallback = null;
27
28
    /**
29
     * @var callable|null
30
     */
31
    private $canProcessCallback = null;
32
33
34
    /**
35
     * @param string $name
36
     */
37
    public function __construct($name) {
38
      $this->name = $name;
39
    }
40
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function getName() {
46
      return $this->name;
47
    }
48
49
50
    /**
51
     * @return string
52
     */
53
    public function getDescription() {
54
      if ($this->description === null) {
55
        throw new \RuntimeException('Empty tool description');
56
57
      }
58
      return $this->description;
59
    }
60
61
62
    /**
63
     * @param File $file
64
     * @return bool
65
     * @throws \Exception
66
     */
67
    public function canProcess(File $file) {
68
      $callback = $this->canProcessCallback;
69
70
      if ($callback === null) {
71
        throw new \RuntimeException('Empty canProcess callback');
72
      }
73
74
      return $callback($file);
75
    }
76
77
78
    /**
79
     * @param File $file
80
     * @param Report $report
81
     * @throws \Exception
82
     */
83
    public function process(File $file, Report $report) {
84
      $callback = $this->processCallback;
85
86
      if ($callback === null) {
87
        throw new \RuntimeException('Empty process callback');
88
      }
89
90
91
      $callback($file, $report);
92
    }
93
94
95
    /**
96
     * @param callable $processCallback
97
     * @return $this
98
     */
99
    public function setProcessCallback(callable  $processCallback) {
100
      $this->processCallback = $processCallback;
101
      return $this;
102
    }
103
104
105
    /**
106
     * @param callable $canProcessCallback
107
     * @return $this
108
     */
109
    public function setCanProcessCallback(callable $canProcessCallback) {
110
      $this->canProcessCallback = $canProcessCallback;
111
      return $this;
112
    }
113
114
  }