Completed
Push — master ( 5fb8f3...d597e6 )
by Julien
11:19
created

MainController::dashboard()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 54
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 13
Bugs 1 Features 11
Metric Value
c 13
b 1
f 11
dl 0
loc 54
rs 9.6716
cc 1
eloc 40
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Http\Models\Actors;
6
use App\Http\Models\Comments;
7
use App\Http\Models\Movies;
8
use App\Http\Models\Sessions;
9
use App\Http\Models\User;
10
use Illuminate\Http\Request;
11
use Illuminate\Support\Facades\DB;
12
use Illuminate\Support\Facades\Validator;
13
use Netshell\Paypal\Facades\Paypal;
14
15
/**
16
 * Class MainController.
17
 */
18
class MainController extends Controller
19
{
20
    /**
21
     * Fo Page.
22
     *
23
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
24
     */
25
    public function index()
26
    {
27
        return view('Main/index');
28
    }
29
30
    /**
31
     * Ajax Movies.
32
     *
33
     * @param Request $request
34
     *
35
     * @return mixed
36
     */
37
    public function ajaxmovies(Request $request)
38
    {
39
        $validator = Validator::make(
40
            $request->all(),  //request all : tous les elements de requetses
41
            [
42
            'title' => 'required|min:10',
43
            ], [
44
            'title.required' => 'Votre titre est obligatoire',
45
            'title.min' => 'Votre titre est trop court',
46
            ]);
47
48
        if ($validator->fails()) { // si mon validateur échoue
49
            return $validator->errors()->all();
50
        } else {
51
            Movies::create([
52
                'title' => $request->title,
53
                'description' => $request->description,
54
                'categories_id' => $request->categories_id,
55
            ]);
56
57
            return $request->title;
58
        }
59
    }
60
61
    /**
62
     * Done Payment.
63
     *
64
     * @param Request $request
65
     *
66
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
67
     */
68 View Code Duplication
    public function done(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70
        $id = $request->get('paymentId');
71
        $payer_id = $request->get('PayerID');
72
73
        $payment = PayPal::getById($id, $this->_apiContext);
0 ignored issues
show
Bug introduced by
The property _apiContext does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
74
75
        $paymentExecution = PayPal::PaymentExecution();
76
77
        $paymentExecution->setPayerId($payer_id);
78
        $executePayment = $payment->execute($paymentExecution, $this->_apiContext);
0 ignored issues
show
Unused Code introduced by
$executePayment is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
79
80
        // Clear the shopping cart, write to database, send notifications, etc.
81
        $request->session()->pull('likes', []);
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82
83
        return view('Main/index');
84
    }
85
86
    /**
87
     * Page Dashboard.
88
     */
89
    public function dashboard()
90
    {
91
        $nbacteurs = Actors::count();
92
        $nbcommentaires = Comments::count();
93
        $nbmovies = Movies::count();
94
        $nbseances = Sessions::count();
95
96
97
        $manager = new \MongoDB\Driver\Manager("mongodb://localhost:27017");
98
        $collection = new \MongoDB\Collection($manager, "laravel.videos");
99
        $videos = collect($collection->find()->toArray())->shuffle();
100
101
        $collection = new \MongoDB\Collection($manager, "laravel.stats");
102
        $youtubeinfo = collect($collection->find(["origin"  => "Youtube"])->toArray())->first();
103
104
        $collection = new \MongoDB\Collection($manager, "laravel.stats");
105
        $tweeterinfo = collect($collection->find(['origin' => 'Twitter', 'type' => 'infos'])->toArray())->first();
106
107
108
        $actor = new Actors(); // Je récpere mon modèle
109
        $comment = new Comments(); // Je récpere mon modèle
110
        $movie = new Movies(); // Je récpere mon modèle
111
        $session = new Sessions(); // Je récpere mon modèle
112
        $user = new User(); // Je récpere mon modèle
113
114
        $avgacteurs = $actor->getAvgActors();
115
        $avgnotecommentaire = $comment->getAvgNote();
116
        $avgnotepresse = $movie->getAvgNotePresse();
117
        $avghour = $session->getAvgHourDate();
118
119
        $seances = $session->getNextSession();
120
        $users = $user->getLastUsers();
121
122
123
124
        return view('Main/dashboard', [
125
            'avgnotecommentaire' => $avgnotecommentaire->avgnote,
126
            'avgnotepresse' => $avgnotepresse->avgpress,
127
            'avgacteurs' => $avgacteurs->age,
128
            'videos' => $videos,
129
            'video' => $videos[0],
130
            'youtubeinfo' => $youtubeinfo->data,
131
            'tweeterinfo' => $tweeterinfo['data'][0],
132
            'youtubeinfodateupdated' => $youtubeinfo->created,
133
            'tweeterinfodateupdated' => $tweeterinfo['created_at'],
134
            'avghour' => $avghour->avghour,
135
            'nbacteurs' => $nbacteurs,
136
            'nbcommentaires' => $nbcommentaires,
137
            'nbmovies' => $nbmovies,
138
            'nbseances' => $nbseances,
139
            'seances' => $seances,
140
            'users' => $users,
141
        ]);
142
    }
143
}
144