Completed
Push — master ( 3422f1...85afe8 )
by Benjamin
02:20
created

MediaProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 53
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A randomMedia() 0 6 1
B generateUrl() 0 23 6
A downloadMedia() 0 12 1
1
<?php
2
3
namespace Alpixel\Bundle\MediaBundle\DataFixtures\ORM;
4
5
use Alpixel\Bundle\MediaBundle\Services\MediaManager;
6
use Faker\Provider\Base as BaseProvider;
7
use Symfony\Component\HttpFoundation\File\File;
8
9
class MediaProvider extends BaseProvider
10
{
11
	protected $mediaManager;
12
13
	public function __construct(MediaManager $mediaManager) 
14
	{
15
		$this->mediaManager = $mediaManager;
16
	}
17
18
   public function randomMedia ($width = null, $height = null, $type = "color")
19
   {
20
		$file = $this->downloadMedia($this->generateUrl($width, $height, $type));
21
		$media = $this->mediaManager->upload($file);
22
		return $media;
23
   }
24
25
   protected function generateUrl ($width = null, $height = null, $type = "color")
26
   {
27
   		$url = "http://lorempixel.com/";
28
29
   		if ($type !== 'color') {
30
   			$url .= 'g/';
31
   		}
32
33
   		if($width === null && $height !== null) {
34
   			$width = round($height * 4 / 3);
35
   		} elseif ($width !== null && $height === null) {
36
   			$height = round($width * 3 / 4);
37
   		} else {
38
   			$width = rand(800, 1600);
39
   			$height = round($width * 3 / 4);
40
   		}
41
42
   		$url .= $width.'/'.$height;
43
44
   		$category = ['abstract', 'city', 'nature'];
45
   		$url .= '/'.$category[array_rand($category, 1)].'/';
46
   		return $url;
47
   }
48
49
   protected function downloadMedia($url) 
50
   {
51
   	$filepath = sys_get_temp_dir().'/tmp.jpg';
52
	$ch = curl_init($url);
53
	$fp = fopen($filepath,'wb');
54
	curl_setopt($ch, CURLOPT_FILE, $fp);
55
	curl_setopt($ch, CURLOPT_HEADER, 0);
56
	curl_exec($ch);
57
	curl_close($ch);
58
	fclose($fp);
59
	return new File($filepath, 'random');	
0 ignored issues
show
Documentation introduced by
'random' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
60
   }
61
}