Sindbad~EG File Manager

Current Path : /proc/2302468/cwd/plugins/fluentform/app/Services/Analytics/
Upload File :
Current File : //proc/2302468/cwd/plugins/fluentform/app/Services/Analytics/AnalyticsService.php

<?php

namespace FluentForm\App\Services\Analytics;

use FluentForm\App\Models\FormAnalytics;
use FluentForm\App\Models\FormMeta;
use FluentForm\App\Services\Browser\Browser;
use FluentForm\Framework\Helpers\ArrayHelper;

class AnalyticsService
{
    
    public function reset($formId)
    {
        FormAnalytics::where('form_id', $formId)->delete();
        FormMeta::where('meta_key', '_total_views')->where('form_id',$formId)->delete();
        
        return ([
            'message' => __('Form Analytics has been successfully resetted', 'fluentform'),
        ]);
    }
    
    public function store($data)
    {
        $userId = null;
        if ($user = wp_get_current_user()) {
            $userId = $user->ID;
        }
        $browser = new Browser();
    
        $formId = ArrayHelper::get($data, 'form_id');
        $data = [
            'count'      => 1,
            'form_id'    => $formId,
            'user_id'    => $userId,
            'ip'         => ArrayHelper::get($data, 'ip'),
            'browser'    => $browser->getBrowser(),
            'platform'   => $browser->getPlatform(),
            'created_at' => current_time('mysql'),
            'source_url' => ArrayHelper::get($data, 'HTTP_REFERER', ''),
        ];
        $query = FormAnalytics::where('ip', $data['ip'])
            ->where('form_id', $data['form_id'])
            ->where('source_url', $data['source_url']);
    
        if (($record = $query->first())) {
            $query->update(['count' => ++$record->count]);
        } else {
            FormAnalytics::insert($data);
            $this->increaseTotalViews($formId);
        }
    
    }
    
    
    /**
     * Store (create/update) total view of a form
     *
     * @param int $formId
     */
    private function increaseTotalViews($formId)
    {
        $hasCount = FormMeta::where('meta_key', '_total_views')
            ->where('form_id', $formId)
            ->first();
        
        if ($hasCount) {
            FormMeta::where('id', $hasCount->id)
                ->update([
                    'value' => intval($hasCount->value) + 1,
                ]);
        } else {
            FormMeta::insert([
                'value'    => 1,
                'form_id'  => $formId,
                'meta_key' => '_total_views',
            ]);
        }
    }
    
}

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists