Completed
Push — Laravel4 ( 02d48b...8c5149 )
by Travis
03:47 queued 03:02
created

SteamId::convertId()   C

Complexity

Conditions 10
Paths 10

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 21
rs 6.6746
cc 10
eloc 17
nc 10
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace Syntax\SteamApi;
2
3
use Syntax\SteamApi\Exceptions\UnrecognizedId;
4
5
trait SteamId {
6
7
	public         $formatted;
8
9
	private        $rawValue;
10
11
	private static $ID32     = 'id32';
12
13
	private static $ID64     = 'id64';
14
15
	private static $ID3      = 'id3';
16
17
	private static $id64Base = '76561197960265728';
18
19
	/**
20
	 * @param string|int  $id
21
	 * @param string|null $format
22
	 *
23
	 * @return mixed
24
     */
25
    public function convertId($id, $format = null)
26
	{
27
		$this->convertToAll($id);
28
29
		switch ($format) {
30
			case 'ID32':
31
			case 'id32':
32
			case 32:
33
				return $this->formatted->{self::$ID32};
34
			case 'ID64':
35
			case 'id64':
36
			case 64:
37
				return $this->formatted->{self::$ID64};
38
			case 'ID3':
39
			case 'id3':
40
			case 3:
41
				return $this->formatted->{self::$ID3};
42
			default:
43
				return $this->formatted;
44
		}
45
	}
46
47
	protected function setUpFormatted()
48
	{
49
		$this->formatted                = new \stdClass();
50
		$this->formatted->{self::$ID32} = null;
51
		$this->formatted->{self::$ID64} = null;
52
		$this->formatted->{self::$ID3}  = null;
53
	}
54
55
	private function convertToAll($id)
56
	{
57
		list($type, $matches) = $this->determineIDType($id);
58
59
		$this->getRawValue($id, $type, $matches);
60
61
		// Convert to each type
62
		$this->convertToID32();
63
		$this->convertToID64();
64
		$this->convertToID3();
65
66
		return $this->formatted;
67
	}
68
69
	private function convertToID32()
70
	{
71
		$z                              = bcdiv($this->rawValue, '2', 0);
72
		$y                              = bcmul($z, '2', 0);
73
		$y                              = bcsub($this->rawValue, $y, 0);
74
		$formatted                      = "STEAM_1:$y:$z";
75
		$this->formatted->{self::$ID32} = $formatted;
76
	}
77
78
	private function convertToID64()
79
	{
80
		$formatted                      = bcadd($this->rawValue, self::$id64Base, 0);
81
		$this->formatted->{self::$ID64} = $formatted;
82
	}
83
84
	private function convertToID3()
85
	{
86
		$formatted                     = "[U:1:$this->rawValue]";
87
		$this->formatted->{self::$ID3} = $formatted;
88
	}
89
90
	private function determineIDType($id)
91
	{
92
		$id = trim($id);
93
94
		if (preg_match('/^STEAM_[0-1]:([0-1]):([0-9]+)$/', $id, $matches)) {
95
			return ['ID32', $matches];
96
		}
97
		if (preg_match('/^[0-9]+$/', $id)) {
98
			return ['ID64', null];
99
		}
100
		if (preg_match('/^\[U:1:([0-9]+)\]$/', $id, $matches)) {
101
			return ['ID3', $matches];
102
		}
103
104
		throw new UnrecognizedId('Id [' . $id . '] is not recognized as a steam id.');
105
	}
106
107
	/**
108
	 * Get a raw value from any type of steam id
109
	 *
110
	 * @param $id
111
	 * @param $type
112
	 * @param $matches
113
	 */
114
	private function getRawValue($id, $type, $matches)
115
	{
116
		switch ($type) {
117
			case 'ID32':
118
				$this->rawValue = bcmul($matches[2], '2', 0);
119
				$this->rawValue = bcadd($this->rawValue, $matches[1], 0);
120
121
				$this->formatted->{self::$ID32} = $id;
122
123
				break;
124
			case 'ID64':
125
				$this->rawValue = bcsub($id, self::$id64Base, 0);
126
127
				$this->formatted->{self::$ID64} = $id;
128
129
				break;
130
			case 'ID3':
131
				$this->rawValue = $matches[1];
132
133
				$this->formatted->{self::$ID3} = $id;
134
135
				break;
136
		}
137
	}
138
139
}
140