Passed
Push — master ( c780ff...99d842 )
by Aleksandr
02:09
created

ExchangeModule::loadRedactorModule()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 2
nop 0
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace carono\exchange1c;
4
5
use carono\exchange1c\helpers\ModuleHelper;
6
use yii\helpers\FileHelper;
7
use yii\web\IdentityInterface;
8
use Yii;
9
/**
10
 * exchange module definition class
11
 */
12
class ExchangeModule extends \yii\base\Module
13
{
14
    /**
15
     * @inheritdoc
16
     */
17
    public $controllerNamespace = 'carono\exchange1c\controllers';
18
    /**
19
     * @var \carono\exchange1c\interfaces\ProductInterface
20
     */
21
    public $productClass;
22
    /**
23
     * @var \carono\exchange1c\interfaces\OfferInterface
24
     */
25
    public $offerClass;
26
    /**
27
     * @var \carono\exchange1c\interfaces\DocumentInterface
28
     */
29
    public $documentClass;
30
    /**
31
     * @var \carono\exchange1c\interfaces\GroupInterface
32
     */
33
    public $groupClass;
34
    /**
35
     * @var \carono\exchange1c\interfaces\PartnerInterface
36
     */
37
    public $partnerClass;
38
    /**
39
     * @var \carono\exchange1c\interfaces\WarehouseInterface
40
     */
41
    public $warehouseClass;
42
43
    /**
44
     * Обмен документами
45
     *
46
     * @var bool
47
     */
48
    public $exchangeDocuments = false;
49
    /**
50
     * Режим отладки - сохраняем xml файлы в runtime
51
     *
52
     * @var bool
53
     */
54
    public $debug = false;
55
    /**
56
     * При обмене используем архиватор, если расширения нет, то зачение не учитывается
57
     *
58
     * @var bool
59
     */
60
    public $useZip = true;
61
    public $tmpDir = '@runtime/1c_exchange';
62
    /**
63
     * При сохранении товара, используем валидацию или нет
64
     *
65
     * @var bool
66
     */
67
    public $validateModelOnSave = false;
68
    public $timeLimit = 1800;
69
    public $memoryLimit = null;
70
    public $bootstrapUrlRule = true;
71
    public $auth;
72
73
    private function loadRedactorModule()
74
    {
75
        $redactorClass = 'yii\redactor\widgets\Redactor';
76
        $moduleRedactorName = 'carono-exchange-redactor';
77
        if (class_exists($redactorClass) && !Yii::$app->getModule($moduleRedactorName)) {
78
            \Yii::$app->setModule($moduleRedactorName, [
79
                'class' => 'yii\redactor\RedactorModule',
80
                'uploadDir' => '@vendor/carono/yii2-1c-exchange/files/articles',
81
                'imageAllowExtensions' => ['jpg', 'png', 'gif'],
82
                'on beforeAction' => function () use ($moduleRedactorName) {
83
                    $path = ModuleHelper::getModuleNameByClass('carono\exchange1c\ExchangeModule', 'exchange');
84
                    $redactor = \Yii::$app->getModule($moduleRedactorName);
85
                    $redactor->uploadUrl = "/$path/file/article?file=";
86
                    \Yii::$app->setModule($moduleRedactorName, $redactor);
87
                }
88
            ]);
89
        }
90
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95
    public function init()
96
    {
97
        if (!isset(\Yii::$app->i18n->translations['models'])) {
98
            \Yii::$app->i18n->translations['models'] = [
99
                'class' => 'yii\i18n\PhpMessageSource',
100
                'basePath' => '@app/messages',
101
                'sourceLanguage' => 'en',
102
            ];
103
        }
104
        $this->loadRedactorModule();
105
        parent::init();
106
    }
107
108
    public function getTmpDir($part = null)
109
    {
110
        $dir = \Yii::getAlias($this->tmpDir);
111
        if (!is_dir($dir)) {
112
            FileHelper::createDirectory($dir, 0777, true);
113
        }
114
        return $dir . ($part ? DIRECTORY_SEPARATOR . trim($part, '/\\') : '');
115
    }
116
117
    /**
118
     * @param $login
119
     * @param $password
120
     * @return null|IdentityInterface
121
     */
122
    public function auth($login, $password)
123
    {
124
        /**
125
         * @var $class \yii\web\IdentityInterface
126
         * @var IdentityInterface $user
127
         */
128
        $class = \Yii::$app->user->identityClass;
129
        if (method_exists($class, 'findByUsername')) {
130
            $user = $class::findByUsername($login);
131
            if ($user && method_exists($user, 'validatePassword') && $user->validatePassword($password)) {
132
                return $user;
133
            }
134
        }
135
        return null;
136
    }
137
}