Completed
Push — master ( c02c23...f37d2c )
by Reginaldo
28:44
created

ProdutoEstoqueController::getUserActive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
class ProdutoEstoqueController extends AppController {
4
5
	public function validar_estoque($produto, $quantidade) {
6
		if (empty($produto)) {
7
			return false;
8
		}
9
10
		$user_active = $this->getUserActive($produto[0]['Produto']['id_usuario']);
11
12
		if ($user_active[0]['Usuario']['sale_without_stock'])
13
			return true;
14
15 View Code Duplication
		if ($produto[0]['Produto']['estoque'] <= 0) {
16
			$this->Session->setFlash('O Produto ' . $produto[0]['Produto']['name'] . ' selecionado não possui estoque disponivel');
17
			return false;
18
		}
19
20
21 View Code Duplication
		if ($produto[0]['Produto']['estoque'] < $quantidade) {
22
			$this->Session->setFlash('A quantidade para o produto ' . $produto[0]['Produto']['name'] . ' escolhida é maior do que a disponivel');
23
			return false;
24
		}
25
26
		return true;
27
	}
28
29 View Code Duplication
	public function getUserActive($id) {
30
		$this->loadModel('Usuario');
31
32
		$user = $this->Usuario->find('all', 
33
			array('conditions' => 
34
				array('Usuario.id' => $id)
35
			)
36
		);
37
		
38
		return $user;
39
	}
40
41
	public function diminuir_estoque_produto($produto_id, $quantidade) {
42
		if (!isset($produto_id) || !isset($quantidade) || !is_numeric($quantidade)) {
43
			return false;
44
		}
45
46
		try {
47
			$this->loadModel('Produto');
48
49
			$produto = $this->Produto->find('first', array(
50
					'conditions' => array('Produto.id' => $produto_id),
51
					'order' => array('Produto.id' => 'desc')
52
				)
53
			);
54
55
			$novo_estoque['estoque'] = $produto['Produto']['estoque'] - $quantidade;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$novo_estoque was never initialized. Although not strictly required by PHP, it is generally a good practice to add $novo_estoque = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
56
57
			$this->Produto->id = $produto_id;
58
			if (!$this->Produto->save($novo_estoque)) {
59
				return false;
60
			}
61
62
			return true;
63
		} catch (Exception $e) {
64
			print_r($e);
65
			exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method diminuir_estoque_produto() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
66
		}
67
 	}
68
69
 	public function diminuir_estoque_produto_variacao($produto_id, $quantidade, $variacao) {
70
 		if (!isset($produto_id) || !isset($quantidade) || !isset($variacao)) {
71
 			return false;
72
 		}
73
74
 		try {
75
 			$this->loadModel('Variacao');
76
77
 			$variacao = $this->Variacao->find('first', array(
78
 					'conditions' => array(
79
 						'Variacao.produto_id' => $produto_id,
80
 						'Variacao.ativo' => 1,
81
 						'Variacao.nome_variacao' => $variacao
82
 					)
83
 				)
84
 			);
85
86
			$novo_estoque['estoque'] = $variacao['Variacao']['estoque'] - $quantidade;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$novo_estoque was never initialized. Although not strictly required by PHP, it is generally a good practice to add $novo_estoque = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
87
88
			$this->Variacao->id = $variacao['Variacao']['id'];
89
			if (!$this->Variacao->save($novo_estoque)) {
90
				return false;
91
			}
92
93
			return true;
94
 		} catch (Exception $e) {
95
 			print_r($e);
96
 			exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method diminuir_estoque_produto_variacao() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
97
 		}
98
 	}
99
100
}