Completed
Push — master ( 542498...7eecb4 )
by Kirill
03:23
created

UsersController::getUsersTop()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 24
rs 8.9714
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of GitterBot package.
4
 *
5
 * @author Serafim <[email protected]>
6
 * @date 14.10.2015 11:21
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace App\Http\Controllers\Api;
12
13
use App\User;
14
use App\Karma;
15
use App\Http\Controllers\Controller;
16
use Illuminate\Database\Eloquent\Relations\HasMany;
17
18
/**
19
 * Class UsersController
20
 * @package App\Http\Controllers
21
 */
22
class UsersController extends Controller
23
{
24
    /**
25
     * @return \Illuminate\Database\Eloquent\Collection|static[]
26
     */
27
    public function index()
28
    {
29
        return \Cache::remember('users', 1, function () {
30
            $karmaStorage = [];
31
            $thanksStorage = [];
32
33
            (new Karma())
34
                ->selectRaw('user_target_id, count(user_id) as count')
35
                ->groupBy('user_target_id')
36
                ->get()
37
                ->each(function ($item) use (&$karmaStorage) {
38
                    $karmaStorage[$item->user_target_id] = $item->count;
39
                });
40
41
            (new Karma())
42
                ->selectRaw('user_id, count(user_target_id) as count')
43
                ->groupBy('user_id')
44
                ->get()
45
                ->each(function ($item) use (&$thanksStorage) {
46
                    $thanksStorage[$item->user_id] = $item->count;
47
                });
48
49
50
            return (new User())
51
                ->get(['id', 'login', 'name', 'gitter_id', 'avatar', 'url'])
52
                ->each(function (User $user) use ($karmaStorage, $thanksStorage) {
53
                    $user->karma_count = $karmaStorage[$user->id] ?? 0;
54
                    $user->thanks_count = $thanksStorage[$user->id] ?? 0;
55
                });
56
        });
57
    }
58
59
    /**
60
     * @return \Illuminate\Database\Eloquent\Collection|static[]
61
     */
62
    public function getUsersTop()
63
    {
64
        return \Cache::remember('top.karma', 1, function () {
65
            $karmaStorage = [];
66
67
            $karma = (new Karma())
68
                ->selectRaw('user_target_id, count(*) as count')
69
                ->groupBy('user_target_id')
70
                ->orderBy('count', 'desc')
71
                ->take(10)
72
                ->get()
73
                ->each(function ($item) use (&$karmaStorage) {
74
                    $karmaStorage[$item->user_target_id] = $item->count;
75
                });
76
77
78
            return (new User())
79
                ->whereIn('id', $karma->pluck('user_target_id'))
80
                ->get(['id', 'login', 'name', 'gitter_id', 'avatar', 'url'])
81
                ->each(function (User $user) use ($karmaStorage) {
82
                    $user->karma_count = $karmaStorage[$user->id] ?? 0;
83
                });
84
        });
85
    }
86
87
    /**
88
     * @return User
89
     */
90
    public function getUser($gitterId)
91
    {
92
        $formatRelations = function (HasMany $query) {
93
            return $query->orderBy('created_at', 'desc');
94
        };
95
96
        return User::query()
97
            ->with([
98
                'karma'        => $formatRelations,
99
                'thanks'       => $formatRelations,
100
                'achievements' => $formatRelations,
101
            ])
102
            ->where('gitter_id', $gitterId)
103
            ->first();
104
    }
105
}