Skip to content
This repository was archived by the owner on Nov 6, 2021. It is now read-only.

Commit 777d088

Browse files
author
websharp
authored
Merge pull request #2 from allin-data/feature/media-images
Feature/media images
2 parents 515f3bc + c0b10aa commit 777d088

18 files changed

+1429
-9
lines changed

Console/Command/FuzzyfyrCommand.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class FuzzyfyrCommand extends Command
5050
const OPTION_DUMMY_CONTENT_EMAIL = 'dummy-content-email';
5151
const OPTION_DUMMY_CONTENT_URL = 'dummy-content-url';
5252
const OPTION_DUMMY_CONTENT_PHONE = 'dummy-content-phone';
53+
const OPTION_DUMMY_CONTENT_IMAGE_PATH = 'dummy-content-image-path';
5354

5455
/**
5556
* Defaults
@@ -58,6 +59,7 @@ class FuzzyfyrCommand extends Command
5859
const DEFAULT_DUMMY_CONTENT_EMAIL = 'lorem.ipsum.%1$s@test.localhost';
5960
const DEFAULT_DUMMY_CONTENT_URL = 'https://lor.emips.um/foo/bar/';
6061
const DEFAULT_DUMMY_CONTENT_PHONE = '+49 (0) 600 987 654 32';
62+
const DEFAULT_DUMMY_CONTENT_IMAGE_PATH = './assets/dummy_image.png';
6163

6264
/**
6365
* @var State
@@ -163,6 +165,13 @@ protected function configure()
163165
InputOption::VALUE_OPTIONAL,
164166
'Used as dummy phone number.',
165167
self::DEFAULT_DUMMY_CONTENT_PHONE
168+
),
169+
new InputOption(
170+
self::OPTION_DUMMY_CONTENT_IMAGE_PATH,
171+
null,
172+
InputOption::VALUE_OPTIONAL,
173+
'Used as dummy image content.',
174+
realpath(__DIR__ . DIRECTORY_SEPARATOR . '..' .DIRECTORY_SEPARATOR. '..'. DIRECTORY_SEPARATOR . self::DEFAULT_DUMMY_CONTENT_IMAGE_PATH)
166175
)
167176
]);
168177

@@ -226,6 +235,7 @@ protected function loadConfiguration(Configuration $configuration, InputInterfac
226235
$configuration->setDummyContentEmail($input->getOption(self::OPTION_DUMMY_CONTENT_EMAIL));
227236
$configuration->setDummyContentUrl($input->getOption(self::OPTION_DUMMY_CONTENT_URL));
228237
$configuration->setDummyPhoneNumber($input->getOption(self::OPTION_DUMMY_CONTENT_PHONE));
238+
$configuration->setDummyImagePath($input->getOption(self::OPTION_DUMMY_CONTENT_IMAGE_PATH));
229239

230240
return $configuration;
231241
}

Handler/CategoryImageHandler.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* This file is part of the Content Fuzzyfyr module for Magento2.
4+
*
5+
* (c) All.In Data GmbH
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace AllInData\ContentFuzzyfyr\Handler;
12+
13+
use Magento\Framework\App\Filesystem\DirectoryList;
14+
use Magento\Catalog\Model\Product\Media\Config;
15+
use Magento\Framework\Filesystem;
16+
use Magento\Framework\Filesystem\Io\File;
17+
use Magento\Framework\Filesystem\Directory\WriteInterface;
18+
19+
/**
20+
* Class CategoryImageHandler
21+
* @package AllInData\ContentFuzzyfyr\Handler
22+
*/
23+
class CategoryImageHandler extends MediaFileHandler
24+
{
25+
/*
26+
* Path
27+
*/
28+
const MEDIA_MODULE_BASE_PATH = 'catalog/category';
29+
}

Handler/MediaFileHandler.php

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
/**
3+
* This file is part of the Content Fuzzyfyr module for Magento2.
4+
*
5+
* (c) All.In Data GmbH
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace AllInData\ContentFuzzyfyr\Handler;
12+
13+
use Magento\Framework\App\Filesystem\DirectoryList;
14+
use Magento\Catalog\Model\Product\Media\Config;
15+
use Magento\Framework\Filesystem;
16+
use Magento\Framework\Filesystem\Io\File;
17+
use Magento\Framework\Filesystem\Directory\WriteInterface;
18+
19+
/**
20+
* Class MediaFileHandler
21+
* @package AllInData\ContentFuzzyfyr\Handler
22+
*/
23+
class MediaFileHandler
24+
{
25+
/*
26+
* Path
27+
*/
28+
const MEDIA_MODULE_BASE_PATH = 'allindata/content/fuzzfyr';
29+
30+
/**
31+
* @var Config
32+
*/
33+
private $mediaConfig;
34+
/**
35+
* @var Filesystem
36+
*/
37+
private $fileSystem;
38+
/**
39+
* @var File
40+
*/
41+
private $ioFile;
42+
/**
43+
* @var WriteInterface
44+
*/
45+
private $mediaDirectory;
46+
47+
/**
48+
* MediaFileHandler constructor.
49+
* @param Config $mediaConfig
50+
* @param Filesystem $fileSystem
51+
* @param File $ioFile
52+
*/
53+
public function __construct(Config $mediaConfig, Filesystem $fileSystem, File $ioFile)
54+
{
55+
$this->mediaConfig = $mediaConfig;
56+
$this->fileSystem = $fileSystem;
57+
$this->ioFile = $ioFile;
58+
}
59+
60+
/**
61+
* @param $filePath
62+
* @return string
63+
* @throws \Magento\Framework\Exception\FileSystemException
64+
*/
65+
public function getMediaCopyOfFile($filePath)
66+
{
67+
$this->init();
68+
69+
$mediaFilePath = $this->mediaDirectory->getAbsolutePath(
70+
sprintf(
71+
'%s/%s',
72+
static::MEDIA_MODULE_BASE_PATH,
73+
basename($filePath)
74+
)
75+
);
76+
77+
// short exit if media file already exists
78+
if ($this->ioFile->fileExists($mediaFilePath, true)) {
79+
return $mediaFilePath;
80+
}
81+
82+
/*
83+
* Check on requested file
84+
*/
85+
$filePath = $this->ioFile->getCleanPath($filePath);
86+
if (!$this->ioFile->fileExists($filePath, true)) {
87+
throw new \InvalidArgumentException(
88+
sprintf(
89+
'Could not resolve given image path: "%s"',
90+
$filePath
91+
)
92+
);
93+
}
94+
95+
/*
96+
* Create module media folder
97+
*/
98+
if (!$this->mediaDirectory->create(static::MEDIA_MODULE_BASE_PATH)) {
99+
throw new \RuntimeException(
100+
sprintf(
101+
'Could not create media folder: "%s"',
102+
static::MEDIA_MODULE_BASE_PATH
103+
)
104+
);
105+
}
106+
107+
/*
108+
* Copy file
109+
*/
110+
if (!$this->ioFile->cp($filePath, $mediaFilePath)) {
111+
throw new \RuntimeException(
112+
sprintf(
113+
'Could not copy media file to: "%s"',
114+
$mediaFilePath
115+
)
116+
);
117+
}
118+
119+
return $mediaFilePath;
120+
}
121+
122+
/**
123+
* @throws \Magento\Framework\Exception\FileSystemException
124+
*/
125+
private function init()
126+
{
127+
if (!$this->mediaDirectory) {
128+
$this->mediaDirectory = $this->fileSystem->getDirectoryWrite(DirectoryList::MEDIA);
129+
}
130+
}
131+
}

Model/Configuration.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ class Configuration
6060
* @var string
6161
*/
6262
private $dummyPhoneNumber;
63+
/**
64+
* @var string
65+
*/
66+
private $dummyImagePath;
6367
/**
6468
* @var array
6569
*/
@@ -263,6 +267,24 @@ public function setDummyPhoneNumber($dummyPhoneNumber)
263267
return $this;
264268
}
265269

270+
/**
271+
* @return string
272+
*/
273+
public function getDummyImagePath()
274+
{
275+
return $this->dummyImagePath;
276+
}
277+
278+
/**
279+
* @param string $dummyImagePath
280+
* @return Configuration
281+
*/
282+
public function setDummyImagePath($dummyImagePath)
283+
{
284+
$this->dummyImagePath = $dummyImagePath;
285+
return $this;
286+
}
287+
266288
/**
267289
* @return array
268290
*/

Observer/CategoriesObserver.php

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,16 @@
1515
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
1616
use Magento\Catalog\Model\ResourceModel\Category as CategoryResource;
1717
use Magento\Catalog\Model\ResourceModel\CategoryFactory as CategoryResourceFactory;
18+
use Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollection;
19+
use Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollectionFactory;
1820

1921
class CategoriesObserver extends FuzzyfyrObserver
2022
{
23+
/*
24+
* Root Category
25+
*/
26+
const ROOT_CATEGORY_ID = "1";
27+
2128
/**
2229
* @var CategoryCollectionFactory
2330
*/
@@ -26,16 +33,25 @@ class CategoriesObserver extends FuzzyfyrObserver
2633
* @var CategoryResourceFactory
2734
*/
2835
protected $categoryResourceFactory;
36+
/**
37+
* @var UrlRewriteCollectionFactory
38+
*/
39+
protected $urlRewriteCollectionFactory;
2940

3041
/**
31-
* CategorysObserver constructor.
42+
* CategoriesObserver constructor.
3243
* @param CategoryCollectionFactory $categoryCollectionFactory
3344
* @param CategoryResourceFactory $categoryResourceFactory
45+
* @param UrlRewriteCollectionFactory $urlRewriteCollectionFactory
3446
*/
35-
public function __construct(CategoryCollectionFactory $categoryCollectionFactory, CategoryResourceFactory $categoryResourceFactory)
36-
{
47+
public function __construct(
48+
CategoryCollectionFactory $categoryCollectionFactory,
49+
CategoryResourceFactory $categoryResourceFactory,
50+
UrlRewriteCollectionFactory $urlRewriteCollectionFactory
51+
) {
3752
$this->categoryCollectionFactory = $categoryCollectionFactory;
3853
$this->categoryResourceFactory = $categoryResourceFactory;
54+
$this->urlRewriteCollectionFactory = $urlRewriteCollectionFactory;
3955
}
4056

4157
/**
@@ -48,19 +64,38 @@ public function isValid(Configuration $configuration)
4864

4965
/**
5066
* {@inheritdoc}
51-
* @TODO clear table url_rewrite for entity_type category
52-
* @TODO mark indexer to invalidate index
5367
*/
5468
protected function run(Configuration $configuration)
5569
{
5670
/** @var CategoryResource $categoryResource */
5771
$categoryResource = $this->categoryResourceFactory->create();
5872

73+
/*
74+
* clear table url_rewrite for entity_type category
75+
*/
76+
/** @var UrlRewriteCollection $urlRewriteCollection */
77+
$urlRewriteCollection = $this->urlRewriteCollectionFactory->create();
78+
$urlRewriteCollection
79+
->addFieldToFilter('entity_type', ['eq' => 'category'])
80+
->load();
81+
foreach ($urlRewriteCollection->getItems() as $urlRewrite) {
82+
/** @var \Magento\UrlRewrite\Model\UrlRewrite $urlRewrite */
83+
$urlRewrite->delete();
84+
}
85+
86+
/*
87+
* Process
88+
*/
5989
/** @var CategoryCollection $categoryCollection */
6090
$categoryCollection = $this->categoryCollectionFactory->create();
6191
$categoryCollection->load();
6292
foreach ($categoryCollection->getItems() as $category) {
6393
/** @var \Magento\Catalog\Model\Category $category */
94+
if (self::ROOT_CATEGORY_ID === $category->getId()) {
95+
// skip root category
96+
continue;
97+
}
98+
$category->load($category->getId());
6499
$this->doUpdate($configuration, $category);
65100
$categoryResource->save($category);
66101
}

0 commit comments

Comments
 (0)