Youtube   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 9
Bugs 4 Features 4
Metric Value
wmc 5
c 9
b 4
f 4
lcom 0
cbo 2
dl 0
loc 76
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B handle() 0 46 4
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Alaouy\Youtube\Facades\Youtube as Yt;
6
use Illuminate\Console\Command;
7
use Illuminate\Support\Facades\Log;
8
9
/**
10
 * Task to register Youtube API Videos
11
 * Class Youtube.
12
 */
13
class Youtube extends Command
14
{
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'youtube:import {keyword}';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Youtube import videos on Mongo';
28
29
    /**
30
     * Create a new command instance.
31
     */
32
    public function __construct()
33
    {
34
        parent::__construct();
35
    }
36
37
    /**
38
     * Execute the console command.
39
     *
40
     * @return mixed
41
     */
42
    public function handle()
43
    {
44
        $keyword = $this->argument('keyword');
45
46
        $channel = Yt::getChannelByName('allocine');
47
48
        if (!empty($channel)) {
49
            $manager = new \MongoDB\Driver\Manager('mongodb://localhost:27017');
50
            $collection = new \MongoDB\Collection($manager, 'laravel.stats');
0 ignored issues
show
Bug introduced by
The call to Collection::__construct() misses a required argument $collectionName.

This check looks for function calls that miss required arguments.

Loading history...
51
            $collection->deleteMany([]);
52
53
            $collection = new \MongoDB\Collection($manager, 'laravel.stats');
0 ignored issues
show
Bug introduced by
The call to Collection::__construct() misses a required argument $collectionName.

This check looks for function calls that miss required arguments.

Loading history...
54
            $stat = [
55
                'origin'  => 'Youtube',
56
                'type'    => 'search',
57
                'data'    => $channel,
58
                'created' => new  \MongoDB\BSON\UTCDatetime(time()),
59
            ];
60
            $collection->insertOne($stat);
61
        }
62
63
        $params = [
64
            'q'          => $keyword,
65
            'type'       => 'video',
66
            'part'       => 'id, snippet',
67
            'maxResults' => 30,
68
        ];
69
70
        $videos = Yt::searchAdvanced($params, true)['results'];
71
72
        if (!empty($videos)) {
73
            $collection = new \MongoDB\Collection($manager, 'laravel.videos');
0 ignored issues
show
Bug introduced by
The variable $manager does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The call to Collection::__construct() misses a required argument $collectionName.

This check looks for function calls that miss required arguments.

Loading history...
74
            $collection->deleteMany([]);
75
76
            foreach ($videos as $video) {
77
                $collection = new \MongoDB\Collection($manager, 'laravel.videos');
0 ignored issues
show
Bug introduced by
The call to Collection::__construct() misses a required argument $collectionName.

This check looks for function calls that miss required arguments.

Loading history...
78
                $stat = [
79
                    'data'    => $video,
80
                    'created' => new  \MongoDB\BSON\UTCDatetime(time()),
81
                ];
82
                $collection->insertOne($stat);
83
            }
84
        }
85
86
        Log::info("Import de l'API Youtube video done! ");
87
    }
88
}
89