File: /var/www/vhost/disk-apps/comfama.sports-crowd.com/app/Services/TicketParametersService.php
<?php
namespace App\Services;
use App\TicketParameter;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;
class TicketParametersService
{
public function getParameterObject()
{
$data = new \stdClass();
$parameters = TicketParameter::select('key', 'value')->get();
foreach ($parameters as $parameter) {
$key = $parameter->key;
$value = $parameter->value;
$data->$key = $value;
}
return $data;
}
public function saveParameters(Request $request)
{
try {
$this->createOrUpdateParam($request, 'number_transfers');
$this->createOrUpdateParam($request, 'send_mail_tickets_app_purchase');
$this->createOrUpdateParam($request, 'send_mail_tickets_web_purchase');
$this->createOrUpdateParam($request, 'enable_transfer_ticket');
$this->createOrUpdateParam($request, 'enable_download_button');
$this->createOrUpdateParam($request, 'enable_tyc');
$this->createOrUpdateParam($request, 'ticket_button');
$this->createOrUpdateParam($request, 'subscriber_button');
$this->createOrUpdateParam($request, 'enable_validate_doors');
if ($request->file('terms')) {
$extension = $request->file('terms')->getClientOriginalExtension();
$filenametostore = 'ticket_terms' . '.' . $extension;
Storage::disk('s3')->put(config('s3.default') . '/ticket/' . $filenametostore, fopen($request->file('terms'), 'r+'), 'public');
$url = config('filesystems.disks.s3.url') . '/ticket/' . $filenametostore;
TicketParameter::updateOrCreate(
['key' => 'terms'],
['key' => 'terms', 'value' => $url]
);
}
if ($request->file('app_ticketing_background')) {
$filenametostore = 'subscriber.png';
Storage::disk('s3')->put(config('s3.default') . '/ticket/' . $filenametostore, fopen($request->file('app_ticketing_background'), 'r+'), 'public');
$url = config('filesystems.disks.s3.url') . '/ticket/' . $filenametostore;
TicketParameter::updateOrCreate(
['key' => 'app_ticketing_background'],
['key' => 'app_ticketing_background', 'value' => $url]
);
}
if ($request->file('web_ticketing_background')) {
$filenametostore = 'webTicketingBackground.jpg';
Storage::disk('s3')->put(config('s3.default') . '/web_customization/' . $filenametostore, fopen($request->file('web_ticketing_background'), 'r+'), 'public');
$url = config('filesystems.disks.s3.url') . '/web_customization/' . $filenametostore;
TicketParameter::updateOrCreate(
['key' => 'web_ticketing_background'],
['key' => 'web_ticketing_background', 'value' => $url]
);
}
Cache::forget('parametersTickets');
return array('r' => true, 'd' => null, 'm' => __('messages.updated_successfully'));
} catch (\Throwable $th) {
return array('r' => false, 'd' => null, 'm' => __('messages.error_updating'));
}
}
public function validateSendingEmailTickets($origin)
{
$sendMail = false;
if ($origin == 'app') {
$sendMail = $this->getParametersValue('send_mail_tickets_app_purchase');
} else if ($origin == 'web') {
$sendMail = $this->getParametersValue('send_mail_tickets_web_purchase');
}
$sendMail = filter_var($sendMail, FILTER_VALIDATE_BOOLEAN);
return $sendMail;
}
public function getParametersValue($key)
{
$parameter = TicketParameter::where('key', $key)->first();
return $parameter ? $parameter->value : '';
}
private function createOrUpdateParam(Request $request, $key)
{
if ($request[$key]) {
TicketParameter::updateOrCreate(
['key' => $key],
['key' => $key, 'value' => $request[$key]]
);
}
}
}