Completed
Push — master ( f17850...8c95bb )
by Nate
11:57
created

HubSpot::isDebugModeEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace flipbox\hubspot;
4
5
use Craft;
6
use craft\base\Plugin;
7
use craft\helpers\UrlHelper;
8
use craft\web\Request;
9
use flipbox\craft\psr6\Cache;
10
use flipbox\craft\psr6\events\RegisterCachePools;
11
use flipbox\hubspot\models\Settings as SettingsModel;
12
use flipbox\hubspot\patron\provider\HubSpot as HubSpotProvider;
13
use flipbox\patron\modules\configuration\events\RegisterProviders;
14
use flipbox\patron\modules\configuration\Module as PatronConfiguration;
15
use flipbox\spark\modules\interfaces\LoggableInterface;
16
use flipbox\spark\modules\traits\LoggableTrait;
17
use yii\base\Event;
18
19
/**
20
 * @method SettingsModel getSettings()
21
 */
22
class HubSpot extends Plugin implements LoggableInterface
23
{
24
25
    use LoggableTrait;
26
27
    /**
28
     * The default transformer
29
     */
30
    const DEFAULT_TRANSFORMER = 'hubspot';
31
32
    /**
33
     * @inheritdoc
34
     */
35
    public function init()
36
    {
37
        // CP Requests
38
        if (Craft::$app->getRequest() instanceof Request &&
39
            Craft::$app->getRequest()->getIsCpRequest()
40
        ) {
41
            Event::on(
42
                PatronConfiguration::class,
43
                PatronConfiguration::EVENT_REGISTER_PROVIDERS,
44
                function (RegisterProviders $event) {
45
                    $event->providers[] = HubSpotProvider::class;
46
                }
47
            );
48
        }
49
50
        // PSR3 logger override
51
        $this->logger()->logger = static::getLogger();
52
        
53
        parent::init();
54
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59
    public function isDebugModeEnabled()
60
    {
61
        return (bool) $this->getSettings()->debugMode;
62
    }
63
64
    /**
65
     * @return SettingsModel
66
     */
67
    public function createSettingsModel()
68
    {
69
        return new SettingsModel();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \flipbox\hubspot\models\Settings(); (flipbox\hubspot\models\Settings) is incompatible with the return type of the parent method craft\base\Plugin::createSettingsModel of type craft\base\Model|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75
    public function settingsHtml()
76
    {
77
        return Craft::$app->getView()->renderTemplate('hubspot/settings', [
78
            'hubspot' => $this
79
        ]);
80
    }
81
82
    /*******************************************
83
     * SERVICES
84
     *******************************************/
85
86
    /**
87
     * @return \flipbox\craft\psr6\Cache
88
     */
89
    public function cache()
90
    {
91
        return $this->get('cache');
92
    }
93
94
    /**
95
     * @return services\Client
96
     */
97
    public function client()
98
    {
99
        return $this->get('client');
100
    }
101
102
    /**
103
     * @return \flipbox\craft\psr3\Logger
104
     */
105
    public function logger()
106
    {
107
        return $this->get('logger');
108
    }
109
110
    /**
111
     * @return services\Transformer
112
     */
113
    public function transformer()
114
    {
115
        return $this->get('transformer');
116
    }
117
118
    /*******************************************
119
     * SUB-MODULES
120
     *******************************************/
121
122
    /**
123
     * @return modules\http\Module
124
     */
125
    public function http()
126
    {
127
        return $this->getModule('http');
128
    }
129
130
    /**
131
     * @return modules\resources\Module
132
     */
133
    public function resources()
134
    {
135
        return $this->getModule('resources');
136
    }
137
}
138