Completed
Push — master ( 6dc6fd...57abcf )
by Travis
13:02
created

Item::checkIssetImage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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