AdminController::addMovie()   B
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 49
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 49
ccs 0
cts 35
cp 0
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 28
nc 5
nop 0
crap 20
1
<?php
2
3
//namespace Admin;
4
5
use Illuminate\Support\Facades\Input;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Input.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use Krucas\Notification\Facades\Notification;
7
use Illuminate\Support\Facades\Redirect;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Redirect.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
use Carbon\Carbon;
9
use Goutte\Client;
10
use Symfony\Component\DomCrawler\Crawler;
11
class AdminController extends \AdminBaseController {
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...
12
13
	/**
14
	 * Admin Homepage
15
	 */
16 1
	public function index() {
17
		$stats = [
18 1
			'users' => User::count(),
19 1
			'leagues' => League::count(),
20 1
			'movies' => Movie::count(),
21 1
		];
22
23
24 1
		$this->layout->title = 'Admin';
25 1
		$this->layout->content = View::make('admin.index', compact('stats'));
26 1
	}
27
	
28
	public function movies() {
29
		$movies_query = Movie::query();
30
		$movies_query->orderBy('release', 'desc');
31
		$movies_query->select('movies.*');
32
		$movies = $movies_query->paginate(50);
33
		$this->layout->title = 'Movies';
34
		$this->layout->content = View::make('admin.movies',compact('movies'));
35
	}	
36
	public function addMovie() {
37
		$mojo_id = str_replace(".htm",'',Input::get('movie'));
38
		$info = new stdClass();
39
		$info->found = false;
40
		$info->mojo_id = $mojo_id;
41
		$info->old = null;
42
		$info->grosses = null;
43
		if ($mojo_id != '')
44
		{
45
			$client = new Client();
46
			$crawler = $client->request('GET', 'http://www.boxofficemojo.com/movies/?page=daily&view=chart&id='.$mojo_id.'.htm', [], [], [
47
				'HTTP_USER_AGENT' => "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0"
48
			]);
49
			$mojo_query = Movie::query();
50
			$mojo_query->where('boxmojo_id', $mojo_id);
51
			$query_result = $mojo_query->first();
52
			if ($query_result)
53
			{
54
				$info->old = $query_result;
55
			}
56
			
57
			$info->title = $crawler->filterXPath("//div[@id='body']//font[@face='Verdana']/b")->text();
58
			$info->release_date = new Carbon($crawler->filterXPath("//div[@id='body']//td[starts-with(.,'Release Date')]//b")->text());
59
			if (!$info->old)
60
			{
61
				$info->grosses = $crawler->filter('#body table[border="0"][class="chart-wide"] tr[bgcolor]:not([bgcolor="#dcdcdc"])')->each(function(Crawler $node) {
62
					$cols = $node->children();
63
64
					$myinfo = [
65
						'release_date'   => new Carbon($cols->eq(1)->text()),
66
						'domestic_total' => intval(str_replace(['$', ','], '', $cols->eq(8)->text())),
67
					];
68
					
69
					return $myinfo;
70
				});
71
			}
72
			
73
			$info->found = true;
74
			
75
			
76
			//Notification::success($title." ".$release_date." ".$boxmojo_id);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
77
78
			//return Redirect::route('admin.movies');			
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
79
		}
80
81
		$this->layout->content = View::make('admin.addmovie',[
82
			'info' => $info
83
		]);
84
	}
85
	
86
	public function confirmMovie() {
87
		// Create the league
88
		$old_id = Input::get('old_id');
89
		if ($old_id) {
90
			$movie_query = Movie::query();
91
			$movie_query->where('id',$old_id);
92
			$movie = $movie_query->first();
93
		}
94
		else
95
		{
96
			$movie = new Movie(Input::only([
97
				'title', 'release_date', 'boxmojo_id'
98
			]));			
99
		}
100
		$movie->name = Input::get('title');
101
		$movie->release = Input::get('release_date');
102
		$movie->boxmojo_id = Input::get('mojo_id');
103
		
104
		$movie->save();
105
		
106
		$earnings = unserialize(Input::get('grosses'));
107
		$count = 0;
108
		if ($earnings)
109
		{
110
			$latest_earnings_id = 0;
111
			
112
			foreach($earnings as $gross) { 
113
				$earnings = new MovieEarning([
114
					'movie_id' => $movie->id,
115
					'date'     => $gross['release_date'],
116
					'domestic' => $gross['domestic_total']
117
				]);
118
				$earnings->save();
119
				$latest_earnings_id = $earnings->id;
120
				$count += 1;
121
			}
122
			if($latest_earnings_id != 0) {
123
				$movie->latest_earnings_id = $latest_earnings_id;
124
				$movie->save();
125
			}			
126
		}
127
		
128
		
129
		Notification::success("Successfully ".($old_id ? "updated " : "added ").$movie->name." with ".$count." day(s) of earnings");
130
131
		return Redirect::route('admin.addMovie');
132
133
	}
134
	
135
	
136
}