DataController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 20
ccs 0
cts 10
cp 0
rs 9.9666
cc 1
nc 1
nop 9
crap 2

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace App\Http\Controllers\API;
4
5
use App\Application;
6
use App\Repositories\InteractionRepository;
7
use App\Repositories\PlaylistRepository;
8
use App\Repositories\SettingRepository;
9
use App\Repositories\UserRepository;
10
use App\Services\ApplicationInformationService;
11
use App\Services\iTunesService;
12
use App\Services\LastfmService;
13
use App\Services\MediaCacheService;
14
use App\Services\YouTubeService;
15
use Illuminate\Http\JsonResponse;
16
use Illuminate\Http\Request;
17
18
/**
19
 * @group 2. Application data
20
 */
21
class DataController extends Controller
22
{
23
    private const RECENTLY_PLAYED_EXCERPT_COUNT = 7;
24
25
    private $lastfmService;
26
    private $youTubeService;
27
    private $iTunesService;
28
    private $mediaCacheService;
29
    private $settingRepository;
30
    private $playlistRepository;
31
    private $interactionRepository;
32
    private $userRepository;
33
    private $applicationInformationService;
34
35
    public function __construct(
36
        LastfmService $lastfmService,
37
        YouTubeService $youTubeService,
38
        iTunesService $iTunesService,
39
        MediaCacheService $mediaCacheService,
40
        SettingRepository $settingRepository,
41
        PlaylistRepository $playlistRepository,
42
        InteractionRepository $interactionRepository,
43
        UserRepository $userRepository,
44
        ApplicationInformationService $applicationInformationService
45
    ) {
46
        $this->lastfmService = $lastfmService;
47
        $this->youTubeService = $youTubeService;
48
        $this->iTunesService = $iTunesService;
49
        $this->mediaCacheService = $mediaCacheService;
50
        $this->settingRepository = $settingRepository;
51
        $this->playlistRepository = $playlistRepository;
52
        $this->interactionRepository = $interactionRepository;
53
        $this->userRepository = $userRepository;
54
        $this->applicationInformationService = $applicationInformationService;
55
    }
56
57
    /**
58
     * Get application data.
59
     *
60
     * The big fat call to retrieve a set of application data catered for the current user
61
     * (songs, albums, artists, playlists, interactions, and if the user is an admin, settings as well).
62
     * Naturally, this call should be made right after the user has been logged in, when you need to populate
63
     * the application's interface with useful information.
64
     *
65
     * @responseFile responses/data.index.json
66
     *
67
     * @return JsonResponse
68
     */
69
    public function index(Request $request)
70
    {
71
        return response()->json($this->mediaCacheService->get() + [
72
            'settings' => $request->user()->is_admin ? $this->settingRepository->getAllAsKeyValueArray() : [],
73
            'playlists' => $this->playlistRepository->getAllByCurrentUser(),
74
            'interactions' => $this->interactionRepository->getAllByCurrentUser(),
75
            'recentlyPlayed' => $this->interactionRepository->getRecentlyPlayed(
76
                $request->user(),
77
                self::RECENTLY_PLAYED_EXCERPT_COUNT
78
            ),
79
            'users' => $request->user()->is_admin ? $this->userRepository->getAll() : [],
80
            'currentUser' => $request->user(),
81
            'useLastfm' => $this->lastfmService->used(),
82
            'useYouTube' => $this->youTubeService->enabled(),
83
            'useiTunes' => $this->iTunesService->used(),
84
            'allowDownload' =>  config('koel.download.allow'),
85
            'supportsTranscoding' => config('koel.streaming.ffmpeg_path')
86
                && is_executable(config('koel.streaming.ffmpeg_path')),
87
            'cdnUrl' => app()->staticUrl(),
0 ignored issues
show
Bug introduced by
The method staticUrl() does not exist on Illuminate\Container\Container. It seems like you code against a sub-type of Illuminate\Container\Container such as App\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

87
            'cdnUrl' => app()->/** @scrutinizer ignore-call */ staticUrl(),
Loading history...
88
            'currentVersion' => Application::KOEL_VERSION,
89
            'latestVersion' => $request->user()->is_admin
90
                ? $this->applicationInformationService->getLatestVersionNumber()
91
                : Application::KOEL_VERSION,
92
        ]);
93
    }
94
}
95