Completed
Pull Request — Laravel4 (#36)
by
unknown
10:35
created

Game::convertFromMinutes()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 29
rs 8.8571
cc 3
eloc 15
nc 4
nop 1
1
<?php namespace Syntax\SteamApi\Containers;
2
3
class Game extends BaseContainer {
4
	public $appId;
5
6
	public $name;
7
8
	public $playtimeTwoWeeks;
9
10
	public $playtimeForever;
11
12
	public $playtimeForeverReadable;
13
14
	public $icon;
15
16
	public $logo;
17
18
	public $header;
19
20
	public $hasCommunityVisibleStats;
21
22
	public function __construct($app)
23
	{
24
		$this->appId                    = $app->appid;
25
		$this->name                     = $this->checkIssetField($app, 'name');
26
		$this->playtimeTwoWeeks         = isset($app->playtime_2weeks) ? $this->convertFromMinutes($app->playtime_2weeks) : '0 minutes';
27
		$this->playtimeForever          = $this->checkIssetField($app, 'playtime_forever', 0);
28
		$this->playtimeForeverReadable  = $this->convertFromMinutes($this->playtimeForever);
29
		$this->icon                     = $this->checkIssetImage($app, 'img_icon_url');
30
		$this->logo                     = $this->checkIssetImage($app, 'img_logo_url');
31
		$this->header                   = 'http://cdn.steampowered.com/v/gfx/apps/'. $this->appId .'/header.jpg';
32
		$this->hasCommunityVisibleStats = $this->checkIssetField($app, 'has_community_visible_stats', 0);
33
	}
34
35
    /**
36
	 * @param        $app
37
	 * @param string $field
38
	 * @param string $value
39
	 *
40
	 * @return null|string
41
	 */
42
    protected function checkIssetImage($app, $field, $value = null)
43
	{
44
		return isset($app->$field) ? $this->getImageForGame($app->appid, $app->$field) : $value;
45
	}
46
47
	protected function getImageForGame($appId, $hash)
48
	{
49
		if ($hash != null) {
50
			return 'http://media.steampowered.com/steamcommunity/public/images/apps/'. $appId .'/'. $hash .'.jpg';
51
		}
52
53
		return null;
54
	}
55
56
	protected function convertFromMinutes($minutes)
57
	{
58
		$seconds = $minutes * 60;
59
60
		$secondsInAMinute = 60;
61
		$secondsInAnHour  = 60 * $secondsInAMinute;
62
		$secondsInADay    = 24 * $secondsInAnHour;
63
64
		// extract days
65
		$days = floor($seconds / $secondsInADay);
66
67
		// extract hours
68
		$hourSeconds = $seconds % $secondsInADay;
69
		$hours = floor($hourSeconds / $secondsInAnHour);
70
71
		// extract minutes
72
		$minuteSeconds = $hourSeconds % $secondsInAnHour;
73
		$minutes = floor($minuteSeconds / $secondsInAMinute);
74
75
		// return the final string
76
		$output = '';
77
78
		if ($days > 0) $output .= $days .' days ';
79
		if ($hours > 0) $output .= $hours .' hours ';
80
81
		$output .= $minutes .' minutes';
82
83
		return $output;
84
	}
85
86
}