Passed
Pull Request — master (#7)
by Insolita
01:42
created

Module   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 85
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A bootstrap() 0 17 2
A init() 0 4 1
A t() 0 3 1
A registerTranslations() 0 10 2
1
<?php
2
/**
3
 * @link https://github.com/zhuravljov/yii2-queue-monitor
4
 * @copyright Copyright (c) 2017 Roman Zhuravlev
5
 * @license http://opensource.org/licenses/BSD-3-Clause
6
 */
7
8
namespace zhuravljov\yii\queue\monitor;
9
10
use yii\base\BootstrapInterface;
11
use yii\base\InvalidConfigException;
12
use yii\i18n\PhpMessageSource;
13
use yii\web\Application as WebApplication;
14
use yii\web\GroupUrlRule;
15
16
/**
17
 * Web Module
18
 *
19
 * @property \yii\i18n\Formatter $formatter
20
 *
21
 * @author Roman Zhuravlev <[email protected]>
22
 */
23
class Module extends \yii\base\Module implements BootstrapInterface
24
{
25
    /**
26
     * @var bool
27
     */
28
    public $canPushAgain = false;
29
    /**
30
     * @var bool
31
     */
32
    public $canExecStop = false;
33
    /**
34
     * @var bool
35
     */
36
    public $canWorkerStop = false;
37
    /**
38
     * @inheritdoc
39
     */
40
    public $layout = 'main';
41
    /**
42
     * @inheritdoc
43
     */
44
    public $controllerNamespace = 'zhuravljov\yii\queue\monitor\controllers';
45
    /**
46
     * @inheritdoc
47
     */
48
    public $defaultRoute = 'job/index';
49
    
50
    public function init()
51
    {
52
        parent::init();
53
        $this->registerTranslations();
54
    }
55
    
56
    /**
57
     * @inheritdoc
58
     */
59
    public function bootstrap($app)
60
    {
61
        if ($app instanceof WebApplication) {
62
            $app->urlManager->addRules([[
63
                'class' => GroupUrlRule::class,
64
                'prefix' => $this->id,
65
                'rules' => [
66
                    'jobs' => 'job/index',
67
                    'job/<id:\d+>/<action\w+>' => 'job/view-<action>',
68
                    'workers' => 'worker/index',
69
                    '<controller:\w+>/<id:\d+>' => '<controller>/view',
70
                    '<controller:\w+>/<action\w+>/<id:\d+>' => '<controller>/<action>',
71
                    '<controller:\w+>/<action\w+>' => '<controller>/<action>',
72
                ],
73
            ]], false);
74
        } else {
75
            throw new InvalidConfigException('The module must be used for web application only.');
76
        }
77
    }
78
    
79
    private function registerTranslations()
80
    {
81
        if (!isset(\Yii::$app->i18n->translations['queue-monitor/*'])) {
82
            \Yii::$app->i18n->translations['queue-monitor/*'] = [
83
                'class' => PhpMessageSource::class,
84
                'sourceLanguage' => 'en-US',
85
                'basePath' => '@zhuravljov/yii/queue/monitor/messages',
86
                'fileMap' => [
87
                    'queue-monitor/main' => 'main.php',
88
                    'queue-monitor/notice' => 'notice.php',
89
                ],
90
    
91
            ];
92
        }
93
    }
94
    
95
    /**
96
     * Module translator.
97
     *
98
     * @param       $category
99
     * @param       $message
100
     * @param array $params
101
     * @param null  $language
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $language is correct as it would always require null to be passed?
Loading history...
102
     *
103
     * @return string
104
     */
105
    public static function t($category, $message, $params = [], $language = null)
106
    {
107
        return \Yii::t('queue-monitor/' . $category, $message, $params, $language);
108
    }
109
}
110