MovieScraperWorker::getMovies()   B
last analyzed

Complexity

Conditions 5
Paths 1

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 0
cts 25
cp 0
rs 8.439
c 0
b 0
f 0
cc 5
eloc 22
nc 1
nop 0
crap 30
1
<?php
2
3
use Goutte\Client;
4
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
5
use Illuminate\Queue\Jobs\Job;
6
use Illuminate\Support\Collection;
7
use Symfony\Component\DomCrawler\Crawler;
8
9
class MovieScraperWorker {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
11
	/**
12
	 * HTTP client for requests
13
	 *
14
	 * @var Client
15
	 */
16
	protected $client;
17
18
	/**
19
	 * URL to scrape
20
	 *
21
	 * @var string
22
	 */
23
	protected $url = 'http://pro.boxoffice.com/statistics/release-calendar?display_all=yes';
24
25
	/**
26
	 * @param Client $client
27
	 */
28
	function __construct(Client $client) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
29
		$this->client = $client;
30
	}
31
32
	/**
33
	 * Scrape for new movie data
34
	 *
35
	 * @param Job $job
36
	 * @param     $data
37
	 */
38
	public function fire(Job $job, $data) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
39
		$movies = $this->getMovies();
40
41
		$this->scanDatabaseForChanges($movies);
0 ignored issues
show
Bug introduced by
It seems like $movies defined by $this->getMovies() on line 39 can also be of type array<integer,array>; however, MovieScraperWorker::scanDatabaseForChanges() does only seem to accept object<Illuminate\Support\Collection>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
42
43
		Log::info('Releases researched', [count($movies)]);
44
45
		$job->delete();
46
	}
47
48
	/**
49
	 * Get the upcoming movies
50
	 *
51
	 * @return Collection|array[]
52
	 */
53
	private function getMovies() {
54
		// Limit the scraped movie release date to the max length of the league
55
		$limit = Carbon::now()->addWeeks(Config::get('draft.maximum_weeks'));
56
57
		$crawler = $this->client->request('GET', $this->url, []);
58
		// Generate a list of movies
59
		$movies = $crawler->filter('.data_table tbody tr')->each(function (Crawler $node) use ($limit) {
60
61
			$cols = $node->children();
62
63
			$release_date = Carbon::createFromFormat('M j, Y|', trim($cols->eq(0)->text()));
64
			if ($release_date->gt($limit)) {
65
				return false;
66
			}
67
68
			try {
69
				$info = [
70
					'release'      => $release_date,
71
					'name'         => $cols->eq(1)->children()->eq(0)->text(),
72
					'boxoffice_id' => str_replace('/statistics/movies/', '', $cols->eq(1)->children()->eq(0)
73
					                                                              ->attr('href'))
74
				];
75
76
			} catch (Exception $e) {
77
				return false;
78
			}
79
			// If anything is missing, return false
80
			$valid = array_reduce($info, function ($state, $item) {
81
					return $state && $item;
82
				}, true);
83
84
			return $valid ? $info : false;
85
		});
86
87
		$movies = new Collection(array_filter($movies));
88
89
		return $movies;
90
	}
91
92
	/**
93
	 * Scan database for new movies and add them to the database
94
	 *
95
	 * @param Collection $movies
0 ignored issues
show
Bug introduced by
There is no parameter named $movies. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
96
	 */
97
	private function scanDatabaseForChanges(Collection $new_data) {
0 ignored issues
show
Coding Style Naming introduced by
The parameter $new_data is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
98
		$new_data = $new_data->keyBy('boxoffice_id');
99
100
		$movies = Movie::whereIn('boxoffice_id', $new_data->keys())->get();
101
102
		$movies_by_id = $movies->keyBy('boxoffice_id');
103
		$new_movies = new Collection(array_diff_key($new_data->toArray(), $movies_by_id->toArray()));
104
105
		$this->updateMovies($new_data, $movies_by_id);
106
		$this->addMovies($new_movies);
107
	}
108
109
	/**
110
	 * Update current movies with new data
111
	 *
112
	 * @param Collection         $new_data
113
	 * @param EloquentCollection $current_data
114
	 */
115
	private function updateMovies(Collection $new_data, EloquentCollection $current_data) {
0 ignored issues
show
Coding Style Naming introduced by
The parameter $new_data is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The parameter $current_data is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
116
		$current_data->map(function(Movie $movie) use($new_data) {
117
			$data = $new_data->get($movie->boxoffice_id);
118
119
			$movie->name = $data['name'];
120
			$movie->release = $data['release'];
121
122
			if($movie->isDirty()) {
123
				$movie->save();
124
			}
125
		});
126
	}
127
128
	/**
129
	 * Add new movies
130
	 *
131
	 * @param Collection $new_movies
132
	 */
133
	private function addMovies(Collection $new_movies) {
0 ignored issues
show
Coding Style Naming introduced by
The parameter $new_movies is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
134
		$new_movies->map(function($data) {
135
			$movie = Movie::create($data);
0 ignored issues
show
Unused Code introduced by
$movie 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...
136
		});
137
	}
138
139
140
}