ExchangeModule::init()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 11
rs 10
1
<?php
2
3
namespace carono\exchange1c;
4
5
use carono\exchange1c\helpers\ModuleHelper;
6
use yii\helpers\FileHelper;
7
use yii\helpers\Inflector;
8
use yii\web\IdentityInterface;
9
use Yii;
10
11
/**
12
 * exchange module definition class
13
 */
14
class ExchangeModule extends \yii\base\Module
15
{
16
    /**
17
     * @inheritdoc
18
     */
19
    public $controllerNamespace = 'carono\exchange1c\controllers';
20
    /**
21
     * Модель продукта
22
     *
23
     * @var \carono\exchange1c\interfaces\ProductInterface
24
     */
25
    public $productClass;
26
    /**
27
     * Модель предложения
28
     *
29
     * @var \carono\exchange1c\interfaces\OfferInterface
30
     */
31
    public $offerClass;
32
    /**
33
     * Модель документа
34
     *
35
     * @var \carono\exchange1c\interfaces\DocumentInterface
36
     */
37
    public $documentClass;
38
    /**
39
     * Модель группы продукта
40
     *
41
     * @var \carono\exchange1c\interfaces\GroupInterface
42
     */
43
    public $groupClass;
44
    /**
45
     * Модель пользователя
46
     *
47
     * @var \carono\exchange1c\interfaces\PartnerInterface
48
     */
49
    public $partnerClass;
50
    /**
51
     * Модель склада
52
     *
53
     * @var \carono\exchange1c\interfaces\WarehouseInterface
54
     */
55
    public $warehouseClass;
56
    /**
57
     * Обмен документами
58
     *
59
     * @var bool
60
     */
61
    public $exchangeDocuments = false;
62
    /**
63
     * Режим отладки - сохраняем xml файлы в runtime
64
     *
65
     * @var bool
66
     */
67
    public $debug = false;
68
    /**
69
     * При обмене используем архиватор, если расширения нет, то зачение не учитывается
70
     *
71
     * @var bool
72
     */
73
    public $useZip = true;
74
    /**
75
     * Папка, где будут сохранятся временные файлы
76
     *
77
     * @var string
78
     */
79
    public $tmpDir = '@runtime/1c_exchange';
80
    /**
81
     * При сохранении товара, используем валидацию или нет
82
     *
83
     * @var bool
84
     */
85
    public $validateModelOnSave = false;
86
    /**
87
     * Установка лимита выполнения скрипта
88
     *
89
     * @var int
90
     */
91
    public $timeLimit = 1800;
92
    /**
93
     * Установка лимита памяти скрипта
94
     *
95
     * @var mixed
96
     */
97
    public $memoryLimit;
98
    /**
99
     * Автоматическая установка правила для ссылки /1c_exchange.php
100
     *
101
     * @var bool
102
     */
103
    public $bootstrapUrlRule = true;
104
105
    /**
106
     * @var bool Добавлять правило в конец
107
     */
108
    public $appendRule = false;
109
110
    public $redactorModuleName = 'carono-exchange-redactor';
111
    /**
112
     * Функция авторизации пользователя
113
     * function ($login, $password): \yii\web\IdentityInterface|null
114
     *
115
     * @var \Closure
116
     */
117
    public $auth;
118
119
    private function loadRedactorModule()
120
    {
121
        $redactorClass = 'yii\redactor\widgets\Redactor';
122
        $moduleRedactorName = $this->redactorModuleName;
123
        if (class_exists($redactorClass) && !Yii::$app->getModule($moduleRedactorName)) {
124
            $routeName = Inflector::camel2id($moduleRedactorName);
125
            \Yii::$app->setModule($moduleRedactorName, [
126
                'class' => 'yii\redactor\RedactorModule',
127
                'uploadDir' => '@vendor/carono/yii2-1c-exchange/files/articles',
128
                'imageUploadRoute' => ["/$routeName/upload/image"],
129
                'fileUploadRoute' => ["/$routeName/upload/file"],
130
                'imageManagerJsonRoute' => ["/$routeName/upload/image-json"],
131
                'fileManagerJsonRoute' => ["/$routeName/upload/file-json"],
132
                'imageAllowExtensions' => ['jpg', 'png', 'gif'],
133
                'on beforeAction' => function () use ($moduleRedactorName) {
134
                    $redactor = \Yii::$app->getModule($moduleRedactorName);
135
                    $redactor->uploadUrl = '../file/article?file=';
136
                    \Yii::$app->setModule($moduleRedactorName, $redactor);
137
                }
138
            ]);
139
        }
140
    }
141
142
    /**
143
     * @return null|\yii\base\Module
144
     */
145
    public function getRedactor()
146
    {
147
        return Yii::$app->getModule($this->redactorModuleName);
148
    }
149
150
    /**
151
     * @inheritdoc
152
     */
153
    public function init()
154
    {
155
        if (!isset(\Yii::$app->i18n->translations['models'])) {
156
            \Yii::$app->i18n->translations['models'] = [
157
                'class' => 'yii\i18n\PhpMessageSource',
158
                'basePath' => '@app/messages',
159
                'sourceLanguage' => 'en',
160
            ];
161
        }
162
        $this->loadRedactorModule();
163
        parent::init();
164
    }
165
166
    public function getTmpDir($part = null)
167
    {
168
        $dir = \Yii::getAlias($this->tmpDir);
169
        if (!is_dir($dir)) {
170
            FileHelper::createDirectory($dir, 0777, true);
171
        }
172
        return $dir . ($part ? DIRECTORY_SEPARATOR . trim($part, '/\\') : '');
173
    }
174
175
    /**
176
     * @param $login
177
     * @param $password
178
     * @return null|IdentityInterface
179
     */
180
    public function auth($login, $password)
181
    {
182
        /**
183
         * @var $class \yii\web\IdentityInterface
184
         * @var IdentityInterface $user
185
         */
186
        $class = \Yii::$app->user->identityClass;
187
        if (method_exists($class, 'findByUsername')) {
188
            $user = $class::findByUsername($login);
189
            if ($user && method_exists($user, 'validatePassword') && $user->validatePassword($password)) {
190
                return $user;
191
            }
192
        }
193
        return null;
194
    }
195
}