ChangingsToFileListeners::run()   B
last analyzed

Complexity

Conditions 7
Paths 8

Size

Total Lines 19

Duplication

Lines 16
Ratio 84.21 %

Importance

Changes 0
Metric Value
cc 7
nc 8
nop 3
dl 16
loc 19
rs 8.8333
c 0
b 0
f 0
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'])){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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'])){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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);
0 ignored issues
show
Documentation introduced by
$this->dirPathChangings . '/' . $fileName is of type string, but the function expects a object<Wabel\CertainAPI\Listeners\sring>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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);
0 ignored issues
show
Documentation introduced by
$this->dirPathChangings . '/' . $fileName is of type string, but the function expects a object<Wabel\CertainAPI\Listeners\sring>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
84
    }
85
}