/
Container.php
215 lines (183 loc) · 6 KB
/
Container.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
declare(strict_types=1);
namespace Itineris\WPHubSpotImporter;
use Itineris\WPHubSpotImporter\Admin\SettingsPage;
use SevenShores\Hubspot\Factory as HubSpotFactory;
use SevenShores\Hubspot\Http\Client;
use SevenShores\Hubspot\Resources\BlogPosts;
use SevenShores\Hubspot\Resources\BlogTopics;
use SevenShores\Hubspot\Resources\OAuth2 as HubSpotOauth2;
use TypistTech\WPKsesView\Factory as ViewFactory;
use TypistTech\WPOptionStore\Factory as OptionStoreFactory;
use TypistTech\WPOptionStore\OptionStoreInterface;
use UnexpectedValueException;
/**
* TODO: Find a better alternative, maybe `typisttech/wp-contained-hook`.
*/
class Container
{
public const BLOG_POST_POST_TYPE = 'post';
public const HUBSPOT_BLOG_POST_ID_META_KEY = '_hubspot_blog_post_id';
public const HUBSPOT_FEATURED_IMAGE_URL_META_KEY = 'hubspot_featured_image_url';
/**
* Must be non-hierarchical.
*/
public const TOPIC_TAXONOMY = 'post_tag';
/** @var Container */
protected static $instance;
/** @var SettingsPage */
protected $settingsPage;
/** @var HubSpotFactory */
protected $hubSpotFactory;
/** @var AuthorRepo */
protected $authorRepo;
/** @var BlogPostRepo */
protected $blogPostRepo;
/** @var OAuth2 */
protected $oAuth2;
/** @var OptionStoreInterface */
protected $optionStore;
/** @var BlogPosts */
protected $blogPosts;
/** @var Importer */
protected $importer;
/** @var BlogTopicRepo */
protected $blogTopicRepo;
/** @var BlogTopics */
protected $blogTopics;
public static function getInstance(): self
{
if (null === static::$instance) {
$instance = apply_filters(
'wp_hubspot_importer_container_init',
new static()
);
if (! $instance instanceof self) {
$message = sprintf(
'Filter "%1$s" should return an instance of "%2$s", instance of "%3$s" given.',
'wp_hubspot_importer_container_init',
__CLASS__,
is_object($instance) ? get_class($instance) : gettype($instance)
);
throw new UnexpectedValueException($message);
}
static::$instance = $instance;
}
return static::$instance;
}
public function getSettingsPage(): SettingsPage
{
if (null === $this->settingsPage) {
$this->settingsPage = new SettingsPage(
ViewFactory::build(__DIR__ . '/Admin/view/settings-page.php'),
$this->getOptionStore(),
$this->getOAuth2()
);
}
return $this->settingsPage;
}
public function getOptionStore(): OptionStoreInterface
{
if (null === $this->optionStore) {
$this->optionStore = OptionStoreFactory::build();
}
return $this->optionStore;
}
public function getOAuth2(): OAuth2
{
if (null === $this->oAuth2) {
$optionStore = $this->getOptionStore();
$client = new Client(
[
'key' => $optionStore->getString('wp_hubspot_importer_client_secret'),
],
null,
[
'http_errors' => false,
]
);
return new OAuth2(
$optionStore,
new HubSpotOauth2($client)
);
}
return $this->oAuth2;
}
public function getImporter(): Importer
{
if (null === $this->importer) {
$this->importer = new Importer(
$this->getBlogPostRepo(),
$this->getAuthorRepo(),
$this->getBlogTopicRepo()
);
}
return $this->importer;
}
protected function getBlogPostRepo(): BlogPostRepo
{
if (null === $this->blogPostRepo) {
$this->blogPostRepo = new BlogPostRepo(
static::BLOG_POST_POST_TYPE,
static::HUBSPOT_BLOG_POST_ID_META_KEY,
static::HUBSPOT_FEATURED_IMAGE_URL_META_KEY,
static::TOPIC_TAXONOMY
);
}
return $this->blogPostRepo;
}
protected function getAuthorRepo(): AuthorRepo
{
if (null === $this->authorRepo) {
$this->authorRepo = new AuthorRepo();
}
return $this->authorRepo;
}
public function getBlogTopicRepo(): BlogTopicRepo
{
if (null === $this->blogTopicRepo) {
$this->blogTopicRepo = new BlogTopicRepo();
}
return $this->blogTopicRepo;
}
public function getBlogPosts(): BlogPosts
{
if (null === $this->blogPosts) {
$hubSpotFactory = $this->getHubSpotFactory();
$this->blogPosts = $hubSpotFactory->blogPosts();
}
return $this->blogPosts;
}
protected function getHubSpotFactory(): HubSpotFactory
{
// TODO: Review me!
if (null === $this->hubSpotFactory) {
$optionStore = $this->getOptionStore();
$oAuth2 = $this->getOAuth2();
$accessTokenExpireAt = $optionStore->getInt('wp_hubspot_importer_access_token_expire_at');
if ($accessTokenExpireAt - time() < HOUR_IN_SECONDS) {
$oAuth2->refreshAccessToken();
}
// TODO: Check access token valid.
$this->hubSpotFactory = new HubSpotFactory(
[
'key' => $optionStore->getString('wp_hubspot_importer_access_token'),
'oauth2' => true,
],
null,
[
'http_errors' => false,
]
);
}
return $this->hubSpotFactory;
}
public function getBlogTopics(): BlogTopics
{
if (null === $this->blogTopics) {
$hubSpotFactory = $this->getHubSpotFactory();
$this->blogTopics = $hubSpotFactory->blogTopics();
}
return $this->blogTopics;
}
}