Skip to content

Commit b187488

Browse files
author
Bohdan Berezhniy
committed
use regular data patch
1 parent d24239a commit b187488

File tree

3 files changed

+46
-96
lines changed

3 files changed

+46
-96
lines changed

Setup/Patch/Data/CreateSamplePost.php

Lines changed: 37 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,47 @@
99
namespace Magefan\Blog\Setup\Patch\Data;
1010

1111
use Magento\Framework\Setup\Patch\DataPatchInterface;
12-
use Magento\Framework\Setup\Patch\PatchVersionInterface;
12+
use Magefan\Blog\Model\PostFactory;
13+
use Magefan\Blog\Model\ResourceModel\Post\CollectionFactory as PostCollectionFactory;
14+
use Magento\Framework\App\State;
15+
use Magento\Framework\App\Config\ScopeConfigInterface;
1316

14-
class CreateSamplePost implements DataPatchInterface, PatchVersionInterface
17+
class CreateSamplePost implements DataPatchInterface
1518
{
1619
/**
17-
* Post factory
18-
*
19-
* @var \Magefan\Blog\Model\PostFactory
20+
* @var PostFactory
2021
*/
2122
private $_postFactory;
2223

2324
/**
24-
* State
25-
*
26-
* @var \Magento\Framework\App\State
25+
* @var PostCollectionFactory
26+
*/
27+
private $postCollection;
28+
29+
/**
30+
* @var State
2731
*/
2832
private $state;
2933

3034
/**
31-
* @var \Magento\Framework\App\Config\ScopeConfigInterface
35+
* @var ScopeConfigInterface
3236
*/
3337
private $scopeConfig;
3438

35-
39+
/**
40+
* @param PostFactory $postFactory
41+
* @param PostCollectionFactory $postCollection
42+
* @param State $state
43+
* @param ScopeConfigInterface $scopeConfig
44+
*/
3645
public function __construct(
37-
\Magefan\Blog\Model\PostFactory $postFactory,
38-
\Magento\Framework\App\State $state,
39-
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
46+
PostFactory $postFactory,
47+
PostCollectionFactory $postCollection,
48+
State $state,
49+
ScopeConfigInterface $scopeConfig
4050
) {
4151
$this->_postFactory = $postFactory;
52+
$this->postCollection = $postCollection;
4253
$this->state = $state;
4354
$this->scopeConfig = $scopeConfig;
4455
}
@@ -51,58 +62,20 @@ public function apply()
5162
/* Do nothing, it's OK */
5263
}
5364

54-
$url = $this->scopeConfig
55-
->getValue(
56-
'web/unsecure/base_url',
57-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
58-
0
59-
);
60-
$useLinks = \Magefan\Community\Model\UrlChecker::showUrl($url);
61-
$useLinks = false;
65+
if (!$this->postCollection->create()->getSize()) {
6266

63-
$data = [
64-
'title' => 'Magento 2 Blog Post Sample',
65-
'meta_keywords' => 'magento 2 blog sample',
66-
'meta_description' => 'Magento 2 blog default post.',
67-
'identifier' => 'magento-2-blog-post-sample',
68-
'content_heading' => 'Magento 2 Blog Post Sample',
69-
'content' =>
70-
$useLinks
71-
? '<p>Welcome to
72-
<a title="Magento Blog"
73-
href="https://magefan.com/magento2-blog-extension"
74-
target="_blank">Magento Blog</a> by
75-
<a title="Magento 2 Extensions"
76-
href="https://magefan.com/magento-2-extensions"
77-
target="_blank">Magefan</a>.
78-
This is your first post. Edit or delete it, then start blogging!
79-
</p>
80-
<p><!-- pagebreak --></p>
81-
<p>Please also read&nbsp;
82-
<a title="Magento 2 Blog online documentation"
83-
href="https://magefan.com/blog/magento-2-blog-extension-documentation"
84-
target="_blank">Magento 2 Blog online documentation</a>&nbsp;and&nbsp;
85-
<a href="https://magefan.com/blog/add-read-more-tag-to-blog-post-content"
86-
target="_blank">How to add "read more" tag to post content</a>
87-
</p>
88-
<p>Follow Magefan on:</p>
89-
<p>
90-
<a title="Magento 2 Blog Extension GitHub"
91-
href="https://github.com/magefan/module-blog"
92-
target="_blank">Magento 2 Blog Extension GitHub</a>&nbsp;|&nbsp;
93-
<a href="https://twitter.com/magento2fan" title="Magefan at Twitter"
94-
target="_blank">Magefan at Twitter</a>&nbsp;|&nbsp;
95-
<a href="https://www.facebook.com/magefan/" title="Magefan at Facebook"
96-
target="_blank">Magefan at Facebook</a>
97-
</p>'
98-
: '<p>Welcome to Magento 2 Blog extension by Magefan.
99-
This is your first post. Edit or delete it, then start blogging!
100-
</p>',
101-
'store_ids' => [0]
102-
];
103-
104-
$this->_postFactory->create()->setData($data)->save();
67+
$data = [
68+
'title' => 'Magento 2 Blog Post Sample',
69+
'meta_keywords' => 'magento 2 blog sample',
70+
'meta_description' => 'Magento 2 blog default post.',
71+
'identifier' => 'magento-2-blog-post-sample',
72+
'content_heading' => 'Magento 2 Blog Post Sample',
73+
'content' => '<p>Welcome to Magento 2 Blog extension by Magefan. This is your first post. Edit or delete it, then start blogging!</p>',
74+
'store_ids' => [0]
75+
];
10576

77+
$this->_postFactory->create()->setData($data)->save();
78+
}
10679
}
10780

10881
public static function getDependencies()
@@ -114,9 +87,4 @@ public function getAliases()
11487
{
11588
return[];
11689
}
117-
118-
public static function getVersion()
119-
{
120-
return '2.12.3';
121-
}
122-
}
90+
}

Setup/Patch/Data/TagInStore.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99
namespace Magefan\Blog\Setup\Patch\Data;
1010

1111
use Magento\Framework\Setup\Patch\DataPatchInterface;
12-
use Magento\Framework\Setup\Patch\PatchVersionInterface;
1312
use Magento\Framework\App\ResourceConnection;
1413

15-
class TagInStore implements DataPatchInterface, PatchVersionInterface
14+
class TagInStore implements DataPatchInterface
1615
{
1716
/**
1817
* @var ResourceConnection
@@ -24,8 +23,7 @@ class TagInStore implements DataPatchInterface, PatchVersionInterface
2423
*/
2524
public function __construct(
2625
ResourceConnection $resourceConnection
27-
)
28-
{
26+
) {
2927
$this->resourceConnection = $resourceConnection;
3028
}
3129

@@ -72,9 +70,4 @@ public function apply()
7270
}
7371
}
7472
}
75-
76-
public static function getVersion()
77-
{
78-
return '2.9.8';
79-
}
8073
}

Setup/Patch/Data/UpdatePostCommentsCount.php

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
use Magento\Framework\Module\ModuleResource;
1212
use Magento\Framework\Setup\Patch\DataPatchInterface;
1313
use Magefan\Blog\Model\ResourceModel\Comment;
14-
use Magento\Framework\Setup\Patch\PatchVersionInterface;
1514

16-
class UpdatePostCommentsCount implements DataPatchInterface, PatchVersionInterface
15+
class UpdatePostCommentsCount implements DataPatchInterface
1716
{
1817
/**
1918
* @var Comment
@@ -32,22 +31,11 @@ class UpdatePostCommentsCount implements DataPatchInterface, PatchVersionInterfa
3231
public function __construct(
3332
Comment $commentResource,
3433
ModuleResource $moduleResource
35-
)
36-
{
34+
) {
3735
$this->commentResource = $commentResource;
3836
$this->moduleResource = $moduleResource;
3937
}
4038

41-
public static function getDependencies()
42-
{
43-
return [];
44-
}
45-
46-
public function getAliases()
47-
{
48-
return [];
49-
}
50-
5139
public function apply()
5240
{
5341
$connection = $this->commentResource->getConnection();
@@ -61,12 +49,13 @@ public function apply()
6149
}
6250
}
6351

64-
public function revert()
52+
public static function getDependencies()
6553
{
54+
return [];
6655
}
6756

68-
public static function getVersion()
57+
public function getAliases()
6958
{
70-
return '2.9.1';
59+
return [];
7160
}
72-
}
61+
}

0 commit comments

Comments
 (0)