Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save forrestedw/234c930457f4cc462e0195763537d826 to your computer and use it in GitHub Desktop.
Save forrestedw/234c930457f4cc462e0195763537d826 to your computer and use it in GitHub Desktop.
<?php
namespace App\Acme;
use Carbon\Carbon;
// now, this class only gets the sales between the given dates.
// It doesn't deal with how with sales are stored, it doesn't
// deal with how to format the output, it doesn't deal with Auth
class SalesReporter
{
private $repository;
// The sales persistence uses an interface. That way we can
// pass any class in _that matches the interface_ to tell SalesReporter
// where to look for the data.
// in the codwe below you can see that this example takes a SalesCollection
// class that implements this interface, but it could be a db call (SalesRepository), or a
// and api call (SalesByApi) or anything else
public function __construct(SalesRepositoryInterface $repository)
{
$this->repository = $repository;
}
public function between($startDate, $endDate, SalesOutputInterface $formatter)
{
// now, the sales data is requested from the repository
$sales = $this->repository->between($startDate, $endDate);
// Now, the format is handled by a SalesOutputInterface. So long as the
// class that is passed as the third param implements this interface, the
// we will return a valid results. Again, this class is now no longer
// concerned with exactly _what_ that format is. In the code below,
// SalesHtmlOutput, that implements SalesOutputInterface, is passed.
return $formatter->output($sales);
}
}
// the repository interface...
interface SalesRepositoryInterface {
public function between(Carbon $startDate, Carbon $endDate);
}
// .. and an exmaple implementation of it.
class CollectionSalesRepository implements SalesRepositoryInterface {
public function between(Carbon $startDate, Carbon $endDate)
{
return collect([
[
'created_at' => new Carbon('2021-03-19 14:43:40'),
'charge' => '2111',
],
])->whereBetween('created_at', [$startDate, $endDate])->sum('charge') / 100;
}
}
// The formatter interface ....
interface SalesOutputInterface {
public function output(float $sales);
}
// .. and an example implementation of it.
class SalesHtmlOutput implements SalesOutputInterface {
public function output(float $sales)
{
return "<h1>Sales: $sales</h1>";
}
}
<?php
namespace App\Acme;
use Carbon\Carbon;
use Exception;
use Illuminate\Support\Facades\Auth;
class SalesReporter
{
public function between($startDate, $endDate)
{
if (! Auth::check()) throw new Exception('Auth required');
$sales = $this->queryDatabaseForSalesBetween($startDate, $endDate);
return $this->format($sales);
}
protected function queryDatabaseForSalesBetween(Carbon $startDate, Carbon $endDate)
{
return collect([
[
'created_at' => new Carbon('2021-03-19 14:43:40'),
'charge' => '211',
],
])->whereBetween('created_at', [$startDate, $endDate])->sum('charge') / 100;
}
protected function format(float $sales) {
return "<h1>Sales: $sales</h1>";
}
}
<?php
Route::get('/', function () {
$report = new SalesReporter(new SalesCollection);
$begin = new Carbon('2021-03-18 14:43:40');
$end = new Carbon('2021-03-20 14:43:40');
return $report->between($begin, $end, new SalesHtmlOutput);
});
@ZakariaAbbasi
Copy link

ZakariaAbbasi commented Jun 10, 2023

file web.php line 5
Line 5 should be changed
$report = new SalesReporter(new CollectionSalesRepository);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment