MovieEarningsTableSeeder   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 73
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B run() 0 30 4
B readFile() 0 21 5
1
<?php
2
3
class MovieEarningsTableSeeder extends Seeder {
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...
4
5
	/**
6
	 * Cache earnings in a static variable for fast re-seeding
7
	 * @var
8
	 */
9
	private static $earnings;
10
11
	/**
12
	 * Seed the Movie Earnings Table
13
	 */
14 9
	public function run() {
15
16 9
		if (! self::$earnings) {
17 1
			self::$earnings = $this->readFile(app_path('database/seeds/movie_earnings.csv'));
18 1
		}
19
20
		// Dump everything into the database, chunked due to amount of data
21 9
		foreach (array_chunk(self::$earnings, 199) as $chunk) {
22 9
			DB::table('movie_earnings')->insert($chunk);
23 9
		}
24
25
		// Get newest entries ID's to update movie's latest earnings ID's
26 9
		$latest = DB::table('movie_earnings')->select([DB::raw('max(id) as maxID'), 'movie_id'])
27 9
		            ->groupBy('movie_id')
28 9
		            ->get();
29
30 9
		$movies = Movie::all();
31
32
		/** @var stdClass $earning */
33 9
		foreach ($latest as $earning) {
34
			/** @var Movie $movie */
35 9
			$movie = $movies->find($earning->movie_id);
36 9
			$movie->latest_earnings_id = $earning->maxID;
37 9
		}
38
39
		// Save latest earnings ID's
40 9
		$movies->map(function (Movie $movie) {
41 9
			$movie->save();
42 9
		});
43 9
	}
44
45
46
	/**
47
	 * Reads a file and returns a generator
48
	 *
49
	 * @param      $path
50
	 * @param bool $hasHeader
51
	 *
52
	 * @return array
53
	 */
54 1
	public function readFile($path, $hasHeader = true) {
55 1
		$header = null;
56
57 1
		$data = [];
58
59 1
		if (($handle = fopen($path, 'r')) !== false) {
60 1
			while (($row = fgetcsv($handle)) !== false) {
61 1
				if (! $header && $hasHeader) {
62 1
					$header = $row;
63 1
				} else {
64 1
					$assoc = array_combine($header, $row);
65 1
					$assoc['created_at'] = $assoc['updated_at'] = Carbon::now();
66
67 1
					$data[] = $assoc;
68
				}
69 1
			}
70 1
			fclose($handle);
71 1
		}
72
73 1
		return $data;
74
	}
75
}