When using some ready-made frameworks (such as Laravel and CodeIgniter), I noticed that the Models are usually extended from other classes that usually connect directly to the database, so I understand this is not mandatory, since Model must contain "business logic" ("business rules") and not necessarily connections to databases.
If what I understand is correct, the frameworks make the self-connection just to make it easier, so when we use the Model the settings of the database are loaded automatically by the framework when the Model is "built" by connecting to the database, or is it just to automate.
My question is, can I use a webservice in of the Model, as being "equivalent" to the database?
For example (Note that this example is completely fictitious):
class MyModel
{
public function putItem($preco, $descricao)
{
$rest = Rest('ws.example.com', 'PUT /', array(
'price' => $price,
'description' => $descricao
));
//Se o produto foi adicionado o HTTP status é 200 então putItem retorna TRUE
return $response->status === 200;
}
public function deleteItem($id)
{
Rest('ws.example.com', 'DELETE /items/{id}', array(
'id' => $id
));
$response = json_decode($rest->getRespose());
//Se o produto foi deletado o HTTP status é 200 então putItem retorna TRUE
return $response->status === 200;
}
public function getItem($id)
{
$rest = Rest('ws.example.com', 'GET /items/{id}', array(
'id' => $id
));
$response = json_decode($rest->getRespose());
if ($response->status === 200) {
//Se o produto existe o HTTP status é 200 então retorna informações dele
return $response->data;
}
//Se não retorna FALSE
return NULL;
}
}
Or should I do it differently and what would this way be?