HasEditFileMode   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 155
rs 10
c 0
b 0
f 0
wmc 16

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getContentPatch() 0 3 1
A getFilename() 0 3 1
A setPath() 0 5 1
A getStartPointForPatch() 0 3 1
A setFilename() 0 5 1
A addPatchContentToFile() 0 19 4
A removePatchContentFromFile() 0 10 3
A setContentPatch() 0 5 1
A getFilePath() 0 3 1
A setStartPointForPatch() 0 5 1
A getPath() 0 3 1
1
<?php
2
3
/*
4
 * Copyright (c) 2017 Salah Alkhwlani <[email protected]>
5
 *
6
 * For the full copyright and license information, please view
7
 * the LICENSE file that was distributed with this source code.
8
 */
9
10
namespace Yemenifree\WpSecurity\Traits;
11
12
trait HasEditFileMode
13
{
14
    /** @var string path dir file */
15
    protected $path;
16
    /** @var string content file */
17
    protected $content_patch;
18
    /** @var string file name */
19
    protected $filename;
20
21
    /** @var string|bool */
22
    protected $startPointForPatch = false;
23
24
    /**
25
     *  remove content from file.
26
     */
27
    protected function removePatchContentFromFile()
28
    {
29
        if (!\file_exists($this->getFilePath())) {
30
            return;
31
        }
32
        $content = \file_get_contents($this->getFilePath());
33
        if (\strpos($content, $this->getContentPatch()) === false) {
34
            return;
35
        }
36
        \file_put_contents($this->getFilePath(), \str_replace($this->getContentPatch(), '', $content));
37
    }
38
39
    /**
40
     * get full path of file.
41
     *
42
     * @return string
43
     */
44
    public function getFilePath()
45
    {
46
        return \rtrim($this->getPath(), '/') . '/' . $this->getFilename();
47
    }
48
49
    /**
50
     * get path of file.
51
     *
52
     * @return string
53
     */
54
    public function getPath()
55
    {
56
        return $this->path;
57
    }
58
59
    /**
60
     * set path of file.
61
     *
62
     * @param string $path
63
     *
64
     * @return HasEditFileMode
65
     */
66
    public function setPath($path)
67
    {
68
        $this->path = $path;
69
70
        return $this;
71
    }
72
73
    /**
74
     * get filename.
75
     *
76
     * @return string
77
     */
78
    public function getFilename()
79
    {
80
        return $this->filename;
81
    }
82
83
    /**
84
     * set filename.
85
     *
86
     * @param string $filename
87
     *
88
     * @return HasEditFileMode
89
     */
90
    public function setFilename($filename)
91
    {
92
        $this->filename = $filename;
93
94
        return $this;
95
    }
96
97
    /**
98
     * get content patch.
99
     *
100
     * @return string
101
     */
102
    public function getContentPatch()
103
    {
104
        return $this->content_patch;
105
    }
106
107
    /**
108
     * set content want to patch.
109
     *
110
     * @param string $content_patch
111
     *
112
     * @return HasEditFileMode
113
     */
114
    public function setContentPatch($content_patch)
115
    {
116
        $this->content_patch = $content_patch;
117
118
        return $this;
119
    }
120
121
    /**
122
     *  add content of htaccess rules from file.
123
     */
124
    protected function addPatchContentToFile()
125
    {
126
        if (!\file_exists($this->getFilePath())) {
127
            \touch($this->getFilePath());
128
        }
129
130
        $content = \file_get_contents($this->getFilePath());
131
132
        if (\strpos($content, $this->getContentPatch()) !== false) {
133
            return;
134
        }
135
136
        if ($this->getStartPointForPatch()) {
137
            $content = \str_replace($this->getStartPointForPatch(), $this->getStartPointForPatch() . $this->getContentPatch(), $content);
138
        } else {
139
            $content = $this->getContentPatch() . $content;
140
        }
141
142
        \file_put_contents($this->getFilePath(), $content);
143
    }
144
145
    /**
146
     * get start point for patch content.
147
     *
148
     * @return string|bool
149
     */
150
    public function getStartPointForPatch()
151
    {
152
        return $this->startPointForPatch;
153
    }
154
155
    /**
156
     * set start point for patch content.
157
     *
158
     * @param string|bool $startPointForPatch
159
     *
160
     * @return HasEditFileMode
161
     */
162
    public function setStartPointForPatch($startPointForPatch)
163
    {
164
        $this->startPointForPatch = $startPointForPatch;
165
166
        return $this;
167
    }
168
}
169