Skip to content

Commit

Permalink
upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
diegosm committed Mar 15, 2020
2 parents f09763e + c0ce92e commit 24f402a
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 35 deletions.
11 changes: 11 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<ruleset name="PHP_CodeSniffer">
<description>The coding standard for our project.</description>
<rule ref="PSR2"/>

<file>src</file>
<file>tests</file>

<!-- Show progression -->
<arg value="p"/>
</ruleset>
23 changes: 12 additions & 11 deletions src/Factories/ReportBuilderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,28 @@

use Carbon\Carbon;
use ReportBuilder\Processors\DailyProcessor;
use ReportBuilder\Processors\DayProcessor;
use ReportBuilder\Processors\MonthlyProcessor;
use ReportBuilder\Processors\WeeklyProcessor;

class ReportBuilderFactory
{
public static function create(Carbon $startDate, Carbon $endDate, $firstDayWeek=0)
public static function create(Carbon $startDate, Carbon $endDate, $firstDayWeek = 0)
{
$days = $startDate->diffInDays($endDate);

switch ($days) {

case ($days > 30):
return new MonthlyProcessor($startDate, $endDate, $firstDayWeek);
break;
if ($days > 30) {
return new MonthlyProcessor($startDate, $endDate, $firstDayWeek);
}

case ($days > 13 && $days <= 30):
return new WeeklyProcessor($startDate, $endDate, $firstDayWeek);
break;
if ($days === 0) {
return new DayProcessor($startDate, $endDate, $firstDayWeek);
}

default:
return new DailyProcessor($startDate, $endDate, $firstDayWeek);
if ($days > 13 && $days <= 30) {
return new WeeklyProcessor($startDate, $endDate, $firstDayWeek);
}

return new DailyProcessor($startDate, $endDate, $firstDayWeek);
}
}
20 changes: 20 additions & 0 deletions src/Processors/DayProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace ReportBuilder\Processors;

// use Carbon\CarbonPeriod;
use ReportBuilder\Contracts\Processor;
use ReportBuilder\Processors\Processor as Base;

class DayProcessor extends Base implements Processor
{
public function make(): array
{
return [
[
'startDate' => (clone $this->startDate)->startOfDay(),
'endDate' => (clone $this->startDate)->endOfDay()
]
];
}
}
2 changes: 1 addition & 1 deletion src/Processors/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Processor
protected $startDate;
protected $endDate;

public function __construct(Carbon $startDate = null, Carbon $endDate = null, $firstDayWeek=0)
public function __construct(Carbon $startDate = null, Carbon $endDate = null, $firstDayWeek = 0)
{
Carbon::setWeekStartsAt($firstDayWeek);
$this->startDate = $startDate ?: Carbon::now();
Expand Down
33 changes: 21 additions & 12 deletions src/ReportResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,36 @@ public function setDates(array $dates): self
*/
public function results()
{
return (isset($this->dates) && count($this->dates) > 0) ? $this->resultsWithDates() : $this->resultsWithoutDates();
return (isset($this->dates) && count($this->dates) > 0) ?
$this->resultsWithDates() :
$this->resultsWithoutDates();
}


/**
*
*/
private function resultsWithDates()
{
$data = [];

for ($x=0; $x < count($this->dates); $x++) {
foreach ($this->reportables->get() as $reportable => $methods) {
for ($x = 0; $x < count($this->dates); $x++) {
foreach ($this->reportables->get() as $reportable => $methods) {
if (is_array($methods)) {
for ($y=0; $y<count($methods); $y++) {
$data[$reportable][$methods[$y]][] = $this->getResult($reportable, $methods[$y], $this->dates[$x]['startDate'], $this->dates[$x]['endDate']);
$data[$reportable][$methods[$y]][] = $this->getResult(
$reportable,
$methods[$y],
$this->dates[$x]['startDate'],
$this->dates[$x]['endDate']
);
}
} else {
$data[$reportable][] = $this->getResult($reportable, $methods, $this->dates[$x]['startDate'], $this->dates[$x]['endDate']);
$data[$reportable][] = $this->getResult(
$reportable,
$methods,
$this->dates[$x]['startDate'],
$this->dates[$x]['endDate']
);
}
}
}
Expand All @@ -80,7 +91,7 @@ private function resultsWithoutDates()
{
$data = [];

foreach ($this->reportables->get() as $reportable => $methods) {
foreach ($this->reportables->get() as $reportable => $methods) {
if (is_array($methods)) {
for ($y=0; $y<count($methods); $y++) {
$data[$reportable][$methods[$y]][] = $this->getResult($reportable, $methods[$y]);
Expand All @@ -98,13 +109,13 @@ private function resultsWithoutDates()
*
* @param $class
* @param $method
* @param $startDate
* @param $endDate
* @param null $startDate
* @param null $endDate
* @return mixed
* @throws \ReflectionException
*/
private function getResult($class, $method, $startDate = null, $endDate = null)
{

// Get Parameters from class and method
$rClass = (new \ReflectionClass(new $class));
$rClassParameters = $rClass->getMethod($method)->getParameters();
Expand All @@ -127,10 +138,8 @@ private function getResult($class, $method, $startDate = null, $endDate = null)
return (new $class)->$method(...$parameters2Method);
}


private function getParameters($startDate = null, $endDate = null)
{

// if is only parameters
if (isset($this->parameters) && !isset($startDate, $endDate)) {
$parameters = $this->parameters;
Expand Down
35 changes: 35 additions & 0 deletions tests/Unit/DayProcessorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Test\Unit;

use Carbon\Carbon;
use PHPUnit\Framework\TestCase;
use ReportBuilder\Processors\DailyProcessor;

class DayProcessorTest extends TestCase
{
protected $processor;
protected $dates;

protected function setUp() : void
{
$this->processor = new DailyProcessor(
Carbon::createFromDate(2018, 8, 7)->startOfDay(),
Carbon::createFromDate(2018, 8, 7)->endOfDay(),
0
);
$this->dates = $this->processor->make();
parent::setUp();
}

public function testShouldHaveSameLastDay()
{
$lastDay = Carbon::createFromDate(2018, 8, 7)->endOfDay();
$this->assertEquals($lastDay, end($this->dates)['endDate']);
}

public function testShouldHaveOneDay()
{
$this->assertEquals(1, count($this->dates));
}
}
13 changes: 12 additions & 1 deletion tests/Unit/ReportBuilderFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Carbon\Carbon;
use PHPUnit\Framework\TestCase;
use ReportBuilder\Processors\DailyProcessor;
use ReportBuilder\Processors\DayProcessor;
use ReportBuilder\Processors\MonthlyProcessor;
use ReportBuilder\Processors\WeeklyProcessor;
use ReportBuilder\Factories\ReportBuilderFactory;
Expand All @@ -16,7 +17,17 @@ protected function setUp() : void
parent::setUp();
}

public function testDefaultProcessorMustBeDaily()
public function testDefaultProcessorMustBeDay()
{
$obj = ReportBuilderFactory::create(
Carbon::createFromFormat('Y-m-d H:i:s', '2019-9-19 00:00:00')->startOfDay(),
Carbon::createFromFormat('Y-m-d H:i:s', '2019-9-19 00:00:00')->endOfDay()
);

$this->assertInstanceOf(DayProcessor::class, $obj);
}

public function tesProcessorMustBeDaily()
{
$obj = ReportBuilderFactory::create(Carbon::now(), Carbon::now()->addDays(13));
$this->assertInstanceOf(DailyProcessor::class, $obj);
Expand Down
7 changes: 6 additions & 1 deletion tests/Unit/ReportResultsFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ class ReportResultsFactoryTest extends TestCase
protected function setUp() : void
{
$this->reportables = (new ReportableCollection())->add(FakeRepository::class, 'popular');
$this->dates = (new DailyProcessor(Carbon::createFromDate(2018, 8, 7), Carbon::createFromDate(2018, 8, 17), 0))->make();
$this->dates = (new DailyProcessor(
Carbon::createFromDate(2018, 8, 7),
Carbon::createFromDate(2018, 8, 17),
0
))->make();

$this->parameters = [
'param' => 'val',
'parameter' => 'value'
Expand Down
17 changes: 8 additions & 9 deletions tests/Utilities/FakeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@

class FakeRepository
{
public function between($startDate=null, $endDate=null)
public function between($startDate = null, $endDate = null)
{
return [
'data' => 'FakeRepository > between',
'startDate' => $startDate,
'endDate' => $endDate
];
'data' => 'FakeRepository > between',
'startDate' => $startDate,
'endDate' => $endDate
];
}

public function popular($startDate=null, $endDate=null, $param=null)
public function popular($startDate = null, $endDate = null, $param = null)
{
return [
'data' => 'FakeRepository > popular',
'data' => 'FakeRepository > popular',
'startDate' => isset($startDate) ? $startDate->format('d/m/Y 00:00:00') : $startDate,
'endDate' => isset($endDate) ? $endDate->format('d/m/Y 23:59:59') : $endDate,
'endDate' => isset($endDate) ? $endDate->format('d/m/Y 23:59:59') : $endDate,
'parameter' => 'Parameter value: ' . $param
];
}
Expand All @@ -28,7 +28,6 @@ public function simpleWithParameterNoDate($param)
return $param;
}


public function simple()
{
return 'simple';
Expand Down

0 comments on commit 24f402a

Please sign in to comment.