Add actions using a class

This commit is contained in:
Oliver Davies 2019-05-03 22:23:28 +01:00
parent 21a6f431db
commit 3f5c453d9b
6 changed files with 178 additions and 101 deletions

View file

@ -11,6 +11,7 @@
],
"require": {
"php": "^7.1",
"josephlavin/tap": "^1.0",
"symfony/config": "^3.4",
"symfony/console": "^3.4",
"symfony/dependency-injection": "^3.4",

View file

@ -14,11 +14,16 @@ class Filter
*/
private $properties = [];
/** @var Collection */
private $conditions;
/** @var Collection */
private $actions;
public function __construct()
{
$this->conditions = collect();
$this->actions = collect();
}
/**
@ -133,7 +138,7 @@ class Filter
*/
public function label(string $label): self
{
$this->properties['label'] = $label;
$this->actions->push(new FilterAction('label', $label));
return $this;
}
@ -143,7 +148,7 @@ class Filter
*/
public function archive(): self
{
$this->properties['shouldArchive'] = 'true';
$this->actions->push(new FilterAction('shouldArchive', 'true'));
return $this;
}
@ -165,8 +170,8 @@ class Filter
*/
public function spam(): self
{
$this->properties['shouldSpam'] = 'true';
$this->properties['shouldNeverSpam'] = 'false';
$this->actions->push(new FilterAction('shouldSpam', 'true'));
$this->actions->push(new FilterAction('shouldNeverSpam', 'false'));
return $this;
}
@ -176,8 +181,8 @@ class Filter
*/
public function neverSpam(): self
{
$this->properties['shouldSpam'] = 'false';
$this->properties['shouldNeverSpam'] = 'true';
$this->actions->push(new FilterAction('shouldSpam', 'false'));
$this->actions->push(new FilterAction('shouldNeverSpam', 'true'));
return $this;
}
@ -187,7 +192,7 @@ class Filter
*/
public function trash(): self
{
$this->properties['shouldTrash'] = 'true';
$this->actions->push(new FilterAction('shouldTrash', 'true'));
return $this;
}
@ -197,7 +202,7 @@ class Filter
*/
public function read(): self
{
$this->properties['markAsRead'] = 'true';
$this->actions->push(new FilterAction('markAsRead', 'true'));
return $this;
}
@ -207,7 +212,7 @@ class Filter
*/
public function star(): self
{
$this->properties['shouldStar'] = 'true';
$this->actions->push(new FilterAction('shouldStar', 'true'));
return $this;
}
@ -219,7 +224,7 @@ class Filter
*/
public function forward(string $to): self
{
$this->properties['forwardTo'] = $to;
$this->actions->push(new FilterAction('forwardTo', $to));
return $this;
}
@ -229,7 +234,7 @@ class Filter
*/
public function important(): self
{
$this->properties['shouldAlwaysMarkAsImportant'] = 'true';
$this->actions->push(new FilterAction('shouldAlwaysMarkAsImportant', 'true'));
return $this;
}
@ -239,7 +244,7 @@ class Filter
*/
public function notImportant(): self
{
$this->properties['shouldNeverMarkAsImportant'] = 'true';
$this->actions->push(new FilterAction('shouldNeverMarkAsImportant', 'true'));
return $this;
}
@ -251,7 +256,7 @@ class Filter
*/
public function categorise(string $category): self
{
$this->properties['smartLabelToApply'] = $category;
$this->actions->push(new FilterAction('smartLabelToApply', $category));
return $this;
}
@ -274,21 +279,24 @@ class Filter
*/
public function toArray(): array
{
return collect($this->properties)->merge(
$this->conditions->flatten(1)->mapWithKeys(function (FilterCondition $condition) {
$values = $condition->getValues();
return $this->conditions->merge($this->actions)->mapWithKeys(function (FilterProperty $property) {
$values = $property->getValues();
return [
$condition->getProperty() => $values->count() == 1
? $values->first()
: $values
];
})
)->toArray();
return [
$property->getProperty() => $values->count() == 1
? $values->first()
: $values
];
})->toArray();
}
public function getConditions(): Collection
{
return $this->conditions;
}
public function getActions(): Collection
{
return $this->actions;
}
}

View file

@ -0,0 +1,7 @@
<?php
namespace Opdavies\GmailFilterBuilder\Model;
class FilterAction extends FilterProperty
{
}

View file

@ -2,29 +2,6 @@
namespace Opdavies\GmailFilterBuilder\Model;
use Tightenco\Collect\Support\Collection;
class FilterCondition
class FilterCondition extends FilterProperty
{
/** @var string */
private $property;
/** @var Collection */
private $values;
public function __construct(string $property, $values)
{
$this->property = $property;
$this->values = collect($values);
}
public function getProperty(): string
{
return $this->property;
}
public function getValues(): Collection
{
return $this->values;
}
}

View file

@ -0,0 +1,30 @@
<?php
namespace Opdavies\GmailFilterBuilder\Model;
use Tightenco\Collect\Support\Collection;
abstract class FilterProperty
{
/** @var string */
private $property;
/** @var Collection */
private $values;
public function __construct(string $property, $values)
{
$this->property = $property;
$this->values = collect($values);
}
public function getProperty(): string
{
return $this->property;
}
public function getValues(): Collection
{
return $this->values;
}
}

View file

@ -3,6 +3,7 @@
namespace Opdavies\Tests\GmailFilterBuilder\Model;
use Opdavies\GmailFilterBuilder\Model\Filter;
use Opdavies\GmailFilterBuilder\Model\FilterAction;
use Opdavies\GmailFilterBuilder\Model\FilterCondition;
use PHPUnit\Framework\TestCase;
@ -198,10 +199,14 @@ class FilterTest extends TestCase
*/
public function labels_can_be_added()
{
$this->assertEquals(
['label' => 'Foo'],
$this->filter->label('Foo')->toArray()
);
$filter = $this->filter->label('Foo');
/** @var FilterAction $action */
$action = $filter->getActions()->first();
$this->assertInstanceOf(FilterAction::class, $action);
$this->assertSame('label', $action->getProperty());
$this->assertSame('Foo', $action->getValues()->first());
}
/**
@ -210,10 +215,14 @@ class FilterTest extends TestCase
*/
public function messages_can_be_archived()
{
$this->assertEquals(
['shouldArchive' => 'true'],
$this->filter->archive()->toArray()
);
$filter = $this->filter->archive();
/** @var FilterAction $action */
$action = $filter->getActions()->first();
$this->assertInstanceOf(FilterAction::class, $action);
$this->assertSame('shouldArchive', $action->getProperty());
$this->assertSame('true', $action->getvalues()->first());
}
/**
@ -222,10 +231,20 @@ class FilterTest extends TestCase
*/
public function messages_can_be_labelled_and_archived()
{
$this->assertEquals(
['shouldArchive' => 'true', 'label' => 'Foo'],
$this->filter->labelAndArchive('Foo')->toArray()
);
$filter = $this->filter->labelAndArchive('test');
$actions = $filter->getActions();
$this->assertCount(2, $actions);
tap($actions->first(), function (FilterAction $action) {
$this->assertSame('label', $action->getProperty());
$this->assertSame('test', $action->getValues()->first());
});
tap($actions->last(), function (FilterAction $action) {
$this->assertSame('shouldArchive', $action->getProperty());
$this->assertSame('true', $action->getValues()->first());
});
}
/**
@ -234,13 +253,20 @@ class FilterTest extends TestCase
*/
public function messages_can_be_marked_as_spam()
{
$this->assertEquals(
[
'shouldSpam' => 'true',
'shouldNeverSpam' => 'false'
],
$this->filter->spam()->toArray()
);
$filter = $this->filter->spam();
$actions = $filter->getActions();
$this->assertCount(2, $actions);
tap($actions->first(), function (FilterAction $action) {
$this->assertSame('shouldSpam', $action->getProperty());
$this->assertSame('true', $action->getValues()->first());
});
tap($actions->last(), function (FilterAction $action) {
$this->assertSame('shouldNeverSpam', $action->getProperty());
$this->assertSame('false', $action->getValues()->first());
});
}
/**
@ -249,13 +275,20 @@ class FilterTest extends TestCase
*/
public function messages_can_be_marked_as_not_spam()
{
$this->assertEquals(
[
'shouldSpam' => 'false',
'shouldNeverSpam' => 'true'
],
$this->filter->neverSpam()->toArray()
);
$filter = $this->filter->neverSpam();
$actions = $filter->getActions();
$this->assertCount(2, $actions);
tap($actions->first(), function (FilterAction $action) {
$this->assertSame('shouldSpam', $action->getProperty());
$this->assertSame('false', $action->getValues()->first());
});
tap($actions->last(), function (FilterAction $action) {
$this->assertSame('shouldNeverSpam', $action->getProperty());
$this->assertSame('true', $action->getValues()->first());
});
}
/**
@ -264,10 +297,13 @@ class FilterTest extends TestCase
*/
public function messages_can_be_deleted()
{
$this->assertEquals(
['shouldTrash' => 'true'],
$this->filter->trash()->toArray()
);
$filter = $this->filter->trash();
$action = $filter->getActions()->first();
$this->assertInstanceOf(FilterAction::class, $action);
$this->assertSame('shouldTrash', $action->getProperty());
$this->assertSame('true', $action->getValues()->first());
}
/**
@ -276,10 +312,13 @@ class FilterTest extends TestCase
*/
public function messages_can_be_marked_as_read()
{
$this->assertEquals(
['markAsRead' => 'true'],
$this->filter->read()->toArray()
);
$filter = $this->filter->read();
$action = $filter->getActions()->first();
$this->assertInstanceOf(FilterAction::class, $action);
$this->assertSame('markAsRead', $action->getProperty());
$this->assertSame('true', $action->getValues()->first());
}
/**
@ -288,10 +327,13 @@ class FilterTest extends TestCase
*/
public function messages_can_be_starred()
{
$this->assertEquals(
['shouldStar' => 'true'],
$this->filter->star()->toArray()
);
$filter = $this->filter->star();
$action = $filter->getActions()->first();
$this->assertInstanceOf(FilterAction::class, $action);
$this->assertSame('shouldStar', $action->getProperty());
$this->assertSame('true', $action->getValues()->first());
}
/**
@ -300,10 +342,13 @@ class FilterTest extends TestCase
*/
public function messages_can_be_forwarded()
{
$this->assertEquals(
['forwardTo' => 'foo@example.com'],
$this->filter->forward('foo@example.com')->toArray()
);
$filter = $this->filter->forward('foo@example.com');
$action = $filter->getActions()->first();
$this->assertInstanceOf(FilterAction::class, $action);
$this->assertSame('forwardTo', $action->getProperty());
$this->assertSame('foo@example.com', $action->getValues()->first());
}
/**
@ -312,10 +357,13 @@ class FilterTest extends TestCase
*/
public function messages_can_be_marked_as_important()
{
$this->assertEquals(
['shouldAlwaysMarkAsImportant' => 'true'],
$this->filter->important()->toArray()
);
$filter = $this->filter->important();
$action = $filter->getActions()->first();
$this->assertInstanceOf(FilterAction::class, $action);
$this->assertSame('shouldAlwaysMarkAsImportant', $action->getProperty());
$this->assertSame('true', $action->getValues()->first());
}
/**
@ -324,10 +372,13 @@ class FilterTest extends TestCase
*/
public function messages_can_be_marked_as_not_important()
{
$this->assertEquals(
['shouldNeverMarkAsImportant' => 'true'],
$this->filter->notImportant()->toArray()
);
$filter = $this->filter->notImportant();
$action = $filter->getActions()->first();
$this->assertInstanceOf(FilterAction::class, $action);
$this->assertSame('shouldNeverMarkAsImportant', $action->getProperty());
$this->assertSame('true', $action->getValues()->first());
}
/**
@ -336,10 +387,13 @@ class FilterTest extends TestCase
*/
public function messages_can_be_categorised()
{
$this->assertEquals(
['smartLabelToApply' => 'Foo'],
$this->filter->categorise('Foo')->toArray()
);
$filter = $this->filter->categorise('Foo');
$action = $filter->getActions()->first();
$this->assertInstanceOf(FilterAction::class, $action);
$this->assertSame('smartLabelToApply', $action->getProperty());
$this->assertSame('Foo', $action->getValues()->first());
}
/** @test */