Skip to content

Instantly share code, notes, and snippets.

@andybeak
Last active February 8, 2018 16:40
Show Gist options
  • Save andybeak/07dcdf61c9d0ceda01414d3feed489be to your computer and use it in GitHub Desktop.
Save andybeak/07dcdf61c9d0ceda01414d3feed489be to your computer and use it in GitHub Desktop.
SOLID laravel examples #blog
<?php
namespace App\Domain\Employee;
// this introduces a source code dependency
use App\Domain\Employee\ScreenOutput;
class Employee
{
// this violates the dependency inversion principal because we depend on a concretion
public function __construct(ScreenOutput $outputDevice) {
$this->outputDevice = $outputDevice;
}
}
<?php
public function showWeeklyHours()
{
$employeeId = e(Input::get('employee_id',''));
if(!$query && $query == '') return Response::json(array(), 400);
$hours = HoursWorked::where('active', true)
->where('employee_id',$employeeId)
->where('created_at', '>', strtototime('-1 week'))
->orderBy('created_at','desc')
->take(7)
->toArray();
// Data normalization and validation
$returndata = [];
foreach ($hours as $hour) {
$returnData = [
'date' => $hour['created_at']
'hours' => number_format($hour['hours_worked'], 2, '.', '');
];
}
// format the data and return it
return Response::json(array(
'hours' => $returnData
));
}
public function calculateWeeklyPay()
{
$employeeId = e(Input::get('employee_id',''));
if(!$query && $query == '') return Response::json(array(), 400);
$hours = HoursWorked::where('active', true)
->where('employee_id',$employeeId)
->where('created_at', '>', strtototime('-1 week'))
->orderBy('created_at','desc')
->take(7)
->toArray();
$employee = Employee::find($employeeId);
// Calculation
$wage = 0;
foreach ($hours as $hour) {
$wage += $employee->hourlyWage * $hour['hours_worked'];
}
// format the data and return it
return Response::json(array(
'week_starting' => strtototime('-1 week')
'wage' => $wage
));
}
<?php
class PayCalculator { }
// the below class would be in a different file!
class PermanentEmployeePayCalculator extends PayCalculator
{
public function calculatePay(int $employeeId) { ... }
}
// the below class would be in a different file!
class TemporaryEmployeePayCalculator extends PayCalculator
{
public function calculatePay(int $employeeId, float $hourlyWage) { ... }
}
<?php
namespace App\Domain\Employee;
class PermanentEmployeePayCalculator implements PayCalculatorInterface
{
public function calculatePay(int $employeeId) { ... }
}
// the below class would be in a different file!
class TemporaryEmployeePayCalculator implements PayCalculatorInterface
{
public function calculatePay(int $employeeId) { ... }
}
<?php
namespace App\Domain\Employee;
class PayCalculator
{
public function calculatePay(int $employeeId) { ... }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment