Day   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 7
Bugs 2 Features 2
Metric Value
c 7
b 2
f 2
dl 0
loc 139
rs 10
wmc 12
lcom 1
cbo 5

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getRoleNamePart() 0 4 1
A getCalorie() 0 9 2
A getProtein() 0 9 2
A getCarbohydrate() 0 9 2
A getFat() 0 9 2
A __construct() 0 7 1
A _initProps() 0 14 1
A __toString() 0 4 1
1
<?php
2
3
namespace Dominikzogg\EnergyCalculator\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use Doctrine\ORM\Mapping as ORM;
8
use Dominikzogg\EnergyCalculator\Voter\RelatedObjectInterface;
9
use Saxulum\Accessor\Accessors\Add;
10
use Saxulum\Accessor\Accessors\Get;
11
use Saxulum\Accessor\Accessors\Remove;
12
use Saxulum\Accessor\Accessors\Set;
13
use Saxulum\Accessor\AccessorTrait;
14
use Saxulum\Hint\Hint;
15
use Saxulum\Accessor\Prop;
16
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
17
use Symfony\Component\Validator\Constraints as Assert;
18
19
/**
20
 * @ORM\Entity(repositoryClass="Dominikzogg\EnergyCalculator\Repository\DayRepository")
21
 * @ORM\Table(name="day", uniqueConstraints={
22
 *      @ORM\UniqueConstraint(name="day_per_user_idx", columns={"date", "user_id"})
23
 * })
24
 * @UniqueEntity(fields={"date", "user"}, message="day.unique")
25
 * @method float getId()
26
 * @method \DateTime getDate()
27
 * @method $this setDate(\DateTime $date)
28
 * @method float getWeight()
29
 * @method $this setWeight(float $weight)
30
 * @method $this addComestiblesWithinDay(ComestibleWithinDay $comestiblesWithinDay)
31
 * @method ComestibleWithinDay[] getComestiblesWithinDay()
32
 * @method $this removeComestiblesWithinDay(ComestibleWithinDay $comestiblesWithinDay)
33
 * @method $this setComestiblesWithinDay(array $comestiblesWithinDay)
34
 * @method \DateTime getCreatedAt()
35
 */
36
class Day implements UserReferenceInterface, RelatedObjectInterface
37
{
38
    use AccessorTrait;
39
    use UserReferenceTrait;
40
41
    /**
42
     * @var string
43
     * @ORM\Column(name="id", type="string", length=24)
44
     * @ORM\Id
45
     * @ORM\GeneratedValue(strategy="NONE")
46
     */
47
    protected $id;
48
49
    /**
50
     * @var \DateTime
51
     * @ORM\Column(name="date", type="date", nullable=false)
52
     * @Assert\NotNull()
53
     * @Assert\Date()
54
     */
55
    protected $date;
56
57
    /**
58
     * @var float
59
     * @ORM\Column(name="weight", type="decimal", precision=10, scale=4, nullable=true)
60
     * @Assert\Range(
61
     *      min=0,
62
     *      max=500,
63
     *      minMessage="day.weight.range.minmessage",
64
     *      maxMessage="day.weight.range.maxmessage",
65
     *      invalidMessage="day.weight.range.invalidmessage"
66
     * )
67
     */
68
    protected $weight;
69
70
    /**
71
     * @var ComestibleWithinDay[]|Collection
72
     * @ORM\OneToMany(targetEntity="ComestibleWithinDay", mappedBy="day", cascade={"persist"})
73
     * @ORM\OrderBy({"createdAt"="ASC", "id"="ASC"})
74
     * @Assert\Valid()
75
     */
76
    protected $comestiblesWithinDay;
77
78
    /**
79
     * @var \DateTime
80
     * @ORM\Column(name="created_at", type="datetime", nullable=true)
81
     */
82
    protected $createdAt;
83
84
    public function __construct()
85
    {
86
        $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...
87
        $this->date = new \DateTime();
88
        $this->comestiblesWithinDay = new ArrayCollection();
89
        $this->createdAt = new \DateTime();
90
    }
91
92
    protected function _initProps()
93
    {
94
        $this->_prop((new Prop('id', Hint::NUMERIC))->method(Get::PREFIX));
95
        $this->_prop((new Prop('date', '\DateTime', true))->method(Get::PREFIX)->method(Set::PREFIX));
96
        $this->_prop((new Prop('weight', Hint::NUMERIC))->method(Get::PREFIX)->method(Set::PREFIX));
97
        $this->_prop(
98
            (new Prop('comestiblesWithinDay', 'Dominikzogg\EnergyCalculator\Entity\ComestibleWithinDay[]', true, 'day', Prop::REMOTE_ONE))
99
                ->method(Add::PREFIX)
100
                ->method(Get::PREFIX)
101
                ->method(Remove::PREFIX)
102
                ->method(Set::PREFIX)
103
        );
104
        $this->_prop((new Prop('createdAt', '\DateTime'))->method(Get::PREFIX));
105
    }
106
107
    /**
108
     * @return string
109
     */
110
    public function __toString()
111
    {
112
        return (string) $this->getDate()->format('d.m.Y');
113
    }
114
115
    /**
116
     * @return float
117
     */
118
    public function getCalorie()
119
    {
120
        $calorie = 0;
121
        foreach ($this->getComestiblesWithinDay() as $comestiblesWithinDay) {
122
            $calorie += $comestiblesWithinDay->getCalorie();
123
        }
124
125
        return $calorie;
126
    }
127
128
    /**
129
     * @return float
130
     */
131
    public function getProtein()
132
    {
133
        $protein = 0;
134
        foreach ($this->getComestiblesWithinDay() as $comestiblesWithinDay) {
135
            $protein += $comestiblesWithinDay->getProtein();
136
        }
137
138
        return $protein;
139
    }
140
141
    /**
142
     * @return float
143
     */
144
    public function getCarbohydrate()
145
    {
146
        $carbohydrate = 0;
147
        foreach ($this->getComestiblesWithinDay() as $comestiblesWithinDay) {
148
            $carbohydrate += $comestiblesWithinDay->getCarbohydrate();
149
        }
150
151
        return $carbohydrate;
152
    }
153
154
    /**
155
     * @return float
156
     */
157
    public function getFat()
158
    {
159
        $fat = 0;
160
        foreach ($this->getComestiblesWithinDay() as $comestiblesWithinDay) {
161
            $fat += $comestiblesWithinDay->getFat();
162
        }
163
164
        return $fat;
165
    }
166
167
    /**
168
     * @return string
169
     */
170
    public function getRoleNamePart()
171
    {
172
        return 'day';
173
    }
174
}
175