Completed
Push — feature/api ( 725c17...de88d6 )
by Laurent
01:40
created

FlightForm::buildOptionsfromConfiguration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 */
5
6
namespace flightlog\form;
7
8
use User;
9
10
/**
11
 * FlightForm class
12
 *
13
 * @author Laurent De Coninck <[email protected]>
14
 */
15
class FlightForm extends Form
16
{
17
18
    /**
19
     * @var User
20
     */
21
    private $user;
22
23
    /**
24
     * @param \ValidatorInterface $validator
25
     * @param \Bbcvols $baseObject
26
     * @param \DoliDB $db
27
     * @param array $options
28
     * @param \User $user
29
     */
30
    public function __construct(\ValidatorInterface $validator, $baseObject, \DoliDB $db, $options,\User $user)
31
    {
32
33
        parent::__construct('flight_form', FormInterface::METHOD_POST, $this->buildOptionsfromConfiguration($options));
0 ignored issues
show
Documentation introduced by
$options is of type array, 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...
34
35
        $this->user = $user;
36
37
        $this->setValidator($validator)
38
            ->bind($baseObject);
39
40
        $this
41
            ->add(new Hidden('idBBC_vols'))
42
            ->add((new InputDate('date'))->required())
43
            ->add((new Input('lieuD'))->required())
44
            ->add((new Input('lieuA'))->required())
45
            ->add((new InputTime('heureD'))->required())
46
            ->add((new InputTime('heureA'))->required())
47
            ->add((new BalloonSelect('BBC_ballons_idBBC_ballons', $db))->required())
48
            ->add(new Number('nbrPax'))
49
            ->add(new InputTextarea('remarque'))
50
            ->add(new InputTextarea('incidents'))
51
            ->add(new InputTextarea('passengerNames'))
52
            ->add((new FlightTypeSelect('fk_type', [], $db)))
53
            ->add(new UserSelect('fk_pilot', $this->getOptions(), $db))
54
            ->add(new UserSelect('fk_organisateur', $this->getOptions(), $db))
55
            ->add(new UserSelect('fk_receiver', $this->getOptions(), $db))
56
            ->add(new Number('kilometers'))
57
            ->add(new Number('cost'))
58
            ->add(new InputTextarea('justif_kilometers'));
59
    }
60
61
    /**
62
     * @param \stdClass $options
63
     *
64
     * @return array
65
     */
66
    private function buildOptionsfromConfiguration($options)
67
    {
68
        $values = [];
69
70
        foreach ($options as $value) {
0 ignored issues
show
Bug introduced by
The expression $options of type object<stdClass> is not traversable.
Loading history...
71
            $values[] = $value;
72
        }
73
74
        return $values;
75
    }
76
77
    /**
78
     * @inheritDoc
79
     */
80
    public function bind($object)
81
    {
82
        /** @var \Bbcvols $flight */
83
        $flight = $object;
84
85
        // Quick fix - Fixme by a factory on this form.
86
        if($this->user->rights->flightlog->vol->advanced){
87
            return parent::bind($object);
88
        }
89
90
        if ($flight->isBilled()) {
91
            $this
92
                ->remove('fk_receiver')
93
                ->remove('cost')
94
                ->remove('nbrPax')
95
                ->remove('passengerNames');
96
        }
97
98
        $endOfYearDate = $flight->getDate()->setDate($flight->getDate()->format('Y'), 12, 31);
99
        if (new \DateTime() >= $endOfYearDate) {
100
            $this
101
                ->remove('fk_receiver')
102
                ->remove('cost')
103
                ->remove('BBC_ballons_idBBC_ballons')
104
                ->remove('nbrPax')
105
                ->remove('passengerNames')
106
                ->remove('fk_type')
107
                ->remove('fk_pilot')
108
                ->remove('fk_organisateur')
109
                ->remove('kilometers')
110
                ->remove('date')
111
                ->remove('heureD')
112
                ->remove('heureA')
113
                ->remove('kilometers')
114
                ->remove('justif_kilometers');
115
        }
116
117
        return parent::bind($object);
118
    }
119
120
121
}