1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Del\Expenses\Service; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Del\Expenses\Criteria\EntryCriteria; |
7
|
|
|
use Del\Expenses\Entity\EntryInterface; |
8
|
|
|
use Del\Expenses\Entity\Expenditure; |
9
|
|
|
use Del\Expenses\Entity\Income; |
10
|
|
|
use Del\Expenses\Repository\EntryRepository; |
11
|
|
|
use Del\Expenses\Value\Category; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class ExpensesService |
15
|
|
|
{ |
16
|
|
|
/** @var float $vatRate */ |
17
|
|
|
private $vatRate; |
18
|
|
|
|
19
|
|
|
/** @var EntryRepository $repository */ |
20
|
|
|
protected $repository; |
21
|
|
|
|
22
|
6 |
|
public function __construct(EntryRepository $repository, $vatRate = 20) |
23
|
|
|
{ |
24
|
6 |
|
$this->vatRate = $vatRate; |
|
|
|
|
25
|
6 |
|
$this->repository = $repository; |
26
|
6 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return EntryRepository |
30
|
|
|
*/ |
31
|
3 |
|
private function getRepository() |
32
|
|
|
{ |
33
|
3 |
|
return $this->repository; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param array $data |
38
|
|
|
* @param EntryInterface $entry |
39
|
|
|
* @return EntryInterface |
40
|
|
|
*/ |
41
|
6 |
|
private function setFromArray(array $data, EntryInterface $entry) |
42
|
|
|
{ |
43
|
6 |
|
if(!$data['date'] instanceof DateTime) { |
44
|
2 |
|
$data['date'] = new DateTime($data['date']); |
45
|
|
|
} |
46
|
6 |
|
$entry->setId($data['userId']) |
47
|
6 |
|
->setUserId($data['userId']) |
48
|
6 |
|
->setCategory(new Category($data['category'])) |
49
|
6 |
|
->setAmount($data['amount']) |
50
|
6 |
|
->setDate($data['date']) |
51
|
6 |
|
->setDescription($data['description']) |
52
|
6 |
|
->setNote($data['note']); |
53
|
6 |
|
return $entry; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return Expenditure |
58
|
|
|
*/ |
59
|
4 |
|
public function createExpenditureFromArray(array $data) |
60
|
|
|
{ |
61
|
4 |
|
$expenditure = new Expenditure(); |
62
|
|
|
/** @var Expenditure $expenditure */ |
63
|
4 |
|
$expenditure = $this->setFromArray($data, $expenditure); |
64
|
4 |
|
return $expenditure; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return Income |
69
|
|
|
*/ |
70
|
3 |
|
public function createIncomeFromArray(array $data) |
71
|
|
|
{ |
72
|
3 |
|
$income = new Income(); |
73
|
|
|
/** @var Income $income */ |
74
|
3 |
|
$income = $this->setFromArray($data, $income); |
75
|
3 |
|
return $income; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Pass an Income, Expenditure, or Expense Claim |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
1 |
|
public function toArray(EntryInterface $entry) |
83
|
|
|
{ |
84
|
|
|
return [ |
85
|
1 |
|
'id' => $entry->getId(), |
86
|
1 |
|
'userId' => $entry->getUserId(), |
87
|
1 |
|
'date' => $entry->getDate(), |
88
|
1 |
|
'amount' => $entry->getAmount(), |
89
|
1 |
|
'description' => $entry->getDescription(), |
90
|
1 |
|
'category' => $entry->getCategory()->getValue(), |
91
|
1 |
|
'note' => $entry->getNote(), |
92
|
|
|
]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param Income $income |
97
|
|
|
* @return Income |
98
|
|
|
*/ |
99
|
2 |
|
public function saveIncome(Income $income) |
100
|
|
|
{ |
101
|
2 |
|
return $this->getRepository()->save($income); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param Income $income |
106
|
|
|
*/ |
107
|
2 |
|
public function deleteIncome(Income $income) |
108
|
|
|
{ |
109
|
2 |
|
$this->deleteEntry($income); |
110
|
2 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param $id |
114
|
|
|
* @return Income |
115
|
|
|
*/ |
116
|
1 |
|
public function findIncomeById($id) |
117
|
|
|
{ |
118
|
1 |
|
$criteria = new EntryCriteria(); |
119
|
1 |
|
$criteria->setId($id); |
120
|
1 |
|
return $this->getRepository()->findOneByCriteria($criteria); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param Expenditure $expenditure |
125
|
|
|
* @return Expenditure |
126
|
|
|
*/ |
127
|
2 |
|
public function saveExpenditure(Expenditure $expenditure) |
128
|
|
|
{ |
129
|
2 |
|
return $this->getRepository()->save($expenditure); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param Expenditure $expenditure |
134
|
|
|
*/ |
135
|
2 |
|
public function deleteExpenditure(Expenditure $expenditure) |
136
|
|
|
{ |
137
|
2 |
|
$this->deleteEntry($expenditure); |
138
|
2 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param $id |
142
|
|
|
* @return Expenditure |
143
|
|
|
*/ |
144
|
1 |
|
public function findExpenditureById($id) |
145
|
|
|
{ |
146
|
1 |
|
$criteria = new EntryCriteria(); |
147
|
1 |
|
$criteria->setId($id); |
148
|
1 |
|
return $this->getRepository()->findOneByCriteria($criteria); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param EntryCriteria $criteria |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
1 |
|
public function findByCriteria(EntryCriteria $criteria) |
156
|
|
|
{ |
157
|
1 |
|
return $this->getRepository()->findByCriteria($criteria); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param EntryInterface $entry |
162
|
|
|
*/ |
163
|
3 |
|
public function deleteEntry(EntryInterface $entry) |
164
|
|
|
{ |
165
|
3 |
|
$this->getRepository()->delete($entry); |
166
|
3 |
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return float |
170
|
|
|
*/ |
171
|
|
|
public function getVatRate() |
172
|
|
|
{ |
173
|
|
|
return $this->vatRate; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param float $vatRate |
178
|
|
|
* @return ExpensesService |
179
|
|
|
*/ |
180
|
|
|
public function setVatRate($vatRate) |
181
|
|
|
{ |
182
|
|
|
$this->vatRate = $vatRate; |
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
} |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.