Completed
Push — master ( a1f3ed...74c577 )
by Kirill
02:49
created

UsersController::index()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 31
rs 8.8571
cc 1
eloc 21
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 Interfaces\Http\Controllers\Api;
12
13
use Domains\User;
14
use Domains\Karma;
15
use Interfaces\Http\Controllers\Controller;
16
use Illuminate\Database\Eloquent\Relations\HasMany;
17
18
/**
19
 * Class UsersController
20
 */
21
class UsersController extends Controller
22
{
23
    /**
24
     * @return \Illuminate\Database\Eloquent\Collection|static[]
25
     */
26
    public function index()
27
    {
28
        return \Cache::remember('users', 1, function () {
29
            $karmaStorage = [];
30
            $thanksStorage = [];
31
32
            (new Karma())
33
                ->selectRaw('user_target_id, count(user_id) as count')
34
                ->groupBy('user_target_id')
35
                ->get()
36
                ->each(function ($item) use (&$karmaStorage) {
37
                    $karmaStorage[$item->user_target_id] = $item->count;
38
                });
39
40
            (new Karma())
41
                ->selectRaw('user_id, count(user_target_id) as count')
42
                ->groupBy('user_id')
43
                ->get()
44
                ->each(function ($item) use (&$thanksStorage) {
45
                    $thanksStorage[$item->user_id] = $item->count;
46
                });
47
48
49
            return (new User())
50
                ->get(['id', 'login', 'name', 'gitter_id', 'avatar', 'url'])
51
                ->each(function (User $user) use ($karmaStorage, $thanksStorage) {
52
                    $user->karma_count = $karmaStorage[$user->id] ?? 0;
53
                    $user->thanks_count = $thanksStorage[$user->id] ?? 0;
54
                });
55
        });
56
    }
57
58
    /**
59
     * @return \Illuminate\Database\Eloquent\Collection|static[]
60
     */
61
    public function getUsersTop()
62
    {
63
        return \Cache::remember('top.karma', 1, function () {
64
            $karmaStorage = [];
65
66
            $karma = (new Karma())
67
                ->selectRaw('user_target_id, count(*) as count')
68
                ->groupBy('user_target_id')
69
                ->orderBy('count', 'desc')
70
                ->take(10)
71
                ->get()
72
                ->each(function ($item) use (&$karmaStorage) {
73
                    $karmaStorage[$item->user_target_id] = $item->count;
74
                });
75
76
77
            return (new User())
78
                ->whereIn('id', $karma->pluck('user_target_id'))
79
                ->get(['id', 'login', 'name', 'gitter_id', 'avatar', 'url'])
80
                ->each(function (User $user) use ($karmaStorage) {
81
                    $user->karma_count = $karmaStorage[$user->id] ?? 0;
82
                });
83
        });
84
    }
85
86
    /**
87
     * @return User
88
     */
89
    public function getUser($gitterId)
90
    {
91
        $formatRelations = function (HasMany $query) {
92
            return $query->orderBy('created_at', 'desc');
93
        };
94
95
        return User::query()
96
            ->with([
97
                'karma'        => $formatRelations,
98
                'thanks'       => $formatRelations,
99
                'achievements' => $formatRelations,
100
            ])
101
            ->where('gitter_id', $gitterId)
102
            ->first();
103
    }
104
}