Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 5bc151d

Browse files
authored
Merge pull request #11 from grayloon/download-product-images
Ability to download product images to Laravel application
2 parents c61dd11 + e1d057c commit 5bc151d

File tree

3 files changed

+214
-0
lines changed

3 files changed

+214
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Grayloon\Magento\Jobs;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Contracts\Queue\ShouldQueue;
7+
use Illuminate\Foundation\Bus\Dispatchable;
8+
use Illuminate\Queue\InteractsWithQueue;
9+
use Illuminate\Queue\SerializesModels;
10+
use Illuminate\Support\Facades\Storage;
11+
12+
class DownloadMagentoProductImage implements ShouldQueue
13+
{
14+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
15+
16+
/**
17+
* The end uri of the image from Magento.
18+
*
19+
* @var string
20+
*/
21+
public $uri;
22+
23+
/**
24+
* The Magento directory where images are located.
25+
*
26+
* @var string
27+
*/
28+
public $directory;
29+
30+
/**
31+
* The fully constructed URL to download the image.
32+
*
33+
* @var string
34+
*/
35+
public $fullUrl;
36+
37+
/**
38+
* Create a new job instance.
39+
*
40+
* @return void
41+
*/
42+
public function __construct($uri, $directory = null)
43+
{
44+
$this->uri = $uri;
45+
$this->directory = $this->directory ?: '/pub/media/catalog/product';
46+
$this->fullUrl = config('magento.base_url').$this->directory.$this->uri;
47+
}
48+
49+
/**
50+
* Execute the job.
51+
*
52+
* @return void
53+
*/
54+
public function handle()
55+
{
56+
$contents = file_get_contents($this->fullUrl);
57+
$name = substr($this->fullUrl, strrpos($this->fullUrl, '/') + 1);
58+
59+
Storage::put('product/'.$name, $contents);
60+
}
61+
}

src/Support/MagentoProducts.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Grayloon\Magento\Support;
44

5+
use Grayloon\Magento\Jobs\DownloadMagentoProductImage;
56
use Grayloon\Magento\Magento;
67
use Grayloon\Magento\Models\MagentoExtensionAttribute;
78
use Grayloon\Magento\Models\MagentoExtensionAttributeType;
@@ -106,6 +107,10 @@ protected function syncCustomAttributes($attributes, $product)
106107
$this->syncProductCategories($attribute['value'], $product);
107108
}
108109

110+
if ($this->isImageType($attribute['attribute_code'])) {
111+
$this->downloadImage($attribute['value']);
112+
}
113+
109114
if (is_array($attribute['value'])) {
110115
$attribute['value'] = json_encode($attribute['value']);
111116
}
@@ -118,6 +123,38 @@ protected function syncCustomAttributes($attributes, $product)
118123
return $this;
119124
}
120125

126+
/**
127+
* Determine if the Custom Attribute type is an image.
128+
*
129+
* @param string $attribute_type
130+
* @return bool
131+
*/
132+
protected function isImageType($attribute_type)
133+
{
134+
$types = [
135+
'thumbnail',
136+
'image',
137+
'small_image',
138+
];
139+
140+
return in_array($attribute_type, $types);
141+
}
142+
143+
/**
144+
* Launch a job to download an image to the Laravel application.
145+
*
146+
* @param string $image
147+
* @return void
148+
*/
149+
protected function downloadImage($image)
150+
{
151+
if ($image === 'no_selection') {
152+
return;
153+
}
154+
155+
DownloadMagentoProductImage::dispatch($image);
156+
}
157+
121158
/**
122159
* Assign the Product Category IDs that belong to the product.
123160
*

tests/Support/MagentoProductsTest.php

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace Grayloon\Magento\Tests\Support;
44

5+
use Grayloon\Magento\Jobs\DownloadMagentoProductImage;
56
use Grayloon\Magento\Models\MagentoCategory;
67
use Grayloon\Magento\Models\MagentoProduct;
78
use Grayloon\Magento\Support\MagentoProducts;
89
use Grayloon\Magento\Tests\TestCase;
910
use Illuminate\Support\Facades\Http;
11+
use Illuminate\Support\Facades\Queue;
1012

1113
class MagentoProductsTest extends TestCase
1214
{
@@ -65,4 +67,118 @@ public function test_magento_product_adds_associated_category()
6567
$this->assertCount(1, $productCategories);
6668
$this->assertSame($category->id, $productCategories->first()->id);
6769
}
70+
71+
public function test_magento_product_launches_job_to_download_product_image()
72+
{
73+
Queue::fake();
74+
75+
factory(MagentoCategory::class)->create();
76+
77+
$products = [
78+
[
79+
'id' => '1',
80+
'name' => 'Dunder Mifflin Paper',
81+
'sku' => 'DFPC001',
82+
'price' => 19.99,
83+
'status' => '1',
84+
'visibility' => '1',
85+
'type_id' => 'simple',
86+
'created_at' => now(),
87+
'updated_at' => now(),
88+
'weight' => 10.00,
89+
'extension_attributes' => [
90+
'website_id' => [1],
91+
],
92+
'custom_attributes' => [
93+
[
94+
'attribute_code' => 'image',
95+
'value' => 'foo.jpg',
96+
],
97+
],
98+
],
99+
];
100+
101+
$magentoProducts = new MagentoProducts();
102+
103+
$magentoProducts->updateProducts($products);
104+
105+
Queue::assertPushed(DownloadMagentoProductImage::class);
106+
}
107+
108+
public function test_magento_product_does_not_job_on_invalid_image_download()
109+
{
110+
Queue::fake();
111+
112+
factory(MagentoCategory::class)->create();
113+
114+
$products = [
115+
[
116+
'id' => '1',
117+
'name' => 'Dunder Mifflin Paper',
118+
'sku' => 'DFPC001',
119+
'price' => 19.99,
120+
'status' => '1',
121+
'visibility' => '1',
122+
'type_id' => 'simple',
123+
'created_at' => now(),
124+
'updated_at' => now(),
125+
'weight' => 10.00,
126+
'extension_attributes' => [
127+
'website_id' => [1],
128+
],
129+
'custom_attributes' => [
130+
[
131+
'attribute_code' => 'image',
132+
'value' => 'no_selection',
133+
],
134+
],
135+
],
136+
];
137+
138+
$magentoProducts = new MagentoProducts();
139+
140+
$magentoProducts->updateProducts($products);
141+
142+
Queue::assertNotPushed(DownloadMagentoProductImage::class);
143+
}
144+
145+
public function test_magento_product_download_image_is_correctly_constructed()
146+
{
147+
Queue::fake();
148+
149+
factory(MagentoCategory::class)->create();
150+
151+
$products = [
152+
[
153+
'id' => '1',
154+
'name' => 'Dunder Mifflin Paper',
155+
'sku' => 'DFPC001',
156+
'price' => 19.99,
157+
'status' => '1',
158+
'visibility' => '1',
159+
'type_id' => 'simple',
160+
'created_at' => now(),
161+
'updated_at' => now(),
162+
'weight' => 10.00,
163+
'extension_attributes' => [
164+
'website_id' => [1],
165+
],
166+
'custom_attributes' => [
167+
[
168+
'attribute_code' => 'image',
169+
'value' => '/foo.jpg',
170+
],
171+
],
172+
],
173+
];
174+
175+
$magentoProducts = new MagentoProducts();
176+
177+
$magentoProducts->updateProducts($products);
178+
179+
Queue::assertPushed(DownloadMagentoProductImage::class);
180+
Queue::assertPushed(fn (DownloadMagentoProductImage $downloadJob) => $downloadJob->uri === '/foo.jpg');
181+
Queue::assertPushed(fn (DownloadMagentoProductImage $downloadJob) => $downloadJob->directory === '/pub/media/catalog/product');
182+
Queue::assertPushed(fn (DownloadMagentoProductImage $downloadJob) => $downloadJob->fullUrl === '/pub/media/catalog/product/foo.jpg');
183+
}
68184
}

0 commit comments

Comments
 (0)