EarningsScraperWorker::getEarnings()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 15
cp 0
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 1
crap 2
1
<?php
2
3
use Carbon\Carbon;
4
use Goutte\Client;
5
use Illuminate\Database\Eloquent\Collection;
6
use Illuminate\Queue\Jobs\Job;
7
use Symfony\Component\DomCrawler\Crawler;
8
9
class EarningsScraperWorker {
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
	 * @type Client
13
	 */
14
	private $client;
15
16
	/**
17
	 * @param Client $client
18
	 */
19
	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...
20
		$this->client = $client;
21
	}
22
23
	/**
24
	 * Scrape earnings
25
	 *
26
	 * @param Job $job
27
	 * @param     $data
28
	 */
29
	public function fire(Job $job, $data) {
30
		if ($job->attempts() > 3) {
31
			$job->delete();
32
33
			return;
34
		}
35
36
		/** @type Carbon $date */
37
		$date = $data['day'] instanceof Carbon ? $data['day'] : new Carbon($data['day']);
38
		list($dateFor, $earnings) = $this->getEarnings($date);
39
40
		$this->storeInteresting($dateFor, $earnings);
41
42
		$job->delete();
43
	}
44
45
	/**
46
	 * Get earnings data
47
	 *
48
	 * @param Carbon $date
49
	 *
50
	 * @return array
51
	 */
52
	private function getEarnings(Carbon $date) {
53
		$crawler = $this->client->request('GET', $this->getUrl($date), [], [], [
54
			'HTTP_USER_AGENT' => "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0"
55
		]);
56
57
		$result_date = new Carbon($crawler->filter('font[face="Verdana"][size="4"] > b')->text());
58
		$movies = $crawler->filter('#body table[border="0"][cellpadding="5"] tr:not([bgcolor="#dcdcdc"])')->each(function (Crawler $node) {
59
			$cols = $node->children();
60
61
			$url = $cols->eq(2)->children()->eq(0)->children()->eq(0)->attr('href');
62
63
			$info = [
64
				'boxmojo_id'   => str_replace('/movies/?page=daily&id=', '', str_replace('.htm', '', $url)),
65
				'domestic_total' => intval(str_replace(['$', ','], '', $cols->eq(9)->text())),
66
			];
67
68
			return $info;
69
		});
70
71
		return [$result_date, $movies];
72
	}
73
74
	/**
75
	 * Get the URL to scrape
76
	 *
77
	 * @param Carbon $date
78
	 *
79
	 * @return string
80
	 */
81
	private function getUrl(Carbon $date) {
82
		return 'http://www.boxofficemojo.com/daily/chart/?view=1day&sortdate=' . $date->toDateString();
83
	}
84
85
	/**
86
	 * Store
87
	 *
88
	 * @param Carbon $dateFor
89
	 * @param        $earnings
90
	 */
91
	private function storeInteresting(Carbon $dateFor, $earnings) {
92
		$movies = $this->getActiveMovies($dateFor)->keyBy('boxmojo_id');
93
94
		foreach ($earnings as $info) {
95
			if (! $movies->has($info['boxmojo_id'])) {
96
				continue;
97
			}
98
99
			/** @type Movie $movie */
100
			$movie = $movies->get($info['boxmojo_id']);
101
			$earnings = $movie->earnings()->where('date', $dateFor)->first();
102
			if (! $earnings) {
103
				$earnings = new MovieEarning([
104
					'movie_id' => $movie->id,
105
					'date'     => $dateFor,
106
				]);
107
			}
108
109
			$earnings->domestic = $info['domestic_total'];
110
111
			if ($earnings->isDirty()) {
112
				$earnings->save();
113
114
				if (! $movie->latestEarnings || $movie->latestEarnings->date < $earnings->date) {
115
					$movie->latest_earnings_id = $earnings->id;
116
					$movie->save();
117
				}
118
119
				// Find any LeagueMovies that might should be updated
120
				$leagueMovies = DB::table('league_movies')
121
				                  ->where('league_movies.movie_id', $movie->id)
122
				                  ->join('leagues', 'league_movies.league_id', '=', 'leagues.id')
123
				                  ->where('leagues.end_date', '>', $dateFor)
124
				                  ->get(['league_movies.id']); // . infront bypasses clearing of dots, which is actually needed here
125
126
				foreach ($leagueMovies as $leagueMovie) {
127
					Queue::push('UpdateLeagueMovieEarnings', [
128
						'league_movie_id' => $leagueMovie->id, 'earnings_id' => $earnings->id
129
					]);
130
				}
131
			}
132
133
		}
134
135
	}
136
137
	/**
138
	 * Get movies that are being used in an active League
139
	 *
140
	 * @param Carbon $dateFor
141
	 *
142
	 * @return Collection
143
	 */
144
	private function getActiveMovies(Carbon $dateFor) {
145
		$movies = Movie::query()
146
		               ->where('release', '<', $dateFor)
147
		               ->whereExists(function (\Illuminate\Database\Query\Builder $query) use ($dateFor) {
148
			               $query->select(DB::raw(1))
149
			                     ->from('league_movies')
150
			                     ->join('leagues', 'league_movies.league_id', '=', 'leagues.id')
151
			                     ->whereRaw('league_movies.movie_id = movies.id')
152
			                     ->where('leagues.end_date', '>', $dateFor);
153
		               })->get();
154
155
		return $movies;
156
	}
157
}