StatsService   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 81.25%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 47
c 4
b 1
f 0
dl 0
loc 73
ccs 39
cts 48
cp 0.8125
rs 10
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getClicks() 0 11 1
A __construct() 0 4 1
A getData() 0 18 3
B getDateFromat() 0 30 7
1
<?php
2
3
namespace Devpri\Tinre\Services;
4
5
use Carbon\Carbon;
6
use Exception;
7
use Illuminate\Support\Facades\DB;
8
use Illuminate\Validation\ValidationException;
9
10
class StatsService
11
{
12
    protected $driver;
13
14 22
    public function __construct()
15
    {
16 22
        $connection = config('database.default');
17 22
        $this->driver = config("database.connections.{$connection}.driver");
18 22
    }
19
20 7
    public function getClicks(int $urlId, string $startDate, string $endDate): array
21
    {
22 7
        $dates = DB::table('clicks')
23 7
            ->select(DB::raw("{$this->getDateFromat($startDate, $endDate)} as label"), DB::raw('count(*) as value'))
24 7
            ->where('url_id', $urlId)
25 7
            ->whereBetween('created_at', [$startDate, $endDate])
26 7
            ->groupBy('label')
27 7
            ->get()
28 7
            ->toArray();
29
30 7
        return $dates;
31
    }
32
33 9
    public function getData(string $column, int $urlId, string $startDate, string $endDate): object
34
    {
35 9
        if (! in_array($column, ['country', 'region', 'city', 'device_type', 'device_brand', 'device_model', 'os', 'browser', 'referer', 'referer_host'])) {
36 1
            throw ValidationException::withMessages([
37 1
                'column' => [__('Unsupported column.')],
38
            ]);
39
        }
40
41 8
        $dbFunction = $this->driver === 'pgsql' ? 'COALESCE' : 'IFNULL';
42
43 8
        $data = DB::table('clicks')->where('url_id', $urlId)
44 8
            ->select(DB::raw("{$dbFunction}({$column}, 'UNK') as label"), DB::raw('count(*) as value'))
45 8
            ->whereBetween('created_at', [$startDate, $endDate])
46 8
            ->groupBy('label')
47 8
            ->orderBy('value', 'DESC')
48 8
            ->paginate(10);
49
50 8
        return $data;
51
    }
52
53 7
    protected function getDateFromat(string $startDate, string $endDate): string
54
    {
55 7
        $startDate = Carbon::parse($startDate);
56 7
        $endDate = Carbon::parse($endDate);
57
58 7
        $diff = $startDate->diffInDays($endDate);
59
60 7
        if ($diff === 0) {
61
            $dateFormat = '%Y-%m-%d %H:00';
62
            $pgsqlDateFormat = 'YYYY-MM-DD HH24:00';
63 7
        } elseif ($diff > 1092) {
64
            $dateFormat = '%Y';
65
            $pgsqlDateFormat = 'YYYY';
66 7
        } elseif ($diff > 90) {
67
            $dateFormat = '%Y-%m';
68
            $pgsqlDateFormat = 'YYYY-MM';
69
        } else {
70 7
            $dateFormat = '%Y-%m-%d';
71 7
            $pgsqlDateFormat = 'YYYY-MM-DD';
72
        }
73
74 7
        switch ($this->driver) {
75 7
            case 'mysql':
76
                return "DATE_FORMAT(created_at, '{$dateFormat}')";
77 7
            case 'pgsql':
78
                return "to_char(created_at, '{$pgsqlDateFormat}')";
79 7
            case 'sqlite':
80 7
                return "strftime('{$dateFormat}', created_at)";
81
            default:
82
                throw new Exception('Unsupported driver.');
83
        }
84
    }
85
}
86