Registryfile::getInstance()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace XoopsModules\Oledrion;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
15
/**
16
 * oledrion
17
 *
18
 * @copyright   {@link https://xoops.org/ XOOPS Project}
19
 * @license     {@link http://www.fsf.org/copyleft/gpl.html GNU public license}
20
 * @author      Hervé Thouzard (http://www.herve-thouzard.com/)
21
 */
22
23
use XoopsModules\Oledrion;
24
25
/**
26
 * Management of text files used to display messages to users on certain pages
27
 */
28
class Registryfile
29
{
30
    public $filename; // File name to process
31
32
    /**
33
     * Access the only instance of this class
34
     *
35
     * @return Registryfile
36
     */
37
    public static function getInstance()
38
    {
39
        static $instance;
40
        if (null === $instance) {
41
            $instance = new static();
42
        }
43
44
        return $instance;
45
    }
46
47
    /**
48
     * Registryfile constructor.
49
     * @param null $fichier
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fichier is correct as it would always require null to be passed?
Loading history...
50
     */
51
    public function __construct($fichier = null)
52
    {
53
        $this->setfile($fichier);
54
    }
55
56
    /**
57
     * @param null $fichier
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fichier is correct as it would always require null to be passed?
Loading history...
58
     */
59
    public function setfile($fichier = null)
60
    {
61
        if ($fichier) {
0 ignored issues
show
introduced by
$fichier is of type null, thus it always evaluated to false.
Loading history...
62
            $this->filename = OLEDRION_TEXT_PATH . $fichier;
63
        }
64
    }
65
66
    /**
67
     * @param  null $fichier
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fichier is correct as it would always require null to be passed?
Loading history...
68
     * @return string
69
     */
70
    public function getfile($fichier = null)
71
    {
72
        $fw = '';
73
        if (!$fichier) {
0 ignored issues
show
introduced by
$fichier is of type null, thus it always evaluated to false.
Loading history...
74
            $fw = $this->filename;
75
        } else {
76
            $fw = OLEDRION_TEXT_PATH . $fichier;
77
        }
78
79
        if (file_exists($fw)) {
80
            return file_get_contents($fw);
81
        }
82
83
        return '';
84
    }
85
86
    /**
87
     * @param       $content
88
     * @param  null $fichier
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fichier is correct as it would always require null to be passed?
Loading history...
89
     * @return bool
90
     */
91
    public function savefile($content, $fichier = null)
92
    {
93
        $fw = '';
94
        if (!$fichier) {
0 ignored issues
show
introduced by
$fichier is of type null, thus it always evaluated to false.
Loading history...
95
            $fw = $this->filename;
96
        } else {
97
            $fw = OLEDRION_TEXT_PATH . $fichier;
98
        }
99
        if (file_exists($fw)) {
100
            if (false === @unlink($fw)) {
101
                throw new \RuntimeException('The file ' . $fw . ' could not be deleted.');
102
            }
103
        }
104
        $fp = fopen($fw, 'wb') || die('Error, impossible to create the file ' . $this->filename);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
105
        fwrite($fp, $content);
0 ignored issues
show
Bug introduced by
$fp of type boolean is incompatible with the type resource expected by parameter $stream of fwrite(). ( Ignorable by Annotation )

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

105
        fwrite(/** @scrutinizer ignore-type */ $fp, $content);
Loading history...
106
        fclose($fp);
0 ignored issues
show
Bug introduced by
$fp of type boolean is incompatible with the type resource expected by parameter $stream of fclose(). ( Ignorable by Annotation )

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

106
        fclose(/** @scrutinizer ignore-type */ $fp);
Loading history...
107
108
        return true;
109
    }
110
}
111