TransmissorFactory::save()   B
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 3
nop 0
dl 0
loc 49
rs 8.8016
c 0
b 0
f 0
1
<?php
2
3
namespace Transmissor\Factory;
4
5
use Transmissor\Models\Debt;
6
use Transmissor\Models\Group;
7
use Transmissor\Models\User;
8
9
class TransmissorFactory
10
{
11
12
    private $payer;
13
    private $amount;
14
    private $receiver;
15
    private $group;
16
17
    public function __construct(User $payer, $amount)
18
    {
19
        $this->payer = $payer;
20
        $this->amount = $amount;
21
    }
22
23
    public function to(User $receiver)
24
    {
25
        $this->receiver = $receiver;
26
27
        return $this;
28
    }
29
30
    public function in(Group $group)
31
    {
32
        $this->group = $group;
33
34
        return $this;
35
    }
36
37
    public function save()
38
    {
39
40
        $this->payer->debts_to_pay()
41
            ->where('to_id', $this->receiver->id)
42
            ->where('group_id', $this->group->id)
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Transmissor\Models\Group>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
43
            ->get()
44
            ->each(
45
                function (Debt $debt) {
46
47
                    $debtAmount = $debt->amount;
48
49
                    if ($this->amount < $debtAmount) {
50
                        $debt->amount -= $this->amount;
51
                        $debt->save();
52
53
                    } else {
54
                        $debt->delete();
55
                    }
56
57
                    $this->amount -= $debtAmount;
58
59
                    if ($this->amount <= 0) {
60
                        return false;
61
                    }
62
                }
63
            );
64
65
        if ($this->amount <= 0) {
66
            return;
67
        }
68
69
        $previousDebt = $this->payer->debts_to_receive()
70
            ->where('from_id', $this->receiver->id)
71
            ->where('group_id', $this->group->id)
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Transmissor\Models\Group>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
72
            ->first();
73
74
        if (! $previousDebt) {
75
            $previousDebt = new Debt();
76
            $previousDebt->from_id = $this->receiver->id;
77
            $previousDebt->to_id = $this->payer->id;
78
            $previousDebt->amount = 0;
79
            $previousDebt->group_id = $this->group->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Transmissor\Models\Group>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
80
            $previousDebt->currency = $this->group->currency;
0 ignored issues
show
Documentation introduced by
The property currency does not exist on object<Transmissor\Models\Group>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
81
        }
82
83
        $previousDebt->amount += $this->amount;
84
        $previousDebt->save();
85
    }
86
}