Passed
Push — master ( 43ef21...3d5094 )
by Anthony
02:31
created

MissionsAleatoire::__construct()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 28
rs 8.439
cc 6
eloc 17
nc 7
nop 0
1
<?php
2
	namespace modules\bataille\app\controller;
3
	
4
	
5
	use core\App;
6
	
7
	class MissionsAleatoire {
8
		
9
		
10
		
11
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
12
		public function __construct() {
13
			$dbc = App::getDb();
14
			
15
			$query = $dbc->select("last_check_mission")->from("_bataille_base")->where("ID_base", "=", Bataille::getIdBase())->get();
16
			
17
			if (is_array($query) && (count($query) == 1)) {
18
				foreach ($query as $obj) {
19
					$last_check_mission = $obj->last_check_mission;
20
				}
21
				
22
				if ($last_check_mission == "") {
23
					$this->setUpdateLastCheckMissions();
24
					$this->setMissionsAleatoire();
25
				}
26
				else {
27
					$today = new \DateTime();
28
					$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...
29
					$interval = $last_check_mission->diff($today);
30
					
31
					$diff_jour = explode("+", $interval->format("%R%a"))[1];
32
					
33
					if ($diff_jour >= 1) {
34
						$this->setUpdateLastCheckMissions();
35
						$this->setMissionsAleatoire();
36
					}
37
				}
38
			}
39
		}
40
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
41
		
42
		
43
		
44
		//-------------------------- GETTER ----------------------------------------------------------------------------//
45
		/**
46
		 * fonction qui récupere tous les types de missions et les return dans un array
47
		 */
48
		private function getTypeMission() {
49
			return explode(",", Bataille::getParam("type_missions"));
50
		}
51
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
52
		
53
		
54
		
55
		//-------------------------- SETTER ----------------------------------------------------------------------------//
56
		/**
57
		 * fonction qui met a jour le last_ckeck_missions dans _bataille_base
58
		 * le met à la date du jour
59
		 */
60
		public function setUpdateLastCheckMissions() {
61
			$dbc = App::getDb();
62
			
63
			$dbc->update("last_check_mission", date("Y-m-d"))
64
				->from("_bataille_base")
65
				->where("ID_base", "=", Bataille::getIdBase())
66
				->set();
67
		}
68
		
69
		/**
70
		 * @param $type
71
		 * fonction qui recupere des missions aleatoirement de chaque type et qui les ajoute
72
		 * dans la table _bataille_mission_aleatoire
73
		 */
74
		private function setMissionsAleatoire() {
75
			$dbc = App::getDb();
76
			$dbc1 = Bataille::getDb();
77
			
78
			$dbc->delete()->from("_bataille_mission_aleatoire")->where("ID_base", "=", Bataille::getIdBase())->del();
79
			
80
			$type_missions = $this->getTypeMission();
81
			
82
			foreach ($type_missions as $un_type) {
83
				$query = $dbc1->select()->from("mission")
84
					->where("type", "=", $un_type)
85
					->orderBy("RAND()")
86
					->limit(0, 3)
87
					->get();
88
				
89
				if ((is_array($query)) && (count($query))) {
90
					foreach ($query as $obj) {
91
						$dbc->insert("ID_mission", $obj->ID_mission)
92
							->insert("ID_base", Bataille::getIdBase())
93
							->into("_bataille_mission_aleatoire")
94
							->set();
95
					}
96
				}
97
			}
98
		}
99
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
100
		
101
	}