Passed
Push — master ( ffd9ca...7c57b8 )
by Anthony
02:23
created

CentreRecherche::getTempsRecherche()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
	namespace modules\bataille\app\controller;
3
	
4
	
5
	use core\App;
6
	use core\functions\DateHeure;
7
	use core\HTML\flashmessage\FlashMessage;
8
9
	class CentreRecherche {
10
		private $coef_centre;
11
		private $recherche;
12
		private $type;
13
		
14
		
15
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
16
		public function __construct() {
17
			$dbc1 = Bataille::getDb();
18
19
			$query = $dbc1->select("coef_centre_recherche")->from("configuration")->where("ID_configuration", "=", 1)->get();
20
21
			if ((is_array($query)) && (count($query) == 1)) {
22
				foreach ($query as $obj) $this->coef_centre = $obj->coef_centre_recherche;
23
			}
24
25
			$query = $dbc1->select()->from("recherche")
26
				->where("niveau_centre", "<=", Bataille::getBatiment()->getNiveauBatiment("centre_recherche"))
27
				->get();
28
29
			if ((is_array($query)) && (count($query) > 0)) {
30
				foreach ($query as $obj) {
31
					$niveau = $this->getNiveauRecherche($obj->recherche, $obj->type);
32
					$niveau_recherche = $niveau;
33
34
					$cout = unserialize($obj->cout);
35
					$temps_recherche = $this->getTempsRecherche($obj->temps_recherche);
36
37
					//si niveau == 0 ca veut dire que la recherche n'a pas encore été effectuée dans la base
38 View Code Duplication
					if ($niveau > 0) {
1 ignored issue
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...
39
						$cout = $this->getCoutRecherche($cout, $niveau);
40
						$temps_recherche = $this->getTempsRecherche($temps_recherche, $niveau);
41
					}
42
					else {
43
						$niveau_recherche = 1;
44
					}
45
46
					$recherhce[] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$recherhce was never initialized. Although not strictly required by PHP, it is generally a good practice to add $recherhce = 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...
47
						"recherche" => $obj->recherche,
48
						"type" => $obj->type,
49
						"niveau" => $niveau,
50
						"cout" => $cout,
51
						"temps_recherche" => DateHeure::Secondeenheure($temps_recherche),
52
						"special" => Bataille::getUnite()->getCaracteristiqueUnite($obj->recherche, $niveau_recherche, $obj->type),
53
						"coef_amelioration" => Bataille::getParam("coef_niveau_unite")
54
					];
55
				}
56
			}
57
58
			Bataille::setValues(["centre_recherche" => $recherhce]);
0 ignored issues
show
Bug introduced by
The variable $recherhce does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
59
		}
60
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
61
		
62
		
63
		
64
		//-------------------------- GETTER ----------------------------------------------------------------------------//
65
		/**
66
		 * @param $recherche
67
		 * @param $type
68
		 * @return int
69
		 * fonction qui va cehrcher le niveau de la recherche actuelle
70
		 * renvoi 0 si elle n'a pas été trouvée
71
		 */
72 View Code Duplication
		private function getNiveauRecherche($recherche, $type) {
1 ignored issue
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...
73
			$dbc = App::getDb();
74
75
			$query = $dbc->select("niveau")
76
				->from("_bataille_centre_recherche")
77
				->where("ID_base", "=", Bataille::getIdBase(), "AND")
78
				->where("recherche", "=", $recherche, "AND")
79
				->where("type", "=", $type)
80
				->get();
81
82
			if ((is_array($query)) && (count($query) > 0)) {
83
				foreach ($query as $obj) {
84
					return $obj->niveau;
85
				}
86
			}
87
88
			return 0;
89
		}
90
91
		/**
92
		 * @param $cout
93
		 * @param $niveau_recherche
94
		 * @return array
95
		 * fonction qui renvoi le cout d'une recherche
96
		 */
97
		private function getCoutRecherche($cout, $niveau_recherche) {
98
			return [
99
				"eau" => $cout["eau"] * ($this->coef_centre * $niveau_recherche),
100
				"electricite" => $cout["electricite"] * ($this->coef_centre * $niveau_recherche),
101
				"fer" => $cout["fer"] * ($this->coef_centre * $niveau_recherche),
102
				"fuel" => $cout["fuel"] * ($this->coef_centre * $niveau_recherche)
103
			];
104
		}
105
106
		/**
107
		 * @param $temps
108
		 * @param int $niveau
109
		 * @return floatfonction qui renvoi le temps qu'il faut pour effectuer une recherche
110
		 */
111
		private function getTempsRecherche($temps, $niveau = 0) {
112
			$pourcent = ($temps*Bataille::getBatiment()->getNiveauBatiment("centre_recherche")/100);
113
114
			if ($niveau == 0) {
115
				return round($temps-$pourcent);;
116
			}
117
118
			return round(($temps * ($this->coef_centre * $niveau))-$pourcent);
119
		}
120
121
		/**
122
		 * @param $type
123
		 * @return array|int
124
		 * permet de renvoyer toutes es recherches déjà effectuées pour notre base en fonction
125
		 * d'un type donné
126
		 */
127
		public function getAllRechercheType($type) {
128
			$dbc = App::getDb();
129
130
			$query = $dbc->select()->from("_bataille_centre_recherche")->where("type", "=", $type)->get();
131
132
			if ((is_array($query)) && (count($query) > 0)) {
133
				foreach ($query as $obj) {
134
					$recherche[] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$recherche was never initialized. Although not strictly required by PHP, it is generally a good practice to add $recherche = 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...
135
						"niveau" => $obj->niveau,
136
						"recherche" => $obj->recherche
137
					];
138
				}
139
140
				return $recherche;
0 ignored issues
show
Bug introduced by
The variable $recherche does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
141
			}
142
143
			return 0;
144
		}
145
146
		/**
147
		 * @return bool
148
		 * fonction qui renvoi un tableau contenant la recherche en cours si celle-ci n'est pas finie
149
		 * sinon elle appelle la fonction setTerminerRecherche
150
		 */
151
		public function getRecherche() {
152
			$dbc = App::getDb();
153
154
			$query = $dbc->select()->from("_bataille_recherche")->where("ID_base", "=", Bataille::getIdBase())->get();
155
156
			if ((is_array($query)) && (count($query) > 0)) {
157
				$today = Bataille::getToday();
158
159
				foreach ($query as $obj) {
160
					$this->recherche = $obj->recherche;
161
					$this->type = $obj->type;
162
163
					if ($obj->date_fin-$today <= 0) {
164
						$this->setTerminerRecherche($obj->ID_recherche);
165
					}
166
					else {
167
						$recherche = [
168
							"recherche" => $obj->recherche,
169
							"type" => $obj->type,
170
							"date_fin_recherche" => $obj->date_fin-$today,
171
							"id_recherche" => $obj->ID_recherche
172
						];
173
					}
174
				}
175
176
				Bataille::setValues(["recherche" => $recherche]);
0 ignored issues
show
Bug introduced by
The variable $recherche does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
177
178
				return true;
179
			}
180
181
			return false;
182
		}
183
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
184
		
185
		
186
		
187
		//-------------------------- SETTER ----------------------------------------------------------------------------//
188
		public function setCommencerRecherche($recherche, $type) {
189
			$dbc = App::getDb();
190
			$dbc1 = Bataille::getDb();
191
192
			//on test si il n'y a pas déjà une recherche en cours
193
			if ($this->getRecherche() == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
194
				FlashMessage::setFlash("Une recherche est déjà en cours, merci d'attendre la fin de celle-ci");
195
				return false;
196
			}
197
198
			//on récupère la recherche dans notre base savoir si on l'a déjà recherchée pour avoir son lvl
199
			$niveau_recherche = $this->getNiveauRecherche($recherche, $type);
200
201
			//récupération du cout initial plus temps de recherche initial pour calculer les bon en fonction
202
			//du lvl du centre + du niveau actuel de la recherche
203
			$query = $dbc1->select("cout")
204
				->select("temps_recherche")
205
				->from("recherche")
206
				->where("recherche", "=", $recherche, "AND")
207
				->where("type", "=", $type)
208
				->get();
209
210
			if ((is_array($query)) && (count($query) == 1)) {
211
				foreach ($query as $obj) {
212
					$cout = unserialize($obj->cout);
213
					$temps_recherche = $obj->temps_recherche;
214
				}
215
			}
216
217 View Code Duplication
			if ($niveau_recherche > 0) {
1 ignored issue
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...
218
				$cout = $this->getCoutRecherche($cout, $niveau_recherche);
0 ignored issues
show
Bug introduced by
The variable $cout does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
219
				$temps_recherche = $this->getTempsRecherche($temps_recherche, $niveau_recherche);
0 ignored issues
show
Bug introduced by
The variable $temps_recherche does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
220
			}
221
222
			//on test si assez de ressources pour effectuer la recherche
223
			$eau = Bataille::getTestAssezRessourceBase("eau", $cout["eau"]);
224
			$electricite = Bataille::getTestAssezRessourceBase("electricite", $cout["electricite"]);
225
			$fer = Bataille::getTestAssezRessourceBase("fer", $cout["fer"]);
226
			$fuel = Bataille::getTestAssezRessourceBase("fuel", $cout["fuel"]);
227
228
229
			if (($eau["class"] || $electricite["class"] || $fer["class"] || $fuel["class"]) == "rouge" ) {
230
				FlashMessage::setFlash("Pas assez de ressources pour effectuer cette recherche");
231
				return false;
232
			}
233
			else {
234
				//on retire les ressources
235
				Bataille::getRessource()->setUpdateRessource($cout["eau"], $cout["electricite"], $cout["fer"], $cout["fuel"], 0, "-");
236
237
				$date_fin = Bataille::getToday()+$temps_recherche;
238
239
				$dbc->insert("recherche", $recherche)
240
					->insert("type", $type)
241
					->insert("date_fin", $date_fin)
242
					->insert("ID_base", Bataille::getIdBase())
243
					->into("_bataille_recherche")
244
					->set();
245
246
				return true;
247
			}
248
		}
249
250
		private function setTerminerRecherche($id_recherche) {
251
			$dbc = App::getDb();
252
			$niveau_recherche = $this->getNiveauRecherche($this->recherche, $this->type);
253
254
			if ($niveau_recherche == 0) {
255
				$dbc->insert("recherche", $this->recherche)
256
					->insert("type", $this->type)
257
					->insert("niveau", 1)
258
					->insert("ID_base", Bataille::getIdBase())
259
					->into("_bataille_centre_recherche")
260
					->set();
261
			}
262
			else {
263
				$dbc->update("niveau", $niveau_recherche+1)
264
					->from("_bataille_centre_recherche")
265
					->where("recherche", "=", $this->recherche, "AND")
266
					->where("type", "=", $this->type, "AND")
267
					->where("ID_base", "=", Bataille::getIdBase())
268
					->set();
269
			}
270
271
			$dbc->delete()->from("_bataille_recherche")->where("ID_recherche", "=", $id_recherche, "AND")
272
				->where("ID_base", "=", Bataille::getIdBase())
273
				->del();
274
		}
275
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
276
		
277
	}