Skip to content

Commit d587e51

Browse files
authored
Merge pull request #6 from avored/dev
merging dev to master
2 parents 0866ffa + cfd9596 commit d587e51

8 files changed

+207
-8
lines changed

config/avored-framework.php

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@
1414
'address' => AvoRed\Ecommerce\Models\Database\Address::class,
1515
],
1616

17+
18+
'filesystems' => [
19+
'disks' => [
20+
'avored' => [
21+
'driver' => 'local',
22+
'root' => storage_path('app/public'),
23+
],
24+
],
25+
],
1726
'image' => [
1827
'driver' => 'gd',
1928
'sizes' => [

src/Image/LocalFile.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct($relativePath = null)
3838
$baseName = basename($relativePath);
3939
$sizeNamePath = str_replace($baseName, $sizeName . '-' . $baseName, $relativePath);
4040

41-
$this->$objectVarName = asset('storage/' . $sizeNamePath);
41+
$this->$objectVarName = asset($sizeNamePath);
4242
}
4343
}
4444

src/Image/Service.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,23 @@ class Service
3131
*/
3232
public function upload($image, $path = null, $makeDiffSizes = true)
3333
{
34-
$this->directory(storage_path('app/public/' . $path));
35-
$name = $image->getClientOriginalName();
36-
$image->move(storage_path('app/public/' . $path), $name);
37-
$relativePath = storage_path('app/public/' . $path . DIRECTORY_SEPARATOR . $name);
34+
35+
$dbPath = $image->store($path, 'avored');
36+
37+
$name = basename($dbPath);
3838

3939
if (true === $makeDiffSizes) {
4040
$sizes = config('avored-framework.image.sizes');
4141
foreach ($sizes as $sizeName => $widthHeight) {
4242
list($width, $height) = $widthHeight;
43-
$this->make($relativePath);
43+
$this->make("storage" . DIRECTORY_SEPARATOR .$dbPath);
4444
$this->resizeImage($width, $height, 'crop');
4545
$imagePath = storage_path('app/public/' . $path) . DIRECTORY_SEPARATOR . $sizeName . '-' . $name;
4646
$this->saveImage($imagePath, 100);
4747
}
4848
}
4949

50-
$localImage = new LocalFile($path . '/' . $name);
50+
$localImage = new LocalFile("storage" . DIRECTORY_SEPARATOR . $dbPath);
5151

5252
return $localImage;
5353
}
@@ -99,10 +99,12 @@ private function openImage($file)
9999
// *** Get extension
100100
$extension = strtolower(strrchr($file, '.'));
101101

102+
102103
switch ($extension) {
103104
case '.jpg':
104105
case '.jpeg':
105106
$img = @imagecreatefromjpeg($file);
107+
106108
break;
107109
case '.gif':
108110
$img = @imagecreatefromgif($file);
@@ -115,6 +117,7 @@ private function openImage($file)
115117
break;
116118
}
117119

120+
118121
return $img;
119122
}
120123

src/Models/Contracts/ProductDownloadableUrlInterface.php

+48
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,53 @@
44

55
interface ProductDownloadableUrlInterface
66
{
7+
8+
/**
9+
* Find an Downloadable Product by given Id which returns ProductDownloadableUrl Model
10+
*
11+
* @param integer $id
12+
* @return \AvoRed\Framework\Models\Database\ProductDownloadableUrl
13+
*/
14+
public function find($id);
15+
16+
/**
17+
* Find an Category by given token which returns Category Model
18+
*
19+
* @param string $token
20+
* @return \AvoRed\Framework\Models\Database\ProductDownloadableUrl
21+
*/
22+
public function findByToken($token);
23+
24+
/**
25+
* Get an All which returns Eloquent Collection
26+
*
27+
* @return \Illuminate\Database\Eloquent\Collection
28+
*/
29+
public function all();
30+
31+
/**
32+
* Category Collection with Limit which returns paginate class
33+
*
34+
* @param integer $noOfItem
35+
* @return \Illuminate\Pagination\LengthAwarePaginator
36+
*/
37+
public function paginate($noOfItem = 10);
38+
39+
/**
40+
* Category Query Builder
41+
*
42+
* @return \Illuminate\Database\Eloquent\Builder
43+
*/
44+
public function query();
45+
46+
/**
47+
* Create an Product Downloadable Url
48+
*
49+
* @param array $data
50+
* @return \AvoRed\Framework\Models\Database\ProductDownloadableUrl
51+
*/
52+
public function create(array $data);
53+
754

55+
856
}

src/Models/Database/Product.php

+66
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use Illuminate\Database\Eloquent\Model;
1111
use Illuminate\Database\Eloquent\Collection;
1212
use Illuminate\Pagination\LengthAwarePaginator;
13+
use Illuminate\Support\Facades\App;
14+
use AvoRed\Framework\Models\Contracts\ProductDownloadableUrlInterface;
15+
use AvoRed\Framework\Models\Repository\ProductDownloadableUrlRepository;
1316

1417
class Product extends Model
1518
{
@@ -170,6 +173,7 @@ public function saveProduct($data)
170173
$this->saveProductCategories($data);
171174
$this->saveProductProperties($data);
172175
$this->saveProductAttributes($data);
176+
$this->saveProductDownloadable($data);
173177

174178
Event::fire(new ProductAfterSave($this, $data));
175179

@@ -187,6 +191,58 @@ protected function saveProductCategories($data)
187191
$this->categories()->sync($data['category_id']);
188192
}
189193
}
194+
195+
/**
196+
* Save Product Downloadable Information
197+
*
198+
* @param array $data
199+
* @return void
200+
*/
201+
protected function saveProductDownloadable($data)
202+
{
203+
204+
if (isset($data['downloadable']) && count($data['downloadable']) > 0) {
205+
206+
$repository = App::get(ProductDownloadableUrlInterface::class);
207+
208+
209+
$mainDownloadableMedia = ($data['downloadable']['main_product']) ?? null;
210+
211+
if(null === $mainDownloadableMedia) {
212+
throw new \Exception('Invalid Downloadable Media Given or Nothing Given');
213+
}
214+
215+
$tmpPath = str_split(strtolower(str_random(3)));
216+
$path = 'uploads/downloadables/' . implode('/', $tmpPath);
217+
$dbPath = $mainDownloadableMedia->store($path,'avored');
218+
$token = str_random(32);
219+
220+
221+
$downModel = $repository->query()->whereProductId($this->id)->first();
222+
223+
if (null === $downModel) {
224+
$downModel = ProductDownloadableUrl::create([
225+
'token' => $token,
226+
'product_id' => $this->id,
227+
'main_path' => $dbPath
228+
]);
229+
} else {
230+
$downModel->update(['main_path' => $dbPath]);
231+
}
232+
233+
234+
235+
$demoDownloadableMedia = ($data['downloadable']['demo_product']) ?? null;
236+
237+
if (null !== $demoDownloadableMedia) {
238+
$tmpPath = str_split(strtolower(str_random(3)));
239+
$path = 'uploads/downloadables/' . implode('/', $tmpPath);
240+
$demoDbPath = $demoDownloadableMedia->store($path,'avored');
241+
242+
$downModel->update(['demo_path' => $demoDbPath]);
243+
}
244+
}
245+
}
190246
/**
191247
* Save Product Attributes
192248
*
@@ -624,4 +680,14 @@ public function orders()
624680
{
625681
return $this->hasMany(Order::class);
626682
}
683+
684+
/**
685+
* Product has downladable Url.
686+
*
687+
* @return \AvoRed\Framework\Models\Database\ProductDownloadableUrl
688+
*/
689+
public function downloadable()
690+
{
691+
return $this->hasOne(ProductDownloadableUrl::class);
692+
}
627693
}

src/Models/Provider.php

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use AvoRed\Framework\Models\Repository\ConfigurationRepository;
1414
use AvoRed\Framework\Models\Contracts\OrderInterface;
1515
use AvoRed\Framework\Models\Repository\OrderRepository;
16+
use AvoRed\Framework\Models\Contracts\ProductDownloadableUrlInterface;
17+
use AvoRed\Framework\Models\Repository\ProductDownloadableUrlRepository;
1618

1719
class Provider extends ServiceProvider
1820
{
@@ -64,5 +66,10 @@ protected function registerModelContracts()
6466
OrderInterface::class,
6567
OrderRepository::class
6668
);
69+
70+
$this->app->bind(
71+
ProductDownloadableUrlInterface::class,
72+
ProductDownloadableUrlRepository::class
73+
);
6774
}
6875
}

src/Models/Repository/ProductDownloadableUrlRepository.php

+58-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,65 @@
44

55

66
use AvoRed\Framework\Models\Contracts\ProductDownloadableUrlInterface;
7+
use AvoRed\Framework\Models\Database\ProductDownloadableUrl;
78

89
class ProductDownloadableUrlRepository implements ProductDownloadableUrlInterface
910
{
10-
11+
/**
12+
* Find an Downloadable Product by given Id which returns ProductDownloadableUrl Model
13+
*
14+
* @param integer $id
15+
* @return \AvoRed\Framework\Models\Database\ProductDownloadableUrl
16+
*/
17+
public function find($id) {
18+
return ProductDownloadableUrl::find($id);
19+
}
20+
21+
/**
22+
* Find an Category by given token which returns Category Model
23+
*
24+
* @param string $token
25+
* @return \AvoRed\Framework\Models\Database\ProductDownloadableUrl
26+
*/
27+
public function findByToken($token) {
28+
return ProductDownloadableUrl::whereToken($token)->first();
29+
}
30+
31+
/**
32+
* Get an All which returns Eloquent Collection
33+
*
34+
* @return \Illuminate\Database\Eloquent\Collection
35+
*/
36+
public function all() {
37+
return ProductDownloadableUrl::all();
38+
}
39+
40+
/**
41+
* Category Collection with Limit which returns paginate class
42+
*
43+
* @param integer $noOfItem
44+
* @return \Illuminate\Pagination\LengthAwarePaginator
45+
*/
46+
public function paginate($noOfItem = 10) {
47+
return ProductDownloadableUrl::paginate($noOfItem);
48+
}
49+
50+
/**
51+
* Category Query Builder
52+
*
53+
* @return \Illuminate\Database\Eloquent\Builder
54+
*/
55+
public function query() {
56+
return ProductDownloadableUrl::query();
57+
}
58+
59+
/**
60+
* Create an Product Downloadable Url
61+
*
62+
* @param array $data
63+
* @return \AvoRed\Framework\Models\Database\ProductDownloadableUrl
64+
*/
65+
public function create(array $data) {
66+
return ProductDownloadableUrl::create($data);
67+
}
1168
}

src/Provider.php

+9
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,16 @@ public function registerResources()
7474

7575
public function registerConfigData()
7676
{
77+
7778
$this->mergeConfigFrom(__DIR__ . '/../config/avored-framework.php', 'avored-framework');
79+
80+
$avoredConfigData = include(__DIR__ . '/../config/avored-framework.php');
81+
$fileSystemConfig = $this->app['config']->get('filesystems', []);
82+
83+
84+
$this->app['config']->set('filesystems', array_merge_recursive($avoredConfigData['filesystems'], $fileSystemConfig));
85+
86+
7887
}
7988

8089
public function publishFiles()

0 commit comments

Comments
 (0)