FileStorage::getFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * @link https://github.com/lav45/yii2-settings
4
 * @copyright Copyright (c) 2016 LAV45
5
 * @author Aleksey Loban <[email protected]>
6
 * @license http://opensource.org/licenses/BSD-3-Clause
7
 */
8
9
namespace lav45\settings\storage;
10
11
use Yii;
12
use yii\base\BaseObject;
13
use yii\helpers\FileHelper;
14
15
/**
16
 * Class FileStorage
17
 * @package lav45\settings\storage
18
 */
19
class FileStorage extends BaseObject implements StorageInterface
20
{
21
    /**
22
     * @var string the directory to store settings files. You may use path alias here.
23
     * If not set, it will use the "settings" subdirectory under the application runtime path.
24
     */
25
    public $path = '@runtime/settings';
26
    /**
27
     * @var int the permission to be set for newly created cache files.
28
     * This value will be used by PHP chmod() function. No umask will be applied.
29
     * If not set, the permission will be determined by the current environment.
30
     */
31
    public $fileMode;
32
    /**
33
     * @var int the permission to be set for newly created directories.
34
     * This value will be used by PHP chmod() function. No umask will be applied.
35
     * Defaults to 0775, meaning the directory is read-writable by owner, but read-only for other users.
36
     */
37
    public $dirMode = 0755;
38
    /**
39
     * @var string settings file suffix. Defaults to '.bin'.
40
     */
41
    public $fileSuffix = '.bin';
42
43
    /**
44
     * Initializes this component by ensuring the existence of the settings path.
45
     */
46 5
    public function init()
47
    {
48 5
        parent::init();
49 5
        $this->path = Yii::getAlias($this->path);
0 ignored issues
show
Documentation Bug introduced by
It seems like Yii::getAlias($this->path) can also be of type false. However, the property $path is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
50 5
        FileHelper::createDirectory($this->path, $this->dirMode, true);
0 ignored issues
show
Bug introduced by
It seems like $this->path can also be of type false; however, parameter $path of yii\helpers\BaseFileHelper::createDirectory() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

50
        FileHelper::createDirectory(/** @scrutinizer ignore-type */ $this->path, $this->dirMode, true);
Loading history...
51
    }
52
53
    /**
54
     * @param string $key
55
     * @return false|null|string
56
     */
57 1
    public function getValue($key)
58
    {
59 1
        $file = $this->getFile($key);
60
61 1
        $fp = @fopen($file, 'rb');
62 1
        if ($fp !== false) {
63 1
            @flock($fp, LOCK_SH);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for flock(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

63
            /** @scrutinizer ignore-unhandled */ @flock($fp, LOCK_SH);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
64 1
            $value = @stream_get_contents($fp);
65 1
            @flock($fp, LOCK_UN);
66 1
            @fclose($fp);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for fclose(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

66
            /** @scrutinizer ignore-unhandled */ @fclose($fp);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
67 1
            return $value;
68
        }
69
70 1
        return false;
71
    }
72
73
    /**
74
     * @param string $key
75
     * @param string $value
76
     * @return bool
77
     */
78 5
    public function setValue($key, $value)
79
    {
80 5
        $file = $this->getFile($key);
81
82 5
        if (@file_put_contents($file, $value, LOCK_EX) !== false) {
83 4
            if ($this->fileMode !== null) {
84 1
                @chmod($file, $this->fileMode);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for chmod(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

84
                /** @scrutinizer ignore-unhandled */ @chmod($file, $this->fileMode);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
85
            }
86 4
            return true;
87
        }
88 1
        $error = error_get_last();
89 1
        Yii::error("Unable to write file '{$file}': {$error['message']}");
90 1
        return false;
91
    }
92
93
    /**
94
     * @param string $key
95
     * @return bool
96
     */
97 4
    public function deleteValue($key)
98
    {
99 4
        return @unlink($this->getFile($key));
100
    }
101
102
    /**
103
     * Returns the storage file path given the key.
104
     * @param string $key
105
     * @return string the file path
106
     */
107 5
    protected function getFile($key)
108
    {
109 5
        return $this->path . DIRECTORY_SEPARATOR . $key . $this->fileSuffix;
110
    }
111
}