FileDriver::getFilename()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
namespace HayriCan\IpChecker;
4
5
/**
6
 * Laravel IP Checker
7
 *
8
 * @author    Hayri Can BARÇIN <hayricanbarcin (#) gmail (.) com>
9
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
10
 * @link      https://github.com/HayriCan/laravel-ip-checker
11
 */
12
13
use Carbon\Carbon;
0 ignored issues
show
Bug introduced by
The type Carbon\Carbon was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Illuminate\Support\Facades\File;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\File was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use HayriCan\IpChecker\Contracts\IpCheckerInterface;
16
17
class FileDriver implements IpCheckerInterface
18
{
19
20
    /**
21
     * file path to save the logs
22
     */
23
    protected $path;
24
    protected $ipList = [];
25
26
    public function __construct()
27
    {
28
        $this->path = storage_path(config('ipchecker.filepath'));
0 ignored issues
show
Bug introduced by
The function storage_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
        $this->path = /** @scrutinizer ignore-call */ storage_path(config('ipchecker.filepath'));
Loading history...
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
        $this->path = storage_path(/** @scrutinizer ignore-call */ config('ipchecker.filepath'));
Loading history...
29
    }
30
31
    /**
32
     * @return array
33
     */
34
    public function getIpArray()
35
    {
36
        if (is_dir($this->path)) {
37
            $files = scandir($this->path);
38
39
            foreach ($files as $file) {
40
                if (!is_dir($file)) {
41
                    $lines = file($this->path.DIRECTORY_SEPARATOR.$file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
42
                    foreach ($lines as $line) {
43
                        $contentarr = explode(";", $line);
44
                        array_push($this->ipList, $contentarr[2]);
45
                    }
46
                }
47
            }
48
            $ipList = $this->ipList ?? [];
49
50
            return $ipList;
51
        }
52
53
        return [];
54
    }
55
56
    /**
57
     * @return array|\Illuminate\Support\Collection|mixed
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Collection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
58
     */
59
    public function getIpList()
60
    {
61
        if (is_dir($this->path)) {
62
            $files = scandir($this->path);
63
64
            foreach ($files as $file) {
65
                if (!is_dir($file)) {
66
                    $lines = file($this->path.DIRECTORY_SEPARATOR.$file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
67
                    foreach ($lines as $line) {
68
                        $contentarr = explode(";", $line);
69
                        array_push($this->ipList, $this->mapArrayToModel($contentarr));
70
                    }
71
                }
72
            }
73
            return collect($this->ipList);
0 ignored issues
show
Bug introduced by
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
            return /** @scrutinizer ignore-call */ collect($this->ipList);
Loading history...
74
        }
75
76
        return [];
77
    }
78
79
    /**
80
     * @param $array
81
     * @return bool
82
     */
83
    public function saveIp($array)
84
    {
85
        $array['created_at']=Carbon::now()->toDateTimeString();
86
        $filename = $this->getFilename();
87
88
        $contents = implode(";", $array);
89
90
        File::makeDirectory($this->path, 0777, true, true);
91
92
        File::append(($this->path.DIRECTORY_SEPARATOR.$filename), $contents.PHP_EOL);
93
94
        return true;
95
    }
96
97
    /**
98
     * @param $ipAddress
99
     * @return bool
100
     */
101
    public function deleteIp($ipAddress)
102
    {
103
        if (is_dir($this->path)) {
104
            $files = scandir($this->path);
105
106
            foreach ($files as $file) {
107
                if (!is_dir($file)) {
108
                    $lines = file($this->path.DIRECTORY_SEPARATOR.$file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
109
                    foreach ($lines as $line) {
110
                        if (strpos($line, $ipAddress)){
111
                            $contents = file_get_contents($this->path.DIRECTORY_SEPARATOR.$file);
112
                            $contents = str_replace($line,'',$contents);
113
                            file_put_contents($this->path.DIRECTORY_SEPARATOR.$file,$contents);
114
                            return true;
115
                        }
116
                    }
117
                }
118
            }
119
            return false;
120
        }
121
122
        return false;
123
    }
124
125
    /**
126
     * Helper method for mapping array into models
127
     *
128
     * @param array $data
129
     * @return IpList
130
     */
131
    public function mapArrayToModel(array $data){
132
        $model = new IpList();
133
        $model->group = $data[0];
134
        $model->definition = $data[1];
135
        $model->ip = $data[2];
136
        $model->created_at = Carbon::make($data[3]);
137
        return $model;
138
    }
139
140
    /**
141
     * get log file if defined in constants
142
     *
143
     * @return string
144
     */
145
    public function getFilename()
146
    {
147
        $filename = 'iplist.php';
148
        if (config('ipchecker.filename')){
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

148
        if (/** @scrutinizer ignore-call */ config('ipchecker.filename')){
Loading history...
149
            $filename = config('ipchecker.filename');
150
        }
151
152
        return $filename;
153
    }
154
}
155