ComestibleWithinDay::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Dominikzogg\EnergyCalculator\Entity;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use Saxulum\Accessor\Accessors\Get;
7
use Saxulum\Accessor\Accessors\Set;
8
use Saxulum\Accessor\AccessorTrait;
9
use Saxulum\Hint\Hint;
10
use Saxulum\Accessor\Prop;
11
use Symfony\Component\Validator\Constraints as Assert;
12
13
/**
14
 * @ORM\Entity()
15
 * @ORM\Table(name="comestible_within_day")
16
 * @method int getId()
17
 * @method Day getDay()
18
 * @method $this setDay(Day $day)
19
 * @method Comestible getComestible()
20
 * @method $this setComestible(Comestible $comestible)
21
 * @method float getAmount()
22
 * @method $this setAmount(float $amount)
23
 * @method \DateTime getCreatedAt()
24
 */
25
class ComestibleWithinDay
26
{
27
    use AccessorTrait;
28
29
    /**
30
     * @var string
31
     * @ORM\Column(name="id", type="string", length=24)
32
     * @ORM\Id
33
     * @ORM\GeneratedValue(strategy="NONE")
34
     */
35
    protected $id;
36
37
    /**
38
     * @var Day
39
     * @ORM\ManyToOne(targetEntity="Day", inversedBy="comestiblesWithinDay")
40
     * @ORM\JoinColumn(name="day_id", referencedColumnName="id", onDelete="CASCADE")
41
     */
42
    protected $day;
43
44
    /**
45
     * @var Comestible
46
     * @ORM\ManyToOne(targetEntity="Comestible")
47
     * @ORM\JoinColumn(name="comestible_id", referencedColumnName="id")
48
     * @Assert\NotNull(message="day.comestibleWithinDay.comestible.notnull")
49
     */
50
    protected $comestible;
51
52
    /**
53
     * @var float
54
     * @ORM\Column(name="amount", type="decimal", precision=10, scale=4, nullable=false)
55
     * @Assert\NotNull(message="day.comestibleWithinDay.amount.notnull")
56
     * @Assert\GreaterThanOrEqual(value=0, message="day.comestibleWithinDay.amount.greaterthanorequal")
57
     */
58
    protected $amount = 0;
59
60
    /**
61
     * @var \DateTime
62
     * @ORM\Column(name="created_at", type="datetime", nullable=true)
63
     */
64
    protected $createdAt;
65
66
    public function __construct()
67
    {
68
        $this->id = new \MongoId();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \MongoId() of type object<MongoId> is incompatible with the declared type string of property $id.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
69
        $this->createdAt = new \DateTime();
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function __toString()
76
    {
77
        return (string) $this->getComestible()->getName();
78
    }
79
80
    protected function _initProps()
81
    {
82
        $this->_prop((new Prop('id', Hint::INT))->method(Get::PREFIX));
83
        $this->_prop(
84
            (new Prop('day', 'Dominikzogg\EnergyCalculator\Entity\Day', true, 'comestiblesWithinDay', Prop::REMOTE_MANY))
85
                ->method(Get::PREFIX)
86
                ->method(Set::PREFIX)
87
        );
88
        $this->_prop(
89
            (new Prop('comestible', 'Dominikzogg\EnergyCalculator\Entity\Comestible'))
90
                ->method(Get::PREFIX)
91
                ->method(Set::PREFIX)
92
        );
93
        $this->_prop((new Prop('amount', Hint::NUMERIC))->method(Get::PREFIX)->method(Set::PREFIX));
94
        $this->_prop((new Prop('createdAt', '\DateTime'))->method(Get::PREFIX));
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    public function getName()
101
    {
102
        return $this->getComestible()->getName();
103
    }
104
105
    /**
106
     * @return float
107
     */
108
    public function getCalorie()
109
    {
110
        return $this->getComestible()->getCalorie() * $this->getAmount() / 100;
111
    }
112
113
    /**
114
     * @return float
115
     */
116
    public function getProtein()
117
    {
118
        return $this->getComestible()->getProtein() * $this->getAmount() / 100;
119
    }
120
121
    /**
122
     * @return float
123
     */
124
    public function getCarbohydrate()
125
    {
126
        return $this->getComestible()->getCarbohydrate() * $this->getAmount() / 100;
127
    }
128
129
    /**
130
     * @return float
131
     */
132
    public function getFat()
133
    {
134
        return $this->getComestible()->getFat() * $this->getAmount() / 100;
135
    }
136
}
137