1 | <?php |
||
32 | abstract class ScannerBase { |
||
33 | |||
34 | /** |
||
35 | * Scan result |
||
36 | * @var Status |
||
37 | */ |
||
38 | protected $status; |
||
39 | |||
40 | /** |
||
41 | * If scanning was done part by part |
||
42 | * the first detected infected part is stored here |
||
43 | * @var Status |
||
44 | */ |
||
45 | protected $infectedStatus; |
||
46 | |||
47 | /** @var int */ |
||
48 | protected $byteCount; |
||
49 | |||
50 | /** @var resource */ |
||
51 | protected $writeHandle; |
||
52 | |||
53 | /** @var AppConfig */ |
||
54 | protected $appConfig; |
||
55 | |||
56 | /** @var ILogger */ |
||
57 | protected $logger; |
||
58 | |||
59 | /** @var StatusFactory */ |
||
60 | protected $statusFactory; |
||
61 | |||
62 | /** @var string */ |
||
63 | protected $lastChunk; |
||
64 | |||
65 | /** @var bool */ |
||
66 | protected $isLogUsed = false; |
||
67 | |||
68 | /** @var bool */ |
||
69 | protected $isAborted = false; |
||
70 | |||
71 | /** |
||
72 | * ScannerBase constructor. |
||
73 | * |
||
74 | * @param AppConfig $config |
||
75 | * @param ILogger $logger |
||
76 | * @param StatusFactory $statusFactory |
||
77 | */ |
||
78 | 2 | public function __construct(AppConfig $config, ILogger $logger, StatusFactory $statusFactory) { |
|
79 | 2 | $this->appConfig = $config; |
|
80 | 2 | $this->logger = $logger; |
|
81 | 2 | $this->statusFactory = $statusFactory; |
|
82 | 2 | } |
|
83 | |||
84 | /** |
||
85 | * Close used resources |
||
86 | */ |
||
87 | abstract protected function shutdownScanner(); |
||
88 | |||
89 | |||
90 | public function getStatus(){ |
||
91 | if ($this->infectedStatus instanceof Status){ |
||
92 | return $this->infectedStatus; |
||
93 | } |
||
94 | if ($this->status instanceof Status){ |
||
95 | return $this->status; |
||
96 | } |
||
97 | return $this->statusFactory->newStatus(); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Synchronous scan |
||
102 | * @param Item $item |
||
103 | * @return Status |
||
104 | */ |
||
105 | public function scan(Item $item) { |
||
106 | $this->initScanner(); |
||
107 | |||
108 | while (false !== ($chunk = $item->fread())) { |
||
109 | $this->writeChunk($chunk); |
||
110 | } |
||
111 | |||
112 | $this->shutdownScanner(); |
||
113 | return $this->getStatus(); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Async scan - new portion of data is available |
||
118 | * @param string $data |
||
119 | */ |
||
120 | public function onAsyncData($data){ |
||
123 | |||
124 | /** |
||
125 | * Async scan - resource is closed |
||
126 | * @return Status |
||
127 | */ |
||
128 | public function completeAsyncScan(){ |
||
129 | $this->shutdownScanner(); |
||
130 | return $this->getStatus(); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Open write handle. etc |
||
135 | */ |
||
136 | public function initScanner(){ |
||
137 | $this->byteCount = 0; |
||
138 | if ($this->status instanceof Status && $this->status->getNumericStatus() === Status::SCANRESULT_INFECTED){ |
||
139 | $this->infectedStatus = clone $this->status; |
||
140 | } |
||
141 | $this->status = $this->statusFactory->newStatus(); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @param string $chunk |
||
146 | */ |
||
147 | protected function writeChunk($chunk){ |
||
152 | |||
153 | /** |
||
154 | * @param string $data |
||
155 | */ |
||
156 | final protected function fwrite($data){ |
||
157 | if ($this->isAborted){ |
||
187 | |||
188 | /** |
||
189 | * @return bool |
||
190 | */ |
||
191 | protected function retry(){ |
||
198 | |||
199 | /** |
||
200 | * @param $data |
||
201 | * @return bool |
||
202 | */ |
||
203 | protected function writeRaw($data){ |
||
213 | |||
214 | /** |
||
215 | * Get a resource to write data into |
||
216 | * @return resource |
||
217 | */ |
||
218 | protected function getWriteHandle(){ |
||
221 | |||
222 | /** |
||
223 | * Prepare chunk (if required) |
||
224 | * @param string $data |
||
225 | * @return string |
||
226 | */ |
||
227 | protected function prepareChunk($data){ |
||
230 | } |
||
231 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: