Item::getBuy_amount()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * @property integer $id
4
 * @property string $item_type
5
 * @property integer $skill
6
 * @property integer $level
7
 * @property string $title
8
 * @property integer $price
9
 * @property integer $price_sell
10
 * @property integer $owned
11
 * @property integer $buy_amount
12
 * @property integer $sell_amount
13
 * @property array $errors
14
 * @property boolean $success
15
 */
16
class Item extends CModel
17
{
18
    const TYPE_BAIT = 'bait';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
19
    const TYPE_ITEM = 'item';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
20
    const TYPE_ITEMSET = 'itemset';
21
    const TYPE_PART = 'part';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
22
23
    private $id;
24
    private $item_type;
25
    private $skill;
26
    private $level;
27
    private $title;
28
    private $price;
29
    private $parts;
0 ignored issues
show
Unused Code introduced by
The property $parts is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
30
    private $owned;
31
    private $buy_amount = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
32
    private $sell_amount = [];
33
34
    private $errors = ['dollar'=>false, 'amount'=>false, 'owned'=>false, 'isLast'=>false, 'freeSlots'=>false];
35
    private $success;
36
37
    public function attributeNames()
38
    {
39
        return [];
40
    }
41
42
    public function getId()
43
    {
44
        return $this->id;
45
    }
46
47
    public function getItem_type()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
48
    {
49
        return $this->item_type;
50
    }
51
52
    public function getSkill()
53
    {
54
        return $this->skill;
55
    }
56
57
    public function getLevel()
58
    {
59
        return $this->level;
60
    }
61
62
    public function getTitle()
63
    {
64
        return $this->title;
65
    }
66
67
    public function getPrice()
68
    {
69
        return $this->price;
70
    }
71
72
    public function getPrice_sell()
73
    {
74
        return floor($this->price / 2);
75
    }
76
77
    public function getOwned()
78
    {
79
        return $this->owned;
80
    }
81
82
    public function getBuy_amount()
83
    {
84
        return $this->buy_amount;
85
    }
86
87
    public function getSell_amount()
88
    {
89
        return $this->sell_amount;
90
    }
91
92
    public function getErrors($attribute = NULL)
93
    {
94
        return $this->errors;
95
    }
96
97
    public function getSuccess()
98
    {
99
        return $this->success;
100
    }
101
102
    public function setId($id)
103
    {
104
        $this->id = (int)$id;
105
    }
106
107
    public function setItem_type($type)
108
    {
109
        $this->item_type = $type;
110
    }
111
112
    public function setOwned($owned)
113
    {
114
        $this->owned = (int)$owned;
115
    }
116
117
    public function fetch()
118
    {
119
        if (!$this->id) {
120
            return false;
121
        }
122
123
        $uid = Yii::app()->player->uid;
124
125
        //read all from db
126
        $dependency = new CExpressionDependency("Yii::app()->params['{$this->item_type}s_version']");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
127
        $res = Yii::app()->db->cache(Yii::app()->params['cacheDuration'], $dependency)->createCommand()
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
128
            ->select('*')
129
            ->from($this->item_type.'s')
130
            ->where('id=:id', [':id'=>$this->id])
131
            ->queryRow();
132
133
        if (!$res) {
134
            return false;
135
        }
136
137
        foreach ($res as $k => $v) {
138
            if ($k == 'id') {
139
                continue;
140
            }
141
142
            $this->$k = $v;
143
        }
144
145
        $own = Yii::app()->db->createCommand()
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
146
            ->select('item_count')
147
            ->from('users_'.$this->item_type.'s')
148
            ->where('uid=:uid and item_id=:item_id', [':uid'=>$uid, ':item_id'=>$this->id])
149
            ->queryRow();
150
        $this->owned = (int)$own['item_count'];
151
152
        $this->setBuyAmount();
153
        $this->setSellAmount();
154
    }
155
156
    public function fetchSet()
157
    {
158
        if (!$this->id) {
159
            return false;
160
        }
161
162
        $uid = Yii::app()->player->uid;
163
164
        $combinedId = (string)$this->id;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
165
        $setId = (int)substr($combinedId, 0, -3);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
166
        $this->level = (int)substr($combinedId, -3);
167
168
169
        //read all from db
170
        $dependency = new CExpressionDependency("Yii::app()->params['{$this->item_type}s_version']");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
171
        $res = Yii::app()->db->cache(Yii::app()->params['cacheDuration'], $dependency)->createCommand()
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
172
            ->select('*')
173
            ->from('itemsets')
174
            ->where('id=:id', [':id'=>$setId])
175
            ->queryRow();
176
177
        if (!$res) {
178
            return false;
179
        }
180
181
        foreach ($res as $k => $v) {
182
            if ($k == 'id') {
183
                continue;
184
            }
185
186
            $this->$k = $v;
187
        }
188
189
        $own = Yii::app()->db->createCommand()
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
190
            ->select('skill, item_count, price')
191
            ->from('users_items')
192
            ->where('uid=:uid and item_id=:item_id', [':uid'=>$uid, ':item_id'=>$this->id])
193
            ->queryRow();
194
        $this->owned = (int)$own['item_count'];
195
196
        //read skill from users tbl
197
        $this->title = $this->level . '. szintű ' . $this->title;
198
        $this->skill = (int)$own['skill'];
199
        $this->price = (int)$own['price'];
200
        $this->level = 1;
201
202
        $this->setBuyAmount();
203
        $this->setSellAmount();
204
    }
205
206
    /**
207
     * @param integer $amount
208
     */
209
    public function buy($amount)
210
    {
211
        $decr = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
212
        $amount = (int)$amount;
213
        if ($amount < 1) {
214
            $this->errors['amount'] = true;
215
            return false;
216
        }
217
218 View Code Duplication
        if ($this->price * $amount > Yii::app()->player->model->dollar) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
219
            $this->errors['dollar'] = true;
220
            return false;
221
        }
222
223 View Code Duplication
        if ($this->item_type !== self::TYPE_PART) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
224
            if ($amount > Yii::app()->player->model->freeSlots) {
225
                $this->errors['freeSlots'] = true;
226
                return false;
227
            }
228
        }
229
230
        $uid = Yii::app()->player->uid;
231
232
        //add to inventory
233
        $update = Yii::app()->db
234
            ->createCommand("UPDATE users_{$this->item_type}s SET item_count=item_count+:amount WHERE uid=:uid AND item_id=:item_id")
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
235
            ->bindValues([':uid'=>$uid, ':item_id'=>(int)$this->id, ':amount'=>$amount])
236
            ->execute();
237
238 View Code Duplication
        if (!$update) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
239
            Yii::app()->db->createCommand()
240
                ->insert('users_'.$this->item_type.'s', [
241
                'uid'=>$uid,
242
                'item_id'=>(int)$this->id,
243
                'item_count'=>$amount,
244
                'skill'=>(int)$this->skill,
245
                ]);
246
        }
247
248
        //pay for it
249
        if ($this->price > 0) {
250
            $decr['dollar'] = $amount * $this->price;
251
            Yii::app()->player->model->updateAttributes([], $decr);
252
        }
253
254
        $this->success = true;
255
        $this->owned += $amount;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
256
        $this->setBuyAmount();
257
    }
258
259
    public function sell($amount)
260
    {
261
        $incr = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
262
        $amount = (int)$amount;
263
        if ($amount < 1) {
264
            $this->errors['amount'] = true;
265
            return false;
266
        }
267
268
        if ($this->owned < $amount) {
269
            $this->errors['owned'] = true;
270
            return false;
271
        }
272
273
        $player = Yii::app()->player->model;
274
275
        $owned = $this->item_type == self::TYPE_BAIT ? $player->owned_baits : $player->owned_items;
276
        if ($owned == 1) {
277
            $this->errors['isLast'] = true;
278
            return false;
279
        }
280
281
        //remove from inventory
282
        Yii::app()->db
283
            ->createCommand("UPDATE users_{$this->item_type}s SET item_count=item_count-:amount WHERE uid=:uid AND item_id=:item_id")
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
284
            ->bindValues([':uid'=>$player->uid, 'item_id'=>(int)$this->id, ':amount'=>$amount])
285
            ->execute();
286
287
        //give money for it
288
        $incr['dollar'] = $amount * $this->price_sell;
289
        Yii::app()->player->model->updateAttributes($incr, []);
290
291
        $this->success = true;
292
        $this->owned -= $amount;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
293
        $this->setSellAmount();
294
295
    }
296
297
    private function setBuyAmount()
298
    {
299
        foreach ([1,5,10] as $amount) {
300
            $this->buy_amount[$amount] = (bool)($this->price * $amount <= Yii::app()->player->model->dollar);
301
        }
302
    }
303
304
    private function setSellAmount()
305
    {
306
        foreach ([1,5,10] as $amount) {
307
            $this->sell_amount[$amount] = (bool)($this->owned >= $amount);
308
        }
309
    }
310
311
    public function __toString()
312
    {
313
        $attributes = ['id','item_type','skill','level','price','title','owned'];
314
        $ret = '';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
315
        foreach ($attributes as $attribute) {
316
            $ret .= $attribute . ': ' . $this->$attribute . "\n";
317
        }
318
        return $ret;
319
    }
320
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
321