Passed
Push — develop ( bb4de3...f6c43e )
by Benjamin
06:52
created

Plugin::getSettings()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 9
nop 0
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link      https://dukt.net/twitter/
4
 * @copyright Copyright (c) 2018, Dukt
5
 * @license   https://github.com/dukt/twitter/blob/master/LICENSE.md
6
 */
7
8
namespace dukt\twitter;
9
10
use Craft;
11
use craft\events\RegisterCacheOptionsEvent;
12
use craft\events\RegisterComponentTypesEvent;
13
use craft\events\RegisterUrlRulesEvent;
14
use craft\helpers\UrlHelper;
15
use craft\services\Dashboard;
16
use craft\services\Fields;
17
use craft\web\twig\variables\CraftVariable;
18
use craft\web\UrlManager;
19
use craft\utilities\ClearCaches;
20
use dukt\twitter\base\PluginTrait;
21
use dukt\twitter\fields\Tweet as TweetField;
22
use dukt\twitter\models\Settings;
23
use dukt\twitter\widgets\SearchWidget;
24
use dukt\twitter\web\twig\Extension;
25
use dukt\twitter\web\twig\variables\TwitterVariable;
26
use yii\base\Event;
27
28
/**
29
 * Twitter Plugin
30
 *
31
 * @author Dukt <[email protected]>
32
 * @since  3.0
33
 */
34
class Plugin extends \craft\base\Plugin
35
{
36
    // Traits
37
    // =========================================================================
38
39
    use PluginTrait;
0 ignored issues
show
introduced by
The trait dukt\twitter\base\PluginTrait requires some properties which are not provided by dukt\twitter\Plugin: $oauthConsumerKey, $oauthConsumerSecret
Loading history...
40
41
    // Properties
42
    // =========================================================================
43
44
    /**
45
     * @var bool
46
     */
47
    public $hasCpSettings = true;
48
49
    /**
50
     * @var \dukt\twitter\Plugin The plugin instance.
51
     */
52
    public static $plugin;
53
54
    // Public Methods
55
    // =========================================================================
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function init()
61
    {
62
        parent::init();
63
64
65
        // Components
66
67
        $this->setComponents([
68
            'twitter' => \dukt\twitter\services\Twitter::class,
69
            'api' => \dukt\twitter\services\Api::class,
70
            'cache' => \dukt\twitter\services\Cache::class,
71
            'oauth' => \dukt\twitter\services\Oauth::class,
72
            'publish' => \dukt\twitter\services\Publish::class,
73
        ]);
74
75
76
        // Events
77
78
        Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function(RegisterUrlRulesEvent $event) {
79
            $rules = [
80
                'twitter/settings' => 'twitter/settings/index',
81
                'twitter/settings/oauth' => 'twitter/settings/oauth',
82
            ];
83
84
            $event->rules = array_merge($event->rules, $rules);
85
        });
86
87
        Event::on(Dashboard::class, Dashboard::EVENT_REGISTER_WIDGET_TYPES, function(RegisterComponentTypesEvent $event) {
88
            $event->types[] = SearchWidget::class;
89
        });
90
91
        Event::on(Fields::class, Fields::EVENT_REGISTER_FIELD_TYPES, function(RegisterComponentTypesEvent $event) {
92
            $event->types[] = TweetField::class;
93
        });
94
95
        Event::on(ClearCaches::class, ClearCaches::EVENT_REGISTER_CACHE_OPTIONS, function(RegisterCacheOptionsEvent $event) {
96
            $event->options[] = [
97
                'key' => 'twitter-caches',
98
                'label' => Craft::t('twitter', 'Twitter caches'),
99
                'action' => Craft::$app->path->getRuntimePath().'/twitter'
100
            ];
101
        });
102
103
        Event::on(CraftVariable::class, CraftVariable::EVENT_INIT, function(Event $event) {
104
            /** @var CraftVariable $variable */
105
            $variable = $event->sender;
106
            $variable->set('twitter', TwitterVariable::class);
107
        });
108
109
110
        // Twig extension
111
112
        Craft::$app->view->twig->addExtension(new Extension());
113
    }
114
115
    /**
116
     * @inheritdoc
117
     */
118
    public function getSettings()
119
    {
120
        $settings = parent::getSettings();
121
        $configFile = Craft::$app->getConfig()->getConfigFromFile('twitter');
122
123
        if($settings) {
124
            $defaultSettingsModel = new Settings();
125
126
            if(!isset($configFile['cacheDuration'])) {
127
                $settings->cacheDuration = $defaultSettingsModel->cacheDuration;
128
            }
129
130
            if(!isset($configFile['enableCache'])) {
131
                $settings->enableCache = $defaultSettingsModel->enableCache;
132
            }
133
            if(!isset($configFile['searchWidgetExtraQuery'])) {
134
                $settings->searchWidgetExtraQuery = $defaultSettingsModel->searchWidgetExtraQuery;
135
            }
136
        }
137
138
        return $settings;
139
    }
140
141
    // Protected Methods
142
    // =========================================================================
143
144
    /**
145
     * Creates and returns the model used to store the plugin’s settings.
146
     *
147
     * @return \craft\base\Model|null
148
     */
149
    protected function createSettingsModel()
150
    {
151
        return new Settings();
152
    }
153
154
    /**
155
     * Returns the rendered settings HTML, which will be inserted into the content
156
     * block on the settings page.
157
     *
158
     * @return string The rendered settings HTML
159
     */
160
    public function getSettingsResponse()
161
    {
162
        $url = UrlHelper::cpUrl('twitter/settings');
163
164
        Craft::$app->controller->redirect($url);
165
166
        return '';
167
    }
168
}
169