Module::module()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Module.php
4
 * @author Revin Roman http://phptime.ru
5
 */
6
7
namespace rmrevin\yii\module\File;
8
9
use yii\base\InvalidConfigException;
10
use yii\helpers\FileHelper;
11
12
/**
13
 * Class Module
14
 * @package rmrevin\yii\module\File
15
 */
16
class Module extends \yii\base\Module
17
{
18
19
    /** @var string */
20
    public $upload_alias = '@app/web/upload';
21
22
    /** @var string */
23
    public $upload_path = null;
24
25
    /** @var string */
26
    public $upload_web_alias = '/upload';
27
28
    /** @var string */
29
    public $upload_web_path = null;
30
31
    /** @var string */
32
    public $storage_alias = '@app/web/storage';
33
34
    /** @var string */
35
    public $storage_path = null;
36
37
    /** @var string */
38
    public $storage_web_alias = '/storage';
39
40
    /** @var string */
41
    public $storage_web_path = null;
42
43
    /** @var int */
44
    public $max_upload_file_size = 10; // megabytes
45
46
    /** @var string */
47
    public $no_image_alias = '@vendor/rmrevin/yii2-file/assets/no-image.png';
48
49
    /**
50
     * @inheritdoc
51
     */
52 15
    public function init()
53
    {
54 15
        parent::init();
55
56 15
        $this->checkIniSizeParam('upload_max_filesize');
57
58 15
        $this->checkIniSizeParam('post_max_size');
59
60 15
        $this->reinitAliases();
61
62 15
        if (empty($this->upload_path)) {
63 1
            throw new InvalidConfigException(\Yii::t('service-file', 'Unable to determine the path to the upload directory (FileService::$upload_path).'));
64
        }
65
66 15
        if (empty($this->storage_path)) {
67 1
            throw new InvalidConfigException(\Yii::t('service-file', 'Unable to determine the path to the storage directory (FileService::$storage_path).'));
68
        }
69
70 15
        $this->createDirectory($this->upload_path);
71
72 15
        $this->createDirectory($this->storage_path);
73
74 15
        if (!is_readable($this->upload_path)) {
75 1
            throw new \RuntimeException(\Yii::t('service-file', 'Upload directory not available for reading (FileService::$upload_path).'));
76
        }
77
78 15
        if (!is_writable($this->upload_path)) {
79 1
            throw new \RuntimeException(\Yii::t('service-file', 'Upload directory not available for writing (FileService::$upload_path).'));
80
        }
81
82 15
        if (!is_readable($this->storage_path)) {
83 1
            throw new \RuntimeException(\Yii::t('service-file', 'Storage directory not available for reading (FileService::$storage_path).'));
84
        }
85
86 15
        if (!is_writable($this->storage_path)) {
87 1
            throw new \RuntimeException(\Yii::t('service-file', 'Storage directory not available for writing (FileService::$storage_path).'));
88
        }
89 15
    }
90
91 15
    public function reinitAliases()
92
    {
93 15
        $this->storage_path = \Yii::getAlias($this->storage_alias);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Yii::getAlias($this->storage_alias) can also be of type boolean. However, the property $storage_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...
94 15
        $this->storage_web_path = \Yii::getAlias($this->storage_web_alias);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Yii::getAlias($this->storage_web_alias) can also be of type boolean. However, the property $storage_web_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...
95 15
        $this->upload_web_path = \Yii::getAlias($this->upload_web_alias);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Yii::getAlias($this->upload_web_alias) can also be of type boolean. However, the property $upload_web_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...
96 15
        $this->upload_path = \Yii::getAlias($this->upload_alias);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Yii::getAlias($this->upload_alias) can also be of type boolean. However, the property $upload_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...
97 15
    }
98
99
    /**
100
     * @return static
101
     */
102 12
    public static function module()
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
103
    {
104 12
        return \Yii::$app->getModule(static::MODULE);
105
    }
106
107
    /**
108
     * @param string $path
109
     */
110 15
    private function createDirectory($path)
111
    {
112 15
        if (!file_exists($path) || !is_dir($path)) {
113 15
            FileHelper::createDirectory($path);
114 15
        }
115 15
    }
116
117
    /**
118
     * @param string $param
119
     */
120 15
    private function checkIniSizeParam($param)
121
    {
122 15
        if ((int)ini_get($param) < $this->max_upload_file_size) {
123 15
            \Yii::warning(sprintf('The parameter `%s` in php.ini must be equal to`%s`', $param,
124 15
                $this->max_upload_file_size . 'M'), __METHOD__);
125 15
        }
126 15
    }
127
128
    const MODULE = 'file';
129
}