Passed
Push — master ( 182e59...7d0ce6 )
by Anthony
02:36
created

MissionsAleatoire::getMissions()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 28
rs 6.7272
cc 7
eloc 18
nc 5
nop 0
1
<?php
2
	namespace modules\bataille\app\controller;
3
	
4
	
5
	use core\App;
6
	use core\functions\DateHeure;
7
	
8
	class MissionsAleatoire {
9
		
10
		
11
		
12
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
13
		/**
14
		 * MissionsAleatoire constructor.
15
		 * le constructeur s'occupe de vérifier le last_check des missions et au cas ou si il est plus vieux d'un jour
16
		 * appeler la fonction pour recharger les missions
17
		 */
18
		public function __construct() {
19
			$dbc = App::getDb();
20
			
21
			$query = $dbc->select("last_check_mission")->from("_bataille_base")->where("ID_base", "=", Bataille::getIdBase())->get();
22
			
23
			if (is_array($query) && (count($query) == 1)) {
24
				foreach ($query as $obj) {
25
					$last_check_mission = $obj->last_check_mission;
26
				}
27
				
28
				if ($last_check_mission == "") {
29
					$this->setUpdateLastCheckMissions();
30
					$this->setMissionsAleatoire();
31
				}
32
				else {
33
					$today = new \DateTime();
34
					$last_check_mission = new \DateTime($last_check_mission);
0 ignored issues
show
Bug introduced by
The variable $last_check_mission 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...
35
					$interval = $last_check_mission->diff($today);
36
					
37
					$diff_jour = explode("+", $interval->format("%R%a"))[1];
38
					
39
					if ($diff_jour >= 1) {
40
						$this->setUpdateLastCheckMissions();
41
						$this->setMissionsAleatoire();
42
					}
43
				}
44
			}
45
		}
46
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
47
		
48
		
49
		
50
		//-------------------------- GETTER ----------------------------------------------------------------------------//
51
		/**
52
		 * fonction qui récupere tous les types de missions et les return dans un array
53
		 */
54
		private function getTypeMission() {
55
			return explode(",", Bataille::getParam("type_missions"));
56
		}
57
		
58
		/**
59
		 * @return int
60
		 * renvoi le nombre de missions encore disponibles dans la base
61
		 */
62
		public function getNbMissions() {
63
			$dbc = App::getDb();
64
			
65
			$query = $dbc->select("ID_mission_aleatoire")->from("_bataille_mission_aleatoire")->where("ID_base", "=", Bataille::getIdBase())->get();
66
			
67
			if ((is_array($query)) && (count($query))) {
68
				foreach ($query as $obj) {
69
					$id[] = $obj->ID_base;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$id was never initialized. Although not strictly required by PHP, it is generally a good practice to add $id = 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...
70
				}
71
				
72
				$count = count($id);
0 ignored issues
show
Bug introduced by
The variable $id 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...
73
				Bataille::setValues([
74
					"nb_mission" => $count
75
				]);
76
				
77
				return $count;
78
			}
79
		}
80
		
81
		/**
82
		 * récupères les missions encore disponible dans la base
83
		 */
84
		public function getMissions() {
85
			$dbc = App::getDb();
86
			$dbc1 = Bataille::getDb();
87
			
88
			$query = $dbc->select()->from("_bataille_mission_aleatoire")->where("ID_base", "=", Bataille::getIdBase())->get();
89
			
90
			if ((is_array($query)) && (count($query))) {
91
				foreach ($query as $obj) {
92
					$query1 = $dbc1->select()->from("mission")->where("ID_mission", "=", $obj->ID_mission)->get();
93
					
94
					if ((is_array($query1)) && (count($query1))) {
95
						foreach ($query1 as $obj) {
96
							$missions[] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$missions was never initialized. Although not strictly required by PHP, it is generally a good practice to add $missions = 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...
97
								"nom_mission" => $obj->nom_mission,
98
								"description" => $obj->description,
99
								"points_gagne" => $obj->points_gagne,
100
								"type" => $obj->type,
101
								"ressource_gagnee" => $obj->ressource_gagnee,
102
								"pourcentage_perte" => $obj->pourcentage_perte,
103
								"duree" => DateHeure::Secondeenheure($obj->duree)
104
							];
105
						}
106
					}
107
				}
108
				
109
				Bataille::setValues(["missions" => $missions]);
0 ignored issues
show
Bug introduced by
The variable $missions 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...
110
			}
111
		}
112
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
113
		
114
		
115
		
116
		//-------------------------- SETTER ----------------------------------------------------------------------------//
117
		/**
118
		 * fonction qui met a jour le last_ckeck_missions dans _bataille_base
119
		 * le met à la date du jour
120
		 */
121
		public function setUpdateLastCheckMissions() {
122
			$dbc = App::getDb();
123
			
124
			$dbc->update("last_check_mission", date("Y-m-d"))
125
				->from("_bataille_base")
126
				->where("ID_base", "=", Bataille::getIdBase())
127
				->set();
128
		}
129
		
130
		/**
131
		 * @param $type
132
		 * fonction qui recupere des missions aleatoirement de chaque type et qui les ajoute
133
		 * dans la table _bataille_mission_aleatoire
134
		 */
135
		private function setMissionsAleatoire() {
136
			$dbc = App::getDb();
137
			$dbc1 = Bataille::getDb();
138
			
139
			$dbc->delete()->from("_bataille_mission_aleatoire")->where("ID_base", "=", Bataille::getIdBase())->del();
140
			
141
			$type_missions = $this->getTypeMission();
142
			
143
			foreach ($type_missions as $un_type) {
144
				$query = $dbc1->select()->from("mission")
145
					->where("type", "=", $un_type)
146
					->orderBy("RAND()")
147
					->limit(0, 3)
148
					->get();
149
				
150
				if ((is_array($query)) && (count($query))) {
151
					foreach ($query as $obj) {
152
						$dbc->insert("ID_mission", $obj->ID_mission)
153
							->insert("ID_base", Bataille::getIdBase())
154
							->into("_bataille_mission_aleatoire")
155
							->set();
156
					}
157
				}
158
			}
159
		}
160
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
161
		
162
	}