RunningBalance   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 109
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getId() 0 4 1
A setUser() 0 5 1
A getUser() 0 4 1
A getBalance() 0 4 1
A setBalance() 0 5 1
A addBalance() 0 4 1
A subtractBalance() 0 4 1
A jsonSerialize() 0 12 2
1
<?php
2
3
namespace JhFlexiTime\Entity;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use ZfcUser\Entity\UserInterface;
7
use DateTime;
8
use JsonSerializable;
9
10
/**
11
 * Class RunningBalance
12
 * @package JhFlexiTime\Entity
13
 * @author Aydin Hassan <[email protected]>
14
 * @ORM\Entity
15
 * @ORM\Table(name="running_balance")
16
 */
17
class RunningBalance implements JsonSerializable
18
{
19
    /**
20
     * @var int
21
     *
22
     * @ORM\Id
23
     * @ORM\Column(type="integer")
24
     * @ORM\GeneratedValue(strategy="AUTO")
25
     */
26
    protected $id = null;
27
    
28
    /**
29
     * @ORM\ManyToOne(targetEntity="JhUser\Entity\User")
30
     */
31
    protected $user = null;
32
    
33
     /**
34
     * @var int
35
     * 
36
     * @ORM\Column(type="string", length=255, nullable=false)
37
     */
38
    protected $balance = 0;
39
    
40
    /**
41
     * Set Defaults
42
     */
43
    public function __construct()
44
    {
45
        $this->date = new DateTime();
0 ignored issues
show
Bug introduced by
The property date does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
46
    }
47
    
48
    /**
49
     * @return int
50
     */
51
    public function getId()
52
    {
53
        return $this->id;
54
    }
55
56
    /**
57
     *
58
     * @param \ZfcUser\Entity\UserInterface $user
59
     * @return \JhFlexiTime\Entity\RunningBalance
60
     */
61
    public function setUser(UserInterface $user)
62
    {
63
        $this->user = $user;
64
        return $this;
65
    }
66
67
    /**
68
     * @return \ZfcUser\Entity\UserInterface
69
     */
70
    public function getUser()
71
    {
72
        return $this->user;
73
    }
74
75
    /**
76
     * @return int
77
     */
78
    public function getBalance()
79
    {
80
        return $this->balance;
81
    }
82
83
    /**
84
     * @param int $balance
85
     * @return \JhFlexiTime\Entity\RunningBalance
86
     */
87
    public function setBalance($balance)
88
    {
89
        $this->balance = $balance;
90
        return $this;
91
    }
92
93
    /**
94
     * @param float $balance
95
     */
96
    public function addBalance($balance)
97
    {
98
        $this->balance += (float) $balance;
99
    }
100
101
    /**
102
     * @param float $balance
103
     */
104
    public function subtractBalance($balance)
105
    {
106
        $this->balance -= (float) $balance;
107
    }
108
109
    /**
110
     * @return array
111
     * @throws \Exception
112
     */
113
    public function jsonSerialize()
114
    {
115
        if (!$this->user instanceof UserInterface) {
116
            throw new \Exception('User Must be an instance of \ZfcUser\Entity\UserInterface');
117
        }
118
119
        return [
120
            'id'        => $this->id,
121
            'user'      => $this->user->getId(),
122
            'balance'   => $this->balance,
123
        ];
124
    }
125
}
126