SettingsRepository.php 2.66 KB
Newer Older
Viral Solani's avatar
Viral Solani committed
1 2 3 4
<?php

namespace App\Repositories\Backend\Settings;

bvipul's avatar
bvipul committed
5
use App\Exceptions\GeneralException;
6
use App\Models\Settings\Setting;
7
use App\Repositories\BaseRepository;
bvipul's avatar
bvipul committed
8
use Illuminate\Support\Facades\Storage;
Viral Solani's avatar
Viral Solani committed
9 10 11 12 13 14 15 16 17 18 19

/**
 * Class SettingsRepository.
 */
class SettingsRepository extends BaseRepository
{
    /**
     * Associated Repository Model.
     */
    const MODEL = Setting::class;

bvipul's avatar
bvipul committed
20
    /**
21
     * Site Logo Path.
bvipul's avatar
bvipul committed
22 23 24 25 26 27
     *
     * @var string
     */
    protected $site_logo_path;

    /**
28
     * Favicon path.
bvipul's avatar
bvipul committed
29 30 31 32 33 34
     *
     * @var string
     */
    protected $favicon_path;

    /**
35
     * Storage Class Object.
bvipul's avatar
bvipul committed
36 37 38 39 40 41
     *
     * @var \Illuminate\Support\Facades\Storage
     */
    protected $storage;

    /**
42
     * Constructor.
bvipul's avatar
bvipul committed
43 44 45
     */
    public function __construct()
    {
46 47
        $this->site_logo_path = 'img'.DIRECTORY_SEPARATOR.'logo'.DIRECTORY_SEPARATOR;
        $this->favicon_path = 'img'.DIRECTORY_SEPARATOR.'favicon'.DIRECTORY_SEPARATOR;
bvipul's avatar
bvipul committed
48 49 50
        $this->storage = Storage::disk('public');
    }

Viral Solani's avatar
Viral Solani committed
51
    /**
Vipul Basapati's avatar
Vipul Basapati committed
52
     * @param \App\Models\Settings\Setting $setting
53
     * @param array                        $input
Viral Solani's avatar
Viral Solani committed
54
     *
Vipul Basapati's avatar
Vipul Basapati committed
55
     * @throws \App\Exceptions\GeneralException
Viral Solani's avatar
Viral Solani committed
56
     *
bvipul's avatar
bvipul committed
57
     * @return bool
Viral Solani's avatar
Viral Solani committed
58
     */
Vipul Basapati's avatar
Vipul Basapati committed
59
    public function update(Setting $setting, array $input)
Viral Solani's avatar
Viral Solani committed
60
    {
61
        if (!empty($input['logo'])) {
bvipul's avatar
bvipul committed
62 63 64
            $this->removeLogo($setting, 'logo');

            $input['logo'] = $this->uploadLogo($setting, $input['logo'], 'logo');
Viral Solani's avatar
Viral Solani committed
65 66
        }

67
        if (!empty($input['favicon'])) {
bvipul's avatar
bvipul committed
68 69 70
            $this->removeLogo($setting, 'favicon');

            $input['favicon'] = $this->uploadLogo($setting, $input['favicon'], 'favicon');
Viral Solani's avatar
Viral Solani committed
71 72
        }

73
        if ($setting->update($input)) {
Vipul Basapati's avatar
Vipul Basapati committed
74
            return true;
75
        }
76

Vipul Basapati's avatar
Vipul Basapati committed
77
        throw new GeneralException(trans('exceptions.backend.settings.update_error'));
Viral Solani's avatar
Viral Solani committed
78 79 80 81 82
    }

    /*
     * Upload logo image
     */
bvipul's avatar
bvipul committed
83
    public function uploadLogo($setting, $logo, $type)
Viral Solani's avatar
Viral Solani committed
84
    {
85
        $path = $type == 'logo' ? $this->site_logo_path : $this->favicon_path;
Viral Solani's avatar
Viral Solani committed
86

87
        $image_name = time().$logo->getClientOriginalName();
Viral Solani's avatar
Viral Solani committed
88

89
        $this->storage->put($path.$image_name, file_get_contents($logo->getRealPath()));
90

Viral Solani's avatar
Viral Solani committed
91 92 93 94
        return $image_name;
    }

    /*
bvipul's avatar
bvipul committed
95
     * remove logo or favicon icon
Viral Solani's avatar
Viral Solani committed
96
     */
bvipul's avatar
bvipul committed
97
    public function removeLogo(Setting $setting, $type)
Viral Solani's avatar
Viral Solani committed
98
    {
99
        $path = $type == 'logo' ? $this->site_logo_path : $this->favicon_path;
Viral Solani's avatar
Viral Solani committed
100

101 102
        if ($setting->$type && $this->storage->exists($path.$setting->$type)) {
            $this->storage->delete($path.$setting->$type);
Viral Solani's avatar
Viral Solani committed
103 104
        }

105
        $result = $setting->update([$type => null]);
106

107 108 109
        if ($result) {
            return true;
        }
Viral Solani's avatar
Viral Solani committed
110

bvipul's avatar
bvipul committed
111
        throw new GeneralException(trans('exceptions.backend.settings.update_error'));
Viral Solani's avatar
Viral Solani committed
112 113
    }
}