In this article we will cover the first SOLID principle. Wondering "What were these principles again?" Go ahead and read the summary article.
The Single Responsibility Principle states that a class should have one, and only one, reason to change. In other words, a class's scope and responsibility should be narrowly focused. As we have said before, ignorance is bliss when it comes to class responsibilities. A class should do its job, and should not be affected by changes to any of its dependencies.
Consider the following class:
class OrderProcessor {
public function __construct(BillerInterface $biller)
{
$this->biller = $biller;
}
public function process(Order $order)
{
$recent = $this->getRecentOrderCount($order);
if ($recent > 0)
{
throw new Exception('Duplicate order likely.');
}
$this->biller->bill($order->account->id, $order->amount);
DB::table('orders')->insert([
'account' => $order->account->id,
'amount' => $order->amount;
'created_at' => Carbon::now();
]);
}
protected function getRecentOrderCount(Order $order)
{
$timestamp = Carbon::now()->subMinutes(5);
return DB::table('orders')
->where('account', $order->account->id)
->where('created_at', '>=', $timestamps)
->count();
}
}
What are the responsibilities of the class above? Obviously, its name implies that it is responsible for processing orders. But, based on the getRecentOrderCount method, we can also see that it is responsible for examining an account's order history in the database in order to detect duplicate orders. This extra validation responsibility means that we must change our order processor when our data store changes, as well as when our order validation rules change.
We should extract this responsibility into another class, such as an OrderRepository:
class OrderRepository {
public function getRecentOrderCount(Account $account)
{
$timestamp = Carbon::now()->subMinutes(5);
return DB::table('orders')
->where('account', $account->id)
->where('created_at', '>=', $timestamp)
->count();
}
public function logOrder(Order $order)
{
DB::table('orders')->insert([
'account' => $order->account->id,
'amount' => $order->amount;
'created_at' => Carbon::now();
]);
}
}
Then, we can inject our repository into the OrderProcessor, alleviating the OrderProcessor of the responsibility of researching an account's order history:
class OrderProcessor {
public function __construct(BillerInterface $biller,
OrderRepository $orders)
{
$this->biller = $biller;
$this->orders = $orders;
}
public function process(Order $order)
{
$recent = $this->orders->getRecentOrderCount($order->account);
if ($recent > 0)
{
throw new Exception('Duplicate order likely.');
}
$this->biller->bill($order->account->id, $order->amount);
$this->orders->logOrder($order);
}
}
Now that we have abstracted our order data gathering responsibilities, we no longer have to change our OrderProcessor when the method of retrieving and logging orders changes. Our class responsibilities are more focused and narrow, providing for cleaner, more expressive code, and a more maintainable application.
Keep in mind, the Single Responsibility Principle isn't just about less lines of code, it's about writing classes that have a narrow responsibility and a cohesive set of available methods, so make sure that all of the methods in a class are aligned with the class's overall responsibility. After building a library of small, clear classes with well defined responsibilities, our code will be more decoupled, easier to test, and friendlier to change.