Passed
Push — master ( 3f23ea...c594c9 )
by Jan
05:15 queued 11s
created

InstockChangedLogEntry::getDifference()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 13
rs 10
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
20
 */
21
22
namespace App\Entity\LogSystem;
23
24
25
use App\Entity\Parts\Part;
26
use Doctrine\ORM\Mapping as ORM;
27
28
/**
29
 * @ORM\Entity()
30
 * @package App\Entity\LogSystem
31
 */
32
class InstockChangedLogEntry extends AbstractLogEntry
33
{
34
    protected $typeString = "instock_changed";
35
36
    /**
37
     * Get the old instock
38
     * @return int
39
     */
40
    public function getOldInstock(): int
41
    {
42
        return $this->extra['o'];
43
    }
44
45
    /**
46
     * Get the new instock
47
     * @return int
48
     */
49
    public function getNewInstock(): int
50
    {
51
        return $this->extra['n'];
52
    }
53
54
    /**
55
     * Gets the comment associated with the instock change
56
     * @return string
57
     */
58
    public function getComment(): string
59
    {
60
        return $this->extra['c'];
61
    }
62
63
    /**
64
     * Returns the price that has to be payed for the change (in the base currency).
65
     * @param $absolute bool Set this to true, if you want only get the absolute value of the price (without minus)
66
     * @return float
67
     */
68
    public function getPrice(bool $absolute = false): float
69
    {
70
        if ($absolute) {
71
            return abs($this->extra['p']);
72
        }
73
        return $this->extra['p'];
74
    }
75
76
    /**
77
     * Returns the difference value of the change ($new_instock - $old_instock).
78
     * @param $absolute bool  Set this to true if you want only the absolute value of the difference.
79
     * @return int Difference is positive if instock has increased, negative if decreased.
80
     */
81
    public function getDifference(bool $absolute = false): int
82
    {
83
        // Check if one of the instock values is unknown
84
        if ($this->getNewInstock() == -2 || $this->getOldInstock() == -2) {
85
            return 0;
86
        }
87
88
        $difference = $this->getNewInstock() - $this->getOldInstock();
89
        if ($absolute) {
90
            return abs($difference);
0 ignored issues
show
Bug Best Practice introduced by
The expression return abs($difference) could return the type double which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
91
        }
92
93
        return $difference;
94
    }
95
96
    /**
97
     * Checks if the Change was an withdrawal of parts.
98
     * @return bool True if the change was an withdrawal, false if not.
99
     */
100
    public function isWithdrawal(): bool
101
    {
102
        return $this->getNewInstock() < $this->getOldInstock();
103
    }
104
105
}