Passed
Push — master ( 1c7663...341095 )
by Anthony
02:41
created

Bataille::getToday()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
	namespace modules\bataille\app\controller;
3
	use core\App;
4
	use core\database\Database;
5
6
	class Bataille {
7
		private static $ressource;
8
		private static $base;
9
		private static $batiment;
10
		private static $database;
11
12
		private static $id_base;
13
14
		public static $values = [];
15
16
		
17
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
18
		public function __construct() {
19
20
		}
21
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
22
		
23
		
24
		
25
		//-------------------------- GETTER ----------------------------------------------------------------------------//
26
		/**
27
		 * @return array
28
		 * get array of all values wich will be used in the page
29
		 */
30
		public static function getValues() {
31
		    return ["bataille" => self::$values];
32
		}
33
34
		//initilisation of all classes of battle
35
		//initialisation of Ressource class
36
		public static function getRessource() {
37
			if (self::$ressource == null) {
38
				self::$ressource = new Ressource();
39
			}
40
41
			return self::$ressource;
42
		}
43
44
		//initialisation of Base class
45
		public static function getBase() {
46
			if (self::$base == null) {
47
				self::$base = new Base();
48
			}
49
50
			return self::$base;
51
		}
52
53
		//initialisation of Batiment class
54
		public static function getBatiment() {
55
			if (self::$batiment == null) {
56
				self::$batiment = new Batiment();
57
			}
58
59
			return self::$batiment;
60
		}
61
62
		//initialisation of Database Core connexion
63
		public static function getDb() {
64
			if (self::$database == null) {
65
				self::$database = new Database("mysql", "bataille_core", "root", "Gerto80", "127.0.0.1");
66
			}
67
			return self::$database;
68
		}
69
70
		public static function getIdIdentite() {
0 ignored issues
show
Coding Style introduced by
getIdIdentite uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
71
			return $_SESSION['idlogin'.CLEF_SITE];
72
		}
73
74
		public static function getIdBase() {
0 ignored issues
show
Coding Style introduced by
getIdBase uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
75
			if (self::$id_base == null) {
76
				self::$id_base = $_SESSION['id_base'];
77
78
				return self::$id_base;
79
			}
80
81
			return self::$id_base;
82
		}
83
84
		/**
85
		 * @param $id_base
86
		 * @return array
87
		 * fonction qui renvoi les posisitons en x et y d'une base
88
		 */
89
		private static function getPosistionBase($id_base) {
90
			$dbc = App::getDb();
91
92
			$query = $dbc->select("posx")
93
				->select("posy")
94
				->from("_bataille_base")
95
				->where("ID_base", "=", $id_base)
96
				->get();
97
98
			foreach ($query as $obj) {
99
				$posx = $obj->posx;
100
				$posy = $obj->posy;
101
			}
102
103
			return ["posx" => $posx, "posy" => $posy];
0 ignored issues
show
Bug introduced by
The variable $posx 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...
Bug introduced by
The variable $posy 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...
104
		}
105
106
		/**
107
		 * @return int
108
		 * return now timestamp
109
		 */
110
		public static function getToday() {
111
			$today = new \DateTime();
112
			return $today->getTimestamp();
113
		}
114
115
		/**
116
		 * @param null $id_base -> sert si definit a recuperer l'id identite de la abse en question
117
		 * @return mixed
118
		 * recupere la date de la derniere connexion
119
		 */
120
		public static function getLastConnexion($id_base = null) {
121
			$dbc = App::getDb();
122
123
			if ($id_base === null) {
124
				$query = $dbc->select()->from("_bataille_last_connexion")->where("ID_identite", "=", self::getIdIdentite())->get();
125
			}
126
			else {
127
				$query = $dbc->select("_bataille_last_connexion.last_connexion")->from("_bataille_base")
128
					->from("_bataille_last_connexion")
129
					->from("identite")
130
					->where("_bataille_base.ID_base", "=", $id_base, "AND")
131
					->where("_bataille_base.ID_identite", "=", "identite.ID_identite", "AND", true)
132
					->where("identite.ID_identite", "=", "_bataille_last_connexion.ID_identite", "", true)
133
					->get();
134
			}
135
136
137
			if ((is_array($query)) && (count($query) > 0)) {
138
				foreach ($query as $obj) {
139
					return $obj->last_connexion;
140
				}
141
			}
142
143
		}
144
145
		/**
146
		 * @return mixed
147
		 * recupere le nombre maximum d'emplacement dans la base
148
		 */
149 View Code Duplication
		public static function getNombreEmplacementBase() {
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...
150
			$dbc1 = self::getDb();
151
152
			$query = $dbc1->select("nombre_emplacement")->from("configuration")->where("ID_configuration", "=", 1)->get();
153
154
			if ((is_array($query)) && (count($query) > 0)) {
155
				foreach ($query as $obj) {
156
					return $obj->nombre_emplacement;
157
				}
158
			}
159
		}
160
161
		/**
162
		 * @param $nom_ressource
163
		 * @param $ressource
164
		 * @return array
165
		 * fonction qui permet de renvyer la couleur rouge si pas assez de ressource pour construire le batiment
166
		 * ou pour creer une unité...
167
		 */
168
		public static function getTestAssezRessourceBase($nom_ressource, $ressource) {
169
			$f = "get".ucfirst($nom_ressource);
170
171
			if ($ressource >  Bataille::getRessource()->$f()) {
172
				/*echo("$nom_ressource $ressource ".Bataille::getRessource()->getEau()." ---");*/
0 ignored issues
show
Unused Code Comprehensibility introduced by
71% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
173
				return [
174
					"ressource" => $ressource,
175
					"class" => "rouge"
176
				];
177
			}
178
179
			return [
180
				"ressource" => $ressource,
181
				"class" => ""
182
			];
183
		}
184
		
185
		/**
186
		 * @param $id_base
187
		 * @return number
188
		 * fonction qui renvoi le temps de trajet entre la base du joueur et une autre base en secondes
189
		 */
190
		public static function getDureeTrajet($id_base) {
0 ignored issues
show
Coding Style introduced by
getDureeTrajet uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
191
			//récupération de la posisiotn de la base du joueur + la base sur laquelle on a cliqué
192
			$base_joueur = self::getPosistionBase($_SESSION['id_base']);
193
			$base_autre = self::getPosistionBase($id_base);
194
			
195
			//calcul des distances séparant les deux bases en x et y
196
			//cette dstance sera multipliée par 15 sur x et y puis ajoutée pour avoir le temps du trajte en seconde
197
			$calc_x = abs($base_joueur['posx']-$base_autre['posx']);
198
			$calc_y = abs($base_joueur['posy']-$base_autre['posy']);
199
			
200
			$temps_voyage = ($calc_x*15)+($calc_y*15);
201
			
202
			return $temps_voyage;
203
		}
204
205
		/**
206
		 * @param null $id_identite
207
		 * get nation of a player
208
		 */
209
		public static function getNationBase($id_identite = null) {
0 ignored issues
show
Coding Style introduced by
getNationBase uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
210
			$dbc = App::getDb();
211
212
			if ($id_identite === null) {
213
				$id_identite = $_SESSION['idlogin'.CLEF_SITE];
214
			}
215
216
			$query = $dbc->select("nation")
217
				->from("identite")
218
				->from("_bataille_nation")
219
				->where("identite.ID_identite", "=", $id_identite, "AND")
220
				->where("identite.ID_identite", "=", "_bataille_nation.ID_identite", "", true)
221
				->get();
222
223
			if ((is_array($query)) && (count($query) > 0)) {
224
				foreach ($query as $obj) {
225
					self::setValues(["nation" => $obj->nation]);
226
				}
227
			}
228
		}
229
230
		/**
231
		 * @param $posx
232
		 * @param $posy
233
		 * @return int
234
		 * fonction qui renvoi un ID_base en fonction de sa posx et posy et 0 si base inexistante
235
		 */
236 View Code Duplication
		public static function getBaseExistPosition($posx, $posy) {
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...
237
			$dbc = App::getDb();
238
239
			$query = $dbc->select("ID_base")->from("_bataille_base")
240
				->where("posx", "=", $posx, "AND")
241
				->where("posy", "=", $posy)
242
				->get();
243
244
			if ((is_array($query)) && (count($query) == 1)) {
245
				foreach ($query as $obj) return $obj->ID_base;
246
			}
247
248
			return 0;
249
		}
250
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
251
		
252
		
253
		
254
		//-------------------------- SETTER ----------------------------------------------------------------------------//
255
		/**
256
		 * set la date de derniere connexion a now
257
		 */
258
		public static function setLastConnexion($id_base = null) {
259
			$dbc = App::getDb();
260
261
			if ($id_base === null) {
262
				$id_identite = self::getIdIdentite();
263
			}
264
			else {
265
				$query = $dbc->select("ID_identite")->from("_bataille_base")->where("ID_base", "=", $id_base)->get();
266
267
				foreach ($query as $obj) $id_identite = $obj->ID_identite;
268
			}
269
270
			$dbc->update("last_connexion", date("Y-m-d H:i:s"))
271
				->from("_bataille_last_connexion")
272
				->where("ID_identite", "=", $id_identite)
0 ignored issues
show
Bug introduced by
The variable $id_identite 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...
273
				->set();
274
		}
275
276
		/**
277
		 * @param $values
278
		 * can set values while keep older infos
279
		 */
280
		public static function setValues($values) {
281
			Bataille::$values = array_merge(Bataille::$values, $values);
282
		}
283
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
284
	}