Shop::getId()   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 array $items
5
 * @property string $item_type
6
 * @property integer $page
7
 * @property integer $owned_baits
8
 * @property integer $owned_items
9
 * @property array $success
10
 * @property CPagination $pagination
11
 * @property integer $count
12
 * @property integer $transactionId
13
 * @property integer $levelLimit
14
 * @property integer $nextItemsLevel
15
 */
16
class Shop extends CModel
17
{
18
    const TYPE_BAIT = 'bait';
19
    const TYPE_ITEM = 'item';
20
    const TYPE_PART = 'part';
21
    
22
    private $id;
23
    private $items = [];
24
    private $item_type;
25
    private $page = 0;
26
    private $pagination;
27
    private $count;
28
    private $transactionId;
29
    private $success = ['setSold'=>false];
30
    
31
    public function attributeNames()
32
    {
33
        return [];
34
    }
35
    
36
    public function getId()
37
    {
38
        return $this->id;
39
    }
40
41
    public function getItems()
42
    {
43
        return $this->items;
44
    }
45
46
    public function getOwned_baits()
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...
47
    {
48
        return Yii::app()->player->model->owned_baits;
49
    }
50
51
    public function getOwned_items()
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...
52
    {
53
        return Yii::app()->player->model->owned_items;
54
    }
55
56
    public function getSuccess()
57
    {
58
        return $this->success;
59
    }
60
61
    public function getPagination()
62
    {
63
        return $this->pagination;
64
    }
65
66
    public function getCount()
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...
67
    {
68
        return $this->count;
69
    }
70
71
    public function getTransactionId()
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...
72
    {
73
        return $this->transactionId;
74
    }
75
76
    public function getLevelLimit()
77
    {
78
        $player = Yii::app()->player->model;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 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...
79
        $levelLimit = $player->level;
80
        if ($player->black_market) {
81
            $levelLimit += 2;
82
        }
83
        return (int)$levelLimit;
84
    }
85
    
86 View Code Duplication
    public function getNextItemsLevel()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
87
    {
88
        $nextLevel = Yii::app()->db->createCommand()
89
            ->select('level')
90
            ->from($this->item_type.'s')
91
            ->where('level > :level', [':level'=>Yii::app()->player->model->level])
92
            ->order('level ASC')
93
            ->limit(1)
94
            ->queryScalar();
95
        return (int)$nextLevel;
96
    }
97
98
    public function setItem_type($type)
99
    {
100
        $this->item_type = $type;
101
    }
102
103
    public function setPage($page)
104
    {
105
        $this->page = $page;
106
    }
107
108
    public function setId($id)
109
    {
110
        $this->id = (int)$id;
111
    }
112
    
113
    public function fetchSets()
114
    {
115
        $res = Yii::app()->db->createCommand()
116
            ->select('id, parts, title')
117
            ->from('itemsets')
118
            ->order('id DESC')
119
            ->queryAll();
120
         
121
        foreach ($res as $item) {
122
            $parts = explode(',', $item['parts']);
123
124
            $ownedOne = false;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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...
125
            $skill = 0;
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...
126
            $constructable = true;
127
            foreach ($parts as $part) {
128
                $i = new Item();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 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...
129
                $i->id = $part;
0 ignored issues
show
Bug introduced by
The property id cannot be accessed from this context as it is declared private in class Item.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
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...
130
                $i->item_type = $this->item_type;
0 ignored issues
show
Bug introduced by
The property item_type cannot be accessed from this context as it is declared private in class Item.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
131
                $i->fetch();
132
                $skill += $i->skill;
0 ignored issues
show
Bug introduced by
The property skill cannot be accessed from this context as it is declared private in class Item.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 15 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...
133
                $item['items'][$part] = $i;
134
135
                if ($i->owned) {
0 ignored issues
show
Bug introduced by
The property owned cannot be accessed from this context as it is declared private in class Item.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
136
                    $ownedOne = true;
137
                } else {
138
                    $constructable = false;
139
                }
140
141
            }
142
            $item['skill_multiplicator'] = $skill;
143
            $item['constructable'] = $constructable;
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...
144
            $item['sold'] = false;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 16 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...
145
            if ($ownedOne) {
146
                $this->items[$item['id']] = $item;
147
            }
148
        }
149
    }
150
    
151
    public function fetchItems()
152
    {
153
        $limit = Yii::app()->params['shopItemsPerPage'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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...
154
        $levelLimit = $this->levelLimit;
155
156
        $this->count = Yii::app()->db->createCommand()
157
            ->select('COUNT(*) AS count')
158
            ->from($this->item_type.'s')
159
            ->where('level <= :level', [':level'=>$levelLimit])
160
            ->queryScalar();
161
162
        $res = Yii::app()->db->createCommand()
163
            ->select('id')
164
            ->from($this->item_type.'s')
165
            ->where('level <= :level', [':level'=>$levelLimit])
166
            ->order('skill DESC, level DESC')
167
            ->limit($limit, ($this->page * $limit) - $limit) // the trick is here!
168
            ->queryAll();
169
         
170
        $this->pagination = new CPagination($this->count);
171
        $this->pagination->setPageSize(Yii::app()->params['shopItemsPerPage']);
172
                
173
        foreach ($res as $item) {
174
            $i = new Item();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 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...
175
            $i->id = $item['id'];
0 ignored issues
show
Bug introduced by
The property id cannot be accessed from this context as it is declared private in class Item.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
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...
176
            $i->item_type = $this->item_type;
0 ignored issues
show
Bug introduced by
The property item_type cannot be accessed from this context as it is declared private in class Item.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
177
            $i->fetch();
178
179
            $this->items[$item['id']] = $i;
180
        }
181
    }
182
183
    public function fetchPlayersItems()
184
    {
185
        $limit = Yii::app()->params['shopItemsPerPage'];
186
187
        $this->count = Yii::app()->db->createCommand()
188
            ->select('COUNT(*) AS count')
189
            ->from('users_'.$this->item_type.'s')
190
            ->where('uid=:uid AND item_count > 0', [':uid'=>Yii::app()->player->uid])
191
            ->queryScalar();
192
193
        $res = Yii::app()->db->createCommand()
194
            ->select('item_id')
195
            ->from('users_'.$this->item_type.'s')
196
            ->where('uid=:uid AND item_count > 0', [':uid'=>Yii::app()->player->uid])
197
            ->order('skill DESC, item_id DESC')
198
            ->limit($limit, ($this->page * $limit) - $limit) // the trick is here!
199
            ->queryAll();
200
         
201
        $this->pagination = new CPagination($this->count);
202
        $this->pagination->setPageSize(Yii::app()->params['shopItemsPerPage']);
203
                
204
        foreach ($res as $item) {
205
            $i = new Item();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 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...
206
            $i->id = $item['item_id'];
0 ignored issues
show
Bug introduced by
The property id cannot be accessed from this context as it is declared private in class Item.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
207
            if ($i->id < 1000) {
0 ignored issues
show
Bug introduced by
The property id cannot be accessed from this context as it is declared private in class Item.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
208
                $i->item_type = $this->item_type;
0 ignored issues
show
Bug introduced by
The property item_type cannot be accessed from this context as it is declared private in class Item.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
209
                $i->fetch();
210
            } else {
211
                $i->item_type = $this->item_type;
0 ignored issues
show
Bug introduced by
The property item_type cannot be accessed from this context as it is declared private in class Item.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
212
                $i->fetchSet();
213
            }
214
215
            $this->items[$item['item_id']] = $i;
216
        }
217
    }
218
    
219
    public function buyItem($id, $amount)
220
    {
221
        if (!isset($this->items[$id])) {
222
            return false;
223
        }
224
225
        $this->transactionId = $id;
226
        $i = $this->items[$id];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 19 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...
227
        $i->buy($amount);
228
        if ($i->success) {
229
            $b = Yii::app()->badge->model;
230
231 View Code Duplication
            if ($i->item_type == Item::TYPE_BAIT) {
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...
232
                Yii::app()->player->model->owned_baits = Yii::app()->player->model->owned_baits + $amount;
233
                $b->triggerBaits(Yii::app()->player->model->owned_baits);
234
            }
235 View Code Duplication
            if ($i->item_type == Item::TYPE_ITEM) {
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...
236
                Yii::app()->player->model->owned_items = Yii::app()->player->model->owned_items + $amount;
237
                $b->triggerItems(Yii::app()->player->model->owned_items);
238
            }
239
            
240
            (new Skill)->updateExtended();
241
        }
242
    }
243
    
244
    public function sellItem($id, $amount)
245
    {
246
        if (!isset($this->items[$id])) {
247
            return false;
248
        }
249
250
        $this->transactionId = $id;
251
        $i = $this->items[$id];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 19 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...
252
        $i->sell($amount);
253
        if ($i->success) {
254 View Code Duplication
            if ($i->item_type == Item::TYPE_BAIT) {
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...
255
                Yii::app()->player->model->owned_baits = Yii::app()->player->model->owned_baits - $amount;
256
            }
257
258 View Code Duplication
            if ($i->item_type == Item::TYPE_ITEM) {
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...
259
                Yii::app()->player->model->owned_items = Yii::app()->player->model->owned_items - $amount;
260
            }
261
262
            (new Skill)->updateExtended();
263
            
264
            $setId = $id > 999 ? $id[0] : 0;
265
            if ($setId) {
266
                Yii::app()->badge->model->triggerSet($setId, true);
267
            }
268
        }
269
    }
270
271
    public function constructItem($id)
272
    {
273
        if (!isset($this->items[$id])) {
274
            throw new CFlashException('A kiválasztott szett nem létezik.');
275
        }
276
277
        $set = $this->items[$id];
278
        if (!$set['constructable']) {
279
            throw new CFlashException('Nem rendelkezel a kiválasztott szett összes elemével.');
280
        }
281
282
        if (!Yii::app()->player->model->freeSlots) {
283
            throw new CFlashException('Nincs szabad helyed, most nem tudod összeszerelni a szettet. Tipp: adj el egy felszerelést vagy növeld meg a teherbírásodat.');
284
        }
285
286
        $player = Yii::app()->player->model;
287
        if ($player->energy < $player->energy_max) {
288
            throw new CFlashException('A szett elkészítéséhez az összes energiádra szükség lesz.');
289
        }
290
291
        foreach ($set['items'] as $i) {
292
            $remaining = $i->owned - 1;
293
            //remove from inventory
294
            Yii::app()->db
295
                ->createCommand("UPDATE users_parts SET item_count=:remaining WHERE uid=:uid AND item_id=:item_id")
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal UPDATE users_parts SET i...id AND item_id=:item_id does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
296
                ->bindValues([':uid'=>$player->uid, 'item_id'=>(int)$i->id, ':remaining'=>$remaining])
297
                ->execute();
298
            $set['items'][$i->id]->owned--;
299
        }
300
301
        //the id of new item
302
        $itemId = $set['id'] . sprintf('%03d', $player->level);
303
304
        //add created item to inventory
305
        $update = Yii::app()->db
306
            ->createCommand("UPDATE users_items SET item_count=item_count+:amount WHERE uid=:uid AND item_id=:item_id")
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal UPDATE users_items SET i...id AND item_id=:item_id does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
307
            ->bindValues([':uid'=>$player->uid, 'item_id'=>(int)$itemId, ':amount'=>1])
308
            ->execute();
309
        
310
        if (!$update) {
311
            $best = Yii::app()->db->createCommand()
312
                ->select('skill, price')
313
                ->from('items')
314
                ->where('level <= :level', [':level'=>Yii::app()->player->model->level])
315
                ->order('skill DESC, level DESC')
316
                ->limit(1)
317
                ->queryRow();
318
319
            Yii::app()->db->createCommand()
320
                ->insert('users_items', [
321
                'uid'=>$player->uid,
322
                'item_id'=>(int)$itemId,
323
                'item_count'=>1,
324
                'skill'=>(int)$best['skill'] * $set['skill_multiplicator'],
325
                'price'=>(int)$best['price'] * $set['skill_multiplicator'] * 2,
326
                ]);
327
        }
328
        $this->items[$id]['sold'] = true;
329
        $this->success['setSold'] = true;
330
        
331
        (new Skill)->updateExtended();
332
333
        //decrement energy
334
        $player->rewriteAttributes(['energy'=>0]);
335
        
336
        Yii::app()->badge->model->triggerSet($set['id']);
337
    }
338
}
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...
339