|
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(); |
|
|
|
|
|
|
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
|
|
|
|
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:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.