Skip to content

Instantly share code, notes, and snippets.

@juanwilde
Last active April 13, 2023 12:45
Show Gist options
  • Save juanwilde/96eb0c7376beb0eb5c97ab2c5c2c018c to your computer and use it in GitHub Desktop.
Save juanwilde/96eb0c7376beb0eb5c97ab2c5c2c018c to your computer and use it in GitHub Desktop.
// Coupled to framework (a service knowing about the implementation of the data layer)
class OrderService
{
public function getOrders(): iterable
{
return Order::all();
}
}
// Not coupled to framework
// first an interface
interface OrderRepository
{
public function all(): iterable;
}
// then the concrete implementation
final class EloquentOrderRepository implements OrderRepository
{
public function all(): iterable
{
return Order::all();
}
}
// And then, use that interface in the service
class OrderService
{
public function __construct(
private readonly OrderRepository $orderRepository
)
public function getOrders(): iterable
{
return $this->orderRepository->all();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment