AddFileToModule::setRootDirForModule()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 1
c 1
b 1
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * adds or replaces a file
4
 * in a git hub module
5
 * in a module...
6
 *
7
 *
8
 */
9
10
abstract class AddFileToModule extends Object
11
{
12
    protected $gitReplaceArray = array(
13
        '+++long-module-name-goes-here+++' => 'LongModuleName',
14
        '+++medium-module-name-goes-here+++' => 'MediumModuleName',
15
        '+++short-module-name-goes-here+++' => 'ShortModuleName',
16
        '+++module-name-goes-here+++' => 'ShortModuleName',
17
        '+++short-module-name-first-letter-capital+++' => 'ModuleNameFirstLetterCapital'
18
    );
19
20
21
    protected $replaceArray = array(
22
        '+++README_DOCUMENTATION+++' => 'Documentation',
23
        '+++README_SUGGESTED_MODULES+++' => 'SuggestedModules',
24
        '+++README_REQUIREMENTS+++' => 'Requirements',
25
        '+++README_INSTALLATION+++' => 'Installation',
26
        '+++README_AUTHOR+++' => 'Author',
27
        '+++README_ASSISTANCE+++' => 'Assistance',
28
        '+++README_CONTRIBUTING+++' => 'Contributing',
29
        '+++README_CONFIGURATION+++' => 'Configuration'
30
    );
31
32
    /**
33
     * root dir for module
34
     * e.g. /var/www/modules/mymodule
35
     * no final slash
36
     *
37
     * @var string
38
     */
39
    protected $rootDirForModule = '';
40
41
42
    /**
43
     * root dir for module
44
     * e.g.
45
     *  - README.md
46
     *  OR
47
     *  - docs/index.php
48
     *
49
     *
50
     * @var string
51
     */
52
    protected $fileLocation = '';
53
54
    /**
55
     * e.g.
56
     * http://www.mysite.com/myfile.txt
57
     * myfile.txt
58
     * where examples.txt will have the base dir + modulechecks director added to it
59
     * e.g.
60
     * examples/myfile.txt becomes
61
     * /var/www/myproject/modulechecks/examples/myfile.txt
62
     * @var string
63
     */
64
    protected $sourceLocation = 'please set in files that extend ';
65
66
    protected $useCustomisationFile = false;
67
68
    protected $gitObject=null;
69
70
    public function __construct($gitObject)
71
    {
72
        parent::__construct();
73
        $this->gitObject = $gitObject;
74
        $rootDirForModule = $gitObject->Directory();
75
        $this->rootDirForModule = $rootDirForModule;
76
    }
77
78
    public function setRootDirForModule($rootDirForModule)
79
    {
80
        $this->$rootDirForModule = $rootDirForModule;
81
    }
82
83
    public function setSourceLocation($sourceLocation)
84
    {
85
        $this->sourceLocation = $sourceLocation;
86
    }
87
88
    public function setFileLocation($relativeDirAndFileName)
89
    {
90
        $this->fileLocation = $relativeDirAndFileName;
91
    }
92
93
94
    public function run()
95
    {
96
        if (! $this->rootDirForModule) {
97
            user_error('no root dir for module has been set');
98
        }
99
        if (! $this->fileLocation) {
100
            user_error('File location not set');
101
        }
102
        $fileContent = $this->getStandardFile();
103
104
        if ($this->useCustomisationFile) {
105
            $fileContent = $this->customiseStandardFile($fileContent);
106
        }
107
108
        $this->saveFile($fileContent);
109
        if ($fileContent) {
110
            $this->replaceWordsInFile();
111
        }
112
    }
113
114
    /**
115
     * you can either return the string from the
116
     * `$sourceLocation` or you can just have a string here
117
     * that returns the data directly....
118
     *
119
     * @param string $fileContent
120
     *
121
     * @return bool - true on success, false on failure
122
     */
123
    protected function getStandardFile()
124
    {
125
        $isURL = (strpos($this->sourceLocation, '//') !== false);
126
127
        if ($isURL) {
128
            $fullFileName = $this->sourceLocation;
129
        } else {
130
            $fullFileName = Director::baseFolder().'/'.$this->sourceLocation;
131
        }
132
133
        print("<li>$fullFileName</li>");
134
135
        $file = fopen($fullFileName, "r");
136
        if ($file) {
0 ignored issues
show
introduced by
$file is of type false|resource, thus it always evaluated to false.
Loading history...
137
            $fileSize = filesize($fullFileName);
138
139
            if ($fileSize > 0) {
140
                $fileContents = fread($file, filesize($fullFileName));
141
            } else {
142
                $fileContents = "";
143
            }
144
            fclose($file);
145
146
            return $fileContents;
147
        } else {
148
            return false;
149
        }
150
    }
151
152
    /**
153
     * takes the standard file and adds any
154
     * customisations to it from the module
155
     *
156
     * @return bool - true on success, false on failure
157
     */
158
    protected function customiseStandardFile($fileContent)
159
    {
160
        $obj = $this->getCustomisationFile();
161
        $fileContent = $obj->customiseFile($this->fileLocation, $fileContent);
0 ignored issues
show
Unused Code introduced by
The assignment to $fileContent is dead and can be removed.
Loading history...
162
    }
163
164
    /**
165
     * writes the file
166
     *
167
     * @param string $fileContent
168
     *
169
     * @return bool - true on success, false on failure
170
     */
171
    protected function saveFile($fileContent)
172
    {
173
        GeneralMethods::output_to_screen("<li> Adding " . $this->fileLocation . " to module  </li>");
174
175
        /*
176
         * If fileLocation  contains folder, name then need to check
177
         * if folder exists
178
         */
179
        if (strpos($this->fileLocation, '/')!==false) {
180
            $folderPath = substr($this->fileLocation, 0, strrpos($this->fileLocation, '/'));
181
182
            //print_r ($this->rootDirForModule.'/'.$folderPath);
183
184
            if (!file_exists($this->rootDirForModule.'/'.$folderPath)) {
185
                $folder = Filesystem::makeFolder($this->rootDirForModule.'/'.$folderPath);
0 ignored issues
show
Unused Code introduced by
The assignment to $folder is dead and can be removed.
Loading history...
Bug introduced by
Are you sure the assignment to $folder is correct as Filesystem::makeFolder($...le . '/' . $folderPath) targeting Filesystem::makeFolder() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
186
            }
187
        }
188
189
        if (isset($folderPath)  && !file_exists($this->rootDirForModule.'/'.$folderPath)) {
190
            user_error('could not find or create directory ' . $this->rootDirForModule.'/'.$folderPath);
191
        }
192
193
        $fileName = $this->rootDirForModule.'/'.$this->fileLocation;
194
        $this->fileLocation;
195
196
        $file = fopen($fileName, "w");
197
198
        if ($file) {
0 ignored issues
show
introduced by
$file is of type false|resource, thus it always evaluated to false.
Loading history...
199
            $result = fwrite($file, $fileContent);
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
200
            $a = file_exists($fileName);
0 ignored issues
show
Unused Code introduced by
The assignment to $a is dead and can be removed.
Loading history...
201
        } else {
202
            return false;
203
        }
204
    }
205
    /**
206
     *
207
     *
208
     * @return ModuleConfig (instance of ModuleConfigInterface)
209
     */
210
    protected function getCustomisationFile()
211
    {
212
        require_once(
213
            $this->rootDirForModule . '/ssmoduleconfigs/ModuleConfig.php'
214
        );
215
        return Injector::inst()->get('ModuleConfig');
216
    }
217
218
219
    protected function getReadMeComponent($componentName)
220
    {
221
        $temp_dir = GitHubModule::Config()->get('absolute_temp_folder');
222
        $moduleName = $this->gitObject->ModuleName;
223
224
        $fileName = $temp_dir . '/' . $moduleName . '/docs/en/' . strtoupper($componentName) . '.md';
225
226
        set_error_handler(array($this, 'catchFopenWarning'), E_WARNING);
227
        $file = fopen($fileName, 'r');
228
        restore_error_handler();
229
230
        if ($file) {
0 ignored issues
show
introduced by
$file is of type false|resource, thus it always evaluated to false.
Loading history...
231
            $content = fread($file, filesize($filename));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $filename does not exist. Did you maybe mean $fileName?
Loading history...
Unused Code introduced by
The assignment to $content is dead and can be removed.
Loading history...
232
        } else {
233
            $content = "";
234
        }
235
    }
236
237
    /*
238
     *
239
     * */
240
    private function catchFopenWarning($errno, $errstr)
241
    {
242
        //
243
    }
244
245
246
    protected function Configuration()
247
    {
248
        return $this->getReadMeComponent('configuration');
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getReadMeComponent('configuration') targeting AddFileToModule::getReadMeComponent() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
249
    }
250
251
    protected function Contributing()
252
    {
253
        return $this->getReadMeComponent('contributing');
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getReadMeComponent('contributing') targeting AddFileToModule::getReadMeComponent() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
254
    }
255
256
    protected function Documentation()
257
    {
258
        return $this->getReadMeComponent('documentation');
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getReadMeComponent('documentation') targeting AddFileToModule::getReadMeComponent() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
259
    }
260
261
    protected function Requirements()
262
    {
263
        return $this->getReadMeComponent('requirements');
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getReadMeComponent('requirements') targeting AddFileToModule::getReadMeComponent() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
264
    }
265
266
    protected function Installation()
267
    {
268
        return $this->getReadMeComponent('installation');
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getReadMeComponent('installation') targeting AddFileToModule::getReadMeComponent() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
269
    }
270
271
    protected function Author()
272
    {
273
        return $this->getReadMeComponent('author');
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getReadMeComponent('author') targeting AddFileToModule::getReadMeComponent() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
274
    }
275
276
    protected function Assistance()
277
    {
278
        return $this->getReadMeComponent('assistance');
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getReadMeComponent('assistance') targeting AddFileToModule::getReadMeComponent() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
279
    }
280
281
    protected function SuggestedModules()
282
    {
283
        return $this->getReadMeComponent('suggestedmodules');
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getReadMeComponent('suggestedmodules') targeting AddFileToModule::getReadMeComponent() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
284
    }
285
286
287
288
289
    /**
290
     * @param string $file
291
     * @param GitHubModule $gitObject
292
     *
293
     * @return string
294
     */
295
    public function replaceWordsInFile()
296
    {
297
        foreach ($this->gitReplaceArray as $searchTerm => $replaceMethod) {
298
            $fileName = $this->rootDirForModule.'/'.$this->fileLocation;
299
            GeneralMethods::replaceInFile($fileName, $searchTerm, $this->gitObject->$replaceMethod());
300
        }
301
302
        foreach ($this->replaceArray as $searchTerm => $replaceMethod) {
303
            $fileName = $this->rootDirForModule.'/'.$this->fileLocation;
304
            GeneralMethods::replaceInFile($fileName, $searchTerm, $this->$replaceMethod());
305
        }
306
    }
307
308
    /**
309
     *
310
     * @return string
311
     */
312
    public function getFileLocation()
313
    {
314
        return $this->fileLocation;
315
    }
316
317
    /**
318
     * @param string $text
319
     * @return string
320
     */
321
    public function replaceWordsInText($text)
322
    {
323
        $originalText = $text;
0 ignored issues
show
Unused Code introduced by
The assignment to $originalText is dead and can be removed.
Loading history...
324
        foreach ($this->gitReplaceArray as $searchTerm => $replaceMethod) {
325
            $text = str_replace($searchTerm, $this->gitObject->$replaceMethod(), $text);
326
        }
327
328
        foreach ($this->replaceArray as $searchTerm => $replaceMethod) {
329
            $text = str_replace($searchTerm, $this->$replaceMethod(), $text);
330
        }
331
        return $text;
332
    }
333
334
    public function compareWithText($compareText)
335
    {
336
        $fileText = $this->getStandardFile();
337
        $text = $this->replaceWordsInText($fileText);
0 ignored issues
show
Bug introduced by
$fileText of type false is incompatible with the type string expected by parameter $text of AddFileToModule::replaceWordsInText(). ( Ignorable by Annotation )

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

337
        $text = $this->replaceWordsInText(/** @scrutinizer ignore-type */ $fileText);
Loading history...
338
        return (trim($text) == trim($compareText));
339
    }
340
}
341