If we have 2 classes
class databaseGateway
class format
Lets say we make a query through databaseGateway and get 5 rows. We want to apply formatting and update the row in the database.
We can do all of this simply within 1 loop
foreach($rows as $entry => $array) {
$update['page'] = $format->html($array['page'];
$update['id'] = $array['id'];
$databaseGateway->update($update);
}
But is this not against the Single Responsibility Principle? According to this principle, would we have to make one loop for formatting the rows, and another loop to update the database?
foreach($rows as $entry => $array) {
$rows[$entry] = $format->html($array['page']);
}
foreach($rows as $entry => $array) {
$databaseGateway->update($array);
}
This seems to be wasteful of resources, but also a duplication of code? Going against the Don't Repeat Yourself principle.
What would be the correct professional way to handle this issue? Seems very common.
Thanks