1 | <?php |
||
13 | class FileActionInspector extends AbstractActionInspector |
||
14 | { |
||
15 | /** @var string */ |
||
16 | private $filePath; |
||
17 | |||
18 | /** @var resource */ |
||
19 | private $fh; |
||
20 | |||
21 | /** |
||
22 | * FileActionLog constructor. |
||
23 | * @param $filePath |
||
24 | */ |
||
25 | 2 | public function __construct($filePath) |
|
33 | |||
34 | /** |
||
35 | * @param ActionInterface $action |
||
36 | * @return boolean returns `false` if action is already exists in this state in the log |
||
37 | * @throws SchedulerException |
||
38 | */ |
||
39 | 2 | public function update(ActionInterface $action) |
|
40 | { |
||
41 | 2 | flock($this->fh, LOCK_EX); |
|
42 | 2 | fseek($this->fh, 0); |
|
43 | 2 | $content = ''; |
|
44 | 2 | $messages = []; |
|
45 | 2 | while (!feof($this->fh)) { |
|
46 | 2 | $content .= fgets($this->fh); |
|
47 | } |
||
48 | 2 | $actionId = $action->getId(); |
|
49 | 2 | $actionState = $action->getState(); |
|
50 | 2 | if ($content) { |
|
51 | 2 | $messages = json_decode($content, true); |
|
52 | } |
||
53 | 2 | if (!isset($messages[$actionId])) { |
|
54 | 2 | $messages[$actionId] = ['state' => $actionState]; |
|
55 | 2 | $result = true; |
|
56 | 2 | } else if ($this->isStateAllowed($action, $messages[$actionId]['state'])){ |
|
57 | 1 | $messages[$actionId]['state'] = $actionState; |
|
58 | 1 | if ($actionState === ActionInterface::STATE_FINISHED) { |
|
59 | 1 | $messages[$actionId]['report'] = $action->getReport(); |
|
60 | } |
||
61 | 1 | $result = true; |
|
62 | } else { |
||
63 | 2 | $result = false; |
|
64 | } |
||
65 | 2 | fseek($this->fh, 0); |
|
66 | 2 | fwrite($this->fh, json_encode($messages)); |
|
67 | 2 | flock($this->fh, LOCK_UN); |
|
68 | 2 | return $result; |
|
69 | } |
||
70 | |||
71 | 2 | function __destruct() |
|
75 | } |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.