Passed
Push — master ( 8ad93e...43ef21 )
by Anthony
06:05
created

MissionsAleatoire::setMissionsAleatoire()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.2
cc 4
eloc 10
nc 3
nop 1
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();
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% 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...
14
			$dbc1 = Bataille::getDb();*/
15
			
16
			//test si on a deje des missions dans la base
17
			$this->getTestCheckMission();
18
		}
19
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
20
		
21
		
22
		
23
		//-------------------------- GETTER ----------------------------------------------------------------------------//
24
		/**
25
		 * fonction qui regarde la derniere fois que la récupération de missions a été effectuée.
26
		 * Si jamais fait on ajoute la date du jour en bdd et on lance la récupération de missions
27
		 * sinon si elle est supérieur a 1 jour on la remet a la date du jour + on get de nouvelles missions
28
		 */
29
		private function getTestCheckMission() {
30
			$dbc = App::getDb();
31
			
32
			$query = $dbc->select("last_check_mission")->from("_bataille_base")->where("ID_base", "=", Bataille::getIdBase())->get();
33
			
34
			if (is_array($query) && (count($query) == 1)) {
35
				foreach ($query as $obj) {
36
					$last_check_mission = $obj->last_check_mission;
37
				}
38
				
39
				if ($last_check_mission == "") {
40
					$this->setUpdateLastCheckMissions();
41
					$this->setMissionsAleatoire();
0 ignored issues
show
Bug introduced by
The call to setMissionsAleatoire() misses a required argument $type.

This check looks for function calls that miss required arguments.

Loading history...
42
				}
43
				else {
44
					$today = new \DateTime();
45
					$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...
46
					$interval = $last_check_mission->diff($today);
47
					
48
					$diff_jour = explode("+", $interval->format("%R%a"))[1];
49
					
50
					if ($diff_jour >= 1) {
51
						$this->setUpdateLastCheckMissions();
52
						$this->setMissionsAleatoire();
0 ignored issues
show
Bug introduced by
The call to setMissionsAleatoire() misses a required argument $type.

This check looks for function calls that miss required arguments.

Loading history...
53
					}
54
				}
55
			}
56
		}
57
		
58
		/**
59
		 * fonction qui récupere tous les types de missions et les return dans un array
60
		 */
61
		private function getTypeMission() {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
62
			
63
		}
64
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
65
		
66
		
67
		
68
		//-------------------------- SETTER ----------------------------------------------------------------------------//
69
		/**
70
		 * fonction qui met a jour le last_ckeck_missions dans _bataille_base
71
		 * le met à la date du jour
72
		 */
73
		public function setUpdateLastCheckMissions() {
74
			$dbc = App::getDb();
75
			
76
			$dbc->update("last_check_mission", date("Y-m-d"))
77
				->from("_bataille_base")
78
				->where("ID_base", "=", Bataille::getIdBase())
79
				->set();
80
		}
81
		
82
		/**
83
		 * @param $type
84
		 * fonction qui recupere des missions aleatoirement de chaque type et qui les ajoute
85
		 * dans la table _bataille_mission_aleatoire
86
		 */
87
		private function setMissionsAleatoire($type) {
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...
88
			$dbc1 = Bataille::getDb();
89
			
90
			$query = $dbc1->select()->from("mission")
91
				->where("type", "=", $type)
92
				->orderBy("RAND()")
93
				->limit(0, 3)
94
				->get();
95
			
96
			if ((is_array($query)) && (count($query))) {
97
				foreach ($query as $obj) {
98
					echo($obj->nom_mission."<br>");
99
				}
100
			}
101
		}
102
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
103
		
104
	}