TemplateProgramDetailServices.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Services;
  3. use App\Repositories\TemplateProgramDetailRepositories as repo;
  4. use Exception;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Log;
  7. use InvalidArgumentException;
  8. use Illuminate\Validation\ValidationException;
  9. class TemplateProgramDetailServices
  10. {
  11. private $repo;
  12. private $path = 'assets\images\logo';
  13. private $allowedFileExtension = [
  14. 'jpg',
  15. 'png',
  16. 'jpeg'
  17. ];
  18. public function __construct(repo $repo)
  19. {
  20. $this->repo = $repo;
  21. }
  22. public function repoGetData(){
  23. return $this->repo->get();
  24. }
  25. public function repoSave($data){
  26. if($data['file']){
  27. $path = $this->uploadImage($data['file']);
  28. $data['logo'] = $path;
  29. }
  30. return $this->repo->save($data);
  31. }
  32. public function repoUpdate($id,$data){
  33. return $this->repo->update($id, $data);
  34. }
  35. public function repoGetDataByID($id){
  36. return $this->repo->getById($id);
  37. }
  38. public function repoDeleteById($id) {
  39. return $this->repo->delete($id);
  40. }
  41. public function uploadImage($image){
  42. $check = in_array($image->extension(), $this->allowedFileExtension);
  43. if($check){
  44. $filename = $image->getClientOriginalName();
  45. $path = $image->move(public_path($this->path), $filename);
  46. return $path->getPathName();
  47. }else{
  48. throw ValidationException::withMessages(['file' => 'Wrong file extension']);
  49. }
  50. }
  51. }