Completed
Push — feature/fixing_cost ( ef981e )
by Laurent
01:53
created

AddFlightDamageController::view()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 6
nop 0
dl 0
loc 43
rs 8.6097
c 0
b 0
f 0
1
<?php
2
3
4
namespace FlightLog\Http\Web\Controller;
5
6
use FlightLog\Application\Damage\Command\CreateDamageCommand;
7
use FlightLog\Application\Damage\Command\CreateDamageCommandHandler;
8
use FlightLog\Http\Web\Form\DamageCreationForm;
9
use FlightLog\Infrastructure\Damage\Repository\FlightDamageRepository;
10
11
final class AddFlightDamageController extends WebController
12
{
13
14
    public function __construct(\DoliDB $db)
15
    {
16
        parent::__construct($db);
17
    }
18
19
    /**
20
     * Display the form.
21
     */
22
    public function view(){
23
        $id = $this->request->getParam('id');
24
25
        if($id === null){
26
            print '<p>Paramètre non fournis.</p>';
27
            return;
28
        }
29
30
        $flight = new \Bbcvols($this->db);
31
        if($flight->fetch($id) <= 0){
32
            print '<p>Vol non trouvé</p>';
33
            return;
34
        }
35
36
        $command = new CreateDamageCommand($flight->getId(), $flight->getPilotId());
37
        $form = new DamageCreationForm('damage_creation', $this->db);
38
        $form->bind($command);
0 ignored issues
show
Documentation introduced by
$command is of type object<FlightLog\Applica...nd\CreateDamageCommand>, but the function expects a object<stdClass>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
39
40
        if($this->request->isPost()){
41
            $form->setData($this->request->getPostParameters());
42
43
            if(!$form->validate()){
44
                return $this->render('flight_damage/form.php', [
45
                    'form' => $form,
46
                ]);
47
            }
48
49
            try{
50
                $this->handle($form->getObject());
0 ignored issues
show
Bug introduced by
It seems like $form->getObject() targeting FlightLog\Http\Web\Form\...eationForm::getObject() can also be of type null or object<stdClass>; however, FlightLog\Http\Web\Contr...ageController::handle() does only seem to accept object<FlightLog\Applica...nd\CreateDamageCommand>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
51
            }catch(\Exception $e){
52
                print $e->getMessage();
53
                dol_syslog($e->getMessage(), LOG_ERR);
54
            }
55
56
57
            $this->redirect($_SERVER["PHP_SELF"].'?id='.$id);
58
            exit;
59
        }
60
61
        return $this->render('flight_damage/form.php', [
62
            'form' => $form,
63
        ]);
64
    }
65
66
    /**
67
     * @param CreateDamageCommand $command
68
     *
69
     * @throws \Exception
70
     */
71
    private function handle(CreateDamageCommand $command)
72
    {
73
        $this->getHandler()->__invoke($command);
74
    }
75
76
    /**
77
     * @return CreateDamageCommandHandler
78
     */
79
    private function getHandler(){
80
        return new CreateDamageCommandHandler($this->db, $this->getDamageRepository());
81
    }
82
83
    /**
84
     * @return FlightDamageRepository
85
     */
86
    private function getDamageRepository(){
87
        return new FlightDamageRepository($this->db);
88
    }
89
90
    /**
91
     * @param string $location
92
     */
93
    protected function redirect($location)
94
    {
95
        if (headers_sent()) {
96
            echo("<script>location.href='$location'</script>");
97
            return;
98
        }
99
100
        header("Location: $location");
101
        exit;
102
    }
103
104
}