1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wabel\CertainAPI\Listeners; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Wabel\CertainAPI\Helpers\FileChangesHelper; |
7
|
|
|
use Wabel\CertainAPI\Interfaces\CertainListener; |
8
|
|
|
|
9
|
|
|
class ChangingsToFileListeners implements CertainListener |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $dirPathChangings; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* ChangingsToFileListeners constructor. |
19
|
|
|
* @param string $dirPathChangings |
20
|
|
|
*/ |
21
|
|
|
public function __construct($dirPathChangings) |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
$this->dirPathChangings = $dirPathChangings; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $eventCode |
29
|
|
|
* @param array $elements |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
public function run(string $eventCode,array $elements, array $options = []) |
33
|
|
|
{ |
34
|
|
View Code Duplication |
if(isset($elements['updated'])){ |
|
|
|
|
35
|
|
|
foreach ($elements['updated'] as $element){ |
36
|
|
|
if(!is_array($element)){ |
37
|
|
|
$element = [$element]; |
38
|
|
|
} |
39
|
|
|
$this->updateUpdateListFile($eventCode,$element); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
View Code Duplication |
if(isset($elements['deleted'])){ |
|
|
|
|
43
|
|
|
foreach ($elements['deleted'] as $element){ |
44
|
|
|
if(!is_array($element)){ |
45
|
|
|
$element = [$element]; |
46
|
|
|
} |
47
|
|
|
$this->updateDeleteListFile($eventCode,$element); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param sring $filePath |
55
|
|
|
* @param array $element |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
private function updateFile($filePath,array $element){ |
59
|
|
|
$content = FileChangesHelper::getJsonContentFromFile($filePath); |
60
|
|
|
$content[] = $element; |
61
|
|
|
FileChangesHelper::writeFile($filePath,json_encode($content)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $eventCode |
66
|
|
|
* @param array $element |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
|
|
private function updateUpdateListFile($eventCode,array $element){ |
70
|
|
|
FileChangesHelper::createDirectory($this->dirPathChangings); |
71
|
|
|
$fileName = 'update_'.$eventCode.'.json'; |
72
|
|
|
$this->updateFile($this->dirPathChangings.'/'.$fileName,$element); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $eventCode |
77
|
|
|
* @param array $element |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
private function updateDeleteListFile($eventCode,array $element){ |
81
|
|
|
FileChangesHelper::createDirectory($this->dirPathChangings); |
82
|
|
|
$fileName = 'delete_'.$eventCode.'.json'; |
83
|
|
|
$this->updateFile($this->dirPathChangings.'/'.$fileName,$element); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.