Completed
Push — master ( f8c4bd...25c22e )
by Nate
09:23 queued 07:32
created

src/HubSpot.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace flipbox\hubspot;
4
5
use Craft;
6
use craft\base\Plugin;
7
use craft\events\RegisterComponentTypesEvent;
8
use craft\services\Fields;
9
use craft\services\Plugins;
10
use flipbox\craft\psr3\Logger;
11
use flipbox\ember\modules\LoggerTrait;
12
use flipbox\hubspot\fields\Resources;
13
use flipbox\hubspot\models\Settings as SettingsModel;
14
use flipbox\hubspot\patron\Events;
15
use yii\base\Event;
16
17
/**
18
 * @method SettingsModel getSettings()
19
 *
20
 * @property services\Cache $cache
21
 * @property services\Connections $connections
22
 * @property Logger $psr3Logger
23
 * @property services\Resources $resources
24
 * @property services\ResourceAssociations $resourceAssociations
25
 * @property services\ResourcesField $resourcesField
26
 * @property services\Transformers $transformers
27
 */
28
class HubSpot extends Plugin
29
{
30
    use LoggerTrait;
31
32
    /**
33
     * The default transformer
34
     */
35
    const DEFAULT_TRANSFORMER = 'hubspot';
36
37
    /**
38
     * @inheritdoc
39
     */
40 3
    protected static function getLogFileName(): string
41
    {
42 3
        return 'hubspot';
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48 3
    protected static function isDebugModeEnabled()
49
    {
50 3
        return (bool)static::getInstance()->getSettings()->debugMode;
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56 21
    public function init()
57
    {
58 21
        parent::init();
59
60
        // Components
61 21
        $this->setComponents([
62 21
            'cache' => services\Cache::class,
63
            'connections' => services\Connections::class,
64 7
            'psr3Logger' => function () {
65 3
                return Craft::createObject([
66 3
                    'class' => Logger::class,
67 3
                    'logger' => static::getLogger(),
68 3
                    'category' => self::getLogFileName()
69
                ]);
70 21
            },
71
            'resources' => services\Resources::class,
72
            'resourceAssociations' => services\ResourceAssociations::class,
73
            'resourcesField' => services\ResourcesField::class,
74
            'transformers' => services\Transformers::class,
75
        ]);
76
77
        // Modules
78 21
        $this->setModules([
79 21
            'cp' => cp\Cp::class
80
81
        ]);
82
83
        // Register our field types
84 21
        Event::on(
85 21
            Fields::class,
86 21
            Fields::EVENT_REGISTER_FIELD_TYPES,
87 7
            function (RegisterComponentTypesEvent $event) {
88
                $event->types[] = Resources::class;
89 21
            }
90
        );
91
92
        // Patron Access Token (if installed)
93 21
        Event::on(
94 21
            Plugins::class,
95 21
            Plugins::EVENT_AFTER_LOAD_PLUGINS,
96 7
            function () {
97
                if (Craft::$app->getPlugins()->getPlugin('patron')) {
98
                    Events::register();
99
                }
100 21
            }
101
        );
102 21
    }
103
104
    /**
105
     * @return SettingsModel
106
     */
107 6
    public function createSettingsModel()
108
    {
109 6
        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...
110
    }
111
112
    /**
113
     * @inheritdoc
114
     */
115
    public function settingsHtml()
116
    {
117
        return Craft::$app->getView()->renderTemplate('hubspot/settings', [
118
            'hubspot' => $this
119
        ]);
120
    }
121
122
    /*******************************************
123
     * SERVICES
124
     *******************************************/
125
126
    /**
127
     * @noinspection PhpDocMissingThrowsInspection
128
     * @return services\Cache
129
     */
130 3
    public function getCache(): services\Cache
131
    {
132
        /** @noinspection PhpUnhandledExceptionInspection */
133
        /** @noinspection PhpIncompatibleReturnTypeInspection */
134 3
        return $this->get('cache');
135
    }
136
137
    /**
138
     * @noinspection PhpDocMissingThrowsInspection
139
     * @return services\Connections
140
     */
141 3
    public function getConnections(): services\Connections
142
    {
143
        /** @noinspection PhpUnhandledExceptionInspection */
144
        /** @noinspection PhpIncompatibleReturnTypeInspection */
145 3
        return $this->get('connections');
146
    }
147
148
    /**
149
     * @noinspection PhpDocMissingThrowsInspection
150
     * @return Logger
151
     */
152 3
    public function getPsrLogger(): Logger
153
    {
154
        /** @noinspection PhpUnhandledExceptionInspection */
155
        /** @noinspection PhpIncompatibleReturnTypeInspection */
156 3
        return $this->get('psr3Logger');
157
    }
158
159
    /**
160
     * @noinspection PhpDocMissingThrowsInspection
161
     * @return services\ResourceAssociations
162
     */
163 3
    public function getResourceAssociations(): services\ResourceAssociations
164
    {
165
        /** @noinspection PhpUnhandledExceptionInspection */
166
        /** @noinspection PhpIncompatibleReturnTypeInspection */
167 3
        return $this->get('resourceAssociations');
168
    }
169
170
    /**
171
     * @noinspection PhpDocMissingThrowsInspection
172
     * @return services\ResourcesField
173
     */
174 3
    public function getResourcesField(): services\ResourcesField
175
    {
176
        /** @noinspection PhpUnhandledExceptionInspection */
177
        /** @noinspection PhpIncompatibleReturnTypeInspection */
178 3
        return $this->get('resourcesField');
179
    }
180
181
    /**
182
     * @noinspection PhpDocMissingThrowsInspection
183
     * @return services\Transformers
184
     */
185 3
    public function getTransformers(): services\Transformers
186
    {
187
        /** @noinspection PhpUnhandledExceptionInspection */
188
        /** @noinspection PhpIncompatibleReturnTypeInspection */
189 3
        return $this->get('transformers');
190
    }
191
192
193
    /*******************************************
194
     * RESOURCES
195
     *******************************************/
196
197
    /**
198
     * @noinspection PhpDocMissingThrowsInspection
199
     * @return services\Resources
200
     */
201 3
    public function getResources(): services\Resources
202
    {
203
        /** @noinspection PhpUnhandledExceptionInspection */
204
        /** @noinspection PhpIncompatibleReturnTypeInspection */
205 3
        return $this->get('resources');
206
    }
207
}
208