| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Hideyo\Ecommerce\Framework\Services\Shop; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use File; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Image; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Hideyo\Ecommerce\Framework\Services\Shop\Entity\ShopRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Hideyo\Ecommerce\Framework\Services\BaseService; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 10 |  |  |   | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  | class ShopService extends BaseService | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     public function __construct(ShopRepository $shop) | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |         $this->repo = $shop; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |         $this->storageImagePath = storage_path() .config('hideyo.storage_path'). "/shop/"; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |         $this->publicImagePath = public_path() .config('hideyo.public_path'). "/shop/"; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     }  | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      * The validation rules for the model. | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * @param  integer  $shopId id attribute model     | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     private function rules($shopId = false) | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         $rules = array( | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |             'title' => 'required|between:4,65|unique:'.$this->repo->getModel()->getTable(), | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |             'active' => 'required' | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         if ($shopId) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |             $rules['title'] =   $rules['title'].',title,'.$shopId; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         return $rules; | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     public function create(array $attributes) | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         $validator = Validator::make($attributes, $this->rules()); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         if ($validator->fails()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |             return $validator; | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         $model = $this->updateOrAddModel($this->repo->getModel(), $attributes); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         if (isset($attributes['logo'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |             $destinationPath = $this->storageImagePath.$model->id; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             $filename =  str_replace(" ", "_", strtolower($attributes['logo']->getClientOriginalName())); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             $upload_success = $attributes['logo']->move($destinationPath, $filename); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             $attributes['logo_file_name'] = $filename; | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             $attributes['logo_file_path'] = $upload_success->getRealPath(); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             $model = $this->updateOrAddModel($model, $attributes); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             if (File::exists($model->logo_file_path)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |                 if (!File::exists($this->publicImagePath.$model->id)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |                     File::makeDirectory($this->publicImagePath.$model->id, 0777, true); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |                 if (!File::exists($this->publicImagePath.$model->id."/".$model->logo_file_name)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |                     $image = Image::make($model->logo_file_path); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |                     $image->interlace(); | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |                     $image->save($this->publicImagePath.$model->id."/".$model->logo_file_name); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         return $model; | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |      * update model by id | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |      * @param  array  $attributes  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |      * @param  integer $shopId      | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      * @return midex              | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |     public function updateById(array $attributes, $shopId) | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         $validator = Validator::make($attributes, $this->rules($shopId)); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         if ($validator->fails()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |             return $validator; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         $model = $this->find($shopId); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         return $this->updateEntity($model, $attributes); | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |     public function updateEntity($model, array $attributes = array()) | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         if (count($attributes) > 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |             if (isset($attributes['logo'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |                 File::delete($model->logo_file_path); | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |                 $destinationPath = $this->storageImagePath.$model->id; | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |                 $filename =  str_replace(" ", "_", strtolower($attributes['logo']->getClientOriginalName())); | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |                 $upload_success = $attributes['logo']->move($destinationPath, $filename); | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |                 $attributes['logo_file_name'] = $filename; | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |                 $attributes['logo_file_path'] = $upload_success->getRealPath(); | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |             $model->slug = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |             $model->fill($attributes); | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |             $model->save(); | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |             if (File::exists($model->logo_file_path)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |                 if (!File::exists($this->publicImagePath.$model->id)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |                     File::makeDirectory($this->publicImagePath.$model->id, 0777, true); | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |                 if (!File::exists($this->publicImagePath.$model->id."/".$model->logo_file_name)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |                     $image = Image::make($model->logo_file_path); | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |                     $image->interlace(); | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |                     $image->save($this->publicImagePath.$model->id."/".$model->logo_file_name); | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |         return $model; | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |      * destroy model | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |      * @param  integer $shopId  | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |      * @return object       | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |     public function destroy($shopId) | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |         $model = $this->find($shopId); | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |         File::deleteDirectory($this->publicImagePath.$model->id); | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |         $destinationPath = $this->storageImagePath.$model->id; | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |         File::deleteDirectory($destinationPath); | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |         $model->save(); | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |         return $model->delete(); | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |      * find model by url | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |      * @param  string $shopUrl  | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |      * @return mixed | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     public function findUrl($shopUrl) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $result = $this->repo->findUrl($shopUrl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         if (isset($result->id)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |          | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 158 |  |  |         return false;       | 
            
                                                                        
                                                                
            
                                    
            
            
                | 159 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 160 |  |  | } |