Plugin   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 116
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A pluginDetails() 0 10 1
A boot() 0 7 1
A registerComponents() 0 6 1
A registerSettings() 0 12 1
A registerMarkupTags() 0 8 1
A getTimeToRead() 0 10 1
A extendModel() 0 8 1
1
<?php
2
3
namespace GinoPane\BlogTimeToRead;
4
5
use System\Classes\PluginBase;
6
use RainLab\Blog\Models\Post as PostModel;
7
use GinoPane\BlogTimeToRead\Models\Settings;
8
use GinoPane\BlogTimeToRead\Helpers\TimeToRead;
9
use GinoPane\BlogTimeToRead\Components\TimeToRead as TimeToReadComponent;
10
11
/**
12
 * Class Plugin
13
 *
14
 * @package GinoPane\BlogTimeToRead
15
 */
16
class Plugin extends PluginBase
17
{
18
    const DEFAULT_ICON = 'icon-clock-o';
19
20
    const LOCALIZATION_KEY = 'ginopane.blogtimetoread::lang.';
21
22
    /**
23
     * @var array   Require some plugins
24
     */
25
    public $require = [
26
        'RainLab.Blog',
27
        'RainLab.Translate'
28
    ];
29
30
    /** @var TimeToRead  */
31
    private $helper;
32
33
    /**
34
     * Returns information about this plugin
35
     *
36
     * @return  array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
37
     */
38
    public function pluginDetails()
39
    {
40
        return [
41
            'name'        => self::LOCALIZATION_KEY . 'plugin.name',
42
            'description' => self::LOCALIZATION_KEY . 'plugin.description',
43
            'author'      => 'Siarhei <Gino Pane> Karavai',
44
            'icon'        => self::DEFAULT_ICON,
45
            'homepage'    => 'https://github.com/GinoPane/oc-blogtimetoread-plugin'
46
        ];
47
    }
48
49
    /**
50
     * Boot method, called right before the request route
51
     */
52
    public function boot()
53
    {
54
        $this->helper = new TimeToRead(Settings::instance());
55
56
        // extend the post model
57
        $this->extendModel();
58
    }
59
60
    /**
61
     * Register components
62
     *
63
     * @return  array
64
     */
65
    public function registerComponents()
66
    {
67
        return [
68
            TimeToReadComponent::class => TimeToReadComponent::NAME,
69
        ];
70
    }
71
72
    /**
73
     * Register plugin settings
74
     *
75
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array<string,string|integer>>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
76
     */
77
    public function registerSettings(){
78
        return [
79
            'settings' => [
80
                'label'       => self::LOCALIZATION_KEY . 'settings.name',
81
                'description' => self::LOCALIZATION_KEY . 'settings.description',
82
                'icon'        => self::DEFAULT_ICON,
83
                'class'       => Settings::class,
84
                'order'       => 800,
85
                'category'    => 'rainlab.blog::lang.blog.menu_label'
86
            ]
87
        ];
88
    }
89
90
    /**
91
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array<string,array<Plugin|string>>>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
92
     */
93
    public function registerMarkupTags()
94
    {
95
        return [
96
            'filters' => [
97
                'timeToRead' => [$this, 'getTimeToRead']
98
            ]
99
        ];
100
    }
101
102
    /**
103
     * @param      $text
104
     * @param null $speed
105
     * @param null $roundingEnabled
106
     *
107
     * @return int
108
     */
109
    public function getTimeToRead($text, $speed = null, $roundingEnabled = null)
110
    {
111
        return $this->helper->calculate(
112
            $text,
113
            array_filter([
114
                Settings::READING_SPEED_KEY => $speed,
115
                Settings::ROUNDING_UP_ENABLED_KEY => $roundingEnabled
116
            ])
117
        );
118
    }
119
120
    /**
121
     * Extend RainLab Post model
122
     */
123
    private function extendModel()
124
    {
125
        PostModel::extend(function ($model) {
126
            $model->addDynamicMethod('getTimeToReadAttribute', function() use ($model) {
127
                return $this->helper->calculate($model->content);
128
            });
129
        });
130
    }
131
}
132