Skip to content

Commit

Permalink
Genesis
Browse files Browse the repository at this point in the history
  • Loading branch information
diego-betalabs committed Sep 14, 2018
1 parent 8a4a009 commit c57d350
Show file tree
Hide file tree
Showing 19 changed files with 1,375 additions and 1 deletion.
41 changes: 41 additions & 0 deletions app/Helpers/Engine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Helpers;

use Betalabs\Engine\Requests\EndpointResolver;
use Betalabs\Engine\Auth\Token;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Facades\Auth;

class Engine
{
/**
* Authenticate tenant in Engine
*
* @param \Illuminate\Contracts\Auth\Authenticatable $tenant
*/
public static function auth(Authenticatable $tenant): void
{
/** @var \Betalabs\LaravelHelper\Models\Tenant $tenant */
$registry = $tenant->engineRegistry;
$endpoint = rtrim($registry->api_base_uri, '/api');

EndpointResolver::setEndpoint($endpoint);
resolve(Token::class)->informToken($registry->api_access_token);
}

/**
* Make a valid Engine API URL
*
* @param null|string $endpoint
*
* @return string
*/
public static function makeUrl(string $endpoint): string
{
/** @var \Betalabs\LaravelHelper\Models\Tenant $registry */
$registry = Auth::user()->engineRegistry;

return $registry->api_base_uri . '/' . trim($endpoint, '/');
}
}
23 changes: 23 additions & 0 deletions app/Listeners/Genesis/Base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Listeners\Genesis;

use App\Helpers\Engine;
use Illuminate\Contracts\Auth\Authenticatable;
use Laravel\Passport\Passport;

abstract class Base
{

/**
* Authenticate tenant
*
* @param \Illuminate\Contracts\Auth\Authenticatable $authenticatable
*/
protected function authenticate(Authenticatable $authenticatable)
{
Passport::actingAs($authenticatable);
Engine::auth($authenticatable);
}

}
23 changes: 23 additions & 0 deletions app/Listeners/Genesis/CreateExtraFields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Listeners\Genesis;

use Betalabs\LaravelHelper\Models\Tenant;

class CreateExtraFields extends Base
{

/**
* Handle the event.
*
* @param \Betalabs\LaravelHelper\Models\Tenant $tenant
*/
public function handle(Tenant $tenant)
{
// Do not forget to authenticate
$this->authenticate($tenant);

// Here you can use API to add extra fields.
}

}
48 changes: 48 additions & 0 deletions app/Listeners/Genesis/CreateWorkflows.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Listeners\Genesis;

use App\Services\Genesis\Workflows\MultipleActionMenu;
use App\Services\Genesis\Workflows\SingleActionMenu;
use Betalabs\LaravelHelper\Models\Tenant;

class CreateWorkflows extends Base
{

/** @var \App\Services\Genesis\Workflows\SingleActionMenu */
private $singleActionMenu;

/** @var \App\Services\Genesis\Workflows\MultipleActionMenu */
private $multipleActionMenu;

/**
* CreateWorkflows constructor.
*
* @param \App\Services\Genesis\Workflows\SingleActionMenu $singleActionMenu
* @param \App\Services\Genesis\Workflows\MultipleActionMenu $multipleActionMenu
*/
public function __construct(
SingleActionMenu $singleActionMenu,
MultipleActionMenu $multipleActionMenu
) {
$this->singleActionMenu = $singleActionMenu;
$this->multipleActionMenu = $multipleActionMenu;
}


/**
* Handle the event.
*
* @param \Betalabs\LaravelHelper\Models\Tenant $tenant
*/
public function handle(Tenant $tenant)
{
// Do not forget to authenticate
$this->authenticate($tenant);

// Services to create workflows
$this->singleActionMenu->create();
$this->multipleActionMenu->create();
}

}
11 changes: 11 additions & 0 deletions app/Models/Enums/Engine/WorkflowConditionApproach.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Models\Enums\Engine;

use MyCLabs\Enum\Enum;

class WorkflowConditionApproach extends Enum
{
const AND = 'and';
const OR = 'or';
}
15 changes: 15 additions & 0 deletions app/Models/Enums/Engine/WorkflowConditionOperator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Models\Enums\Engine;

use MyCLabs\Enum\Enum;

class WorkflowConditionOperator extends Enum
{
const EQUAL = '=';
const DIFFERENT = '!=';
const GREATER_THAN = '>';
const GREATER_THAN_OR_EQUAL_TO = '>=';
const LESS_THAN = '<';
const LESS_THAN_OR_EQUAL_TO = '<=';
}
14 changes: 14 additions & 0 deletions app/Models/Enums/Engine/WorkflowIdentification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Models\Enums\Engine;

use MyCLabs\Enum\Enum;

class WorkflowIdentification extends Enum
{
const ML_AD_SINGLE_ACTION_MENU = 'ml-advertisements-single-action-menu';
const ML_AD_MULTIPLE_ACTION_MENU = 'ml-advertisements-multiple-action-menu';
const ML_AD_EXTRA_FORMS = 'ml-advertisements-extra-forms';
const ML_ORDER_SINGLE_ACTION_MENU = 'ml-orders-single-action-menu';
const ML_ORDER_MULTIPLE_ACTION_MENU = 'ml-orders-multiple-action-menu';
}
11 changes: 11 additions & 0 deletions app/Models/Enums/Engine/WorkflowStepApproach.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Models\Enums\Engine;

use MyCLabs\Enum\Enum;

class WorkflowStepApproach extends Enum
{
const SYNCHRONOUS = 'synchronous';
const ASYNCHRONOUS = 'asynchronous';
}
9 changes: 8 additions & 1 deletion app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace App\Providers;

use Illuminate\Support\Facades\Event;
use App\Listeners\Genesis\CreateExtraFields;
use App\Listeners\Genesis\CreateWorkflows;
use Betalabs\LaravelHelper\Events\GenesisCompleted;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
Expand All @@ -18,6 +20,11 @@ class EventServiceProvider extends ServiceProvider
Registered::class => [
SendEmailVerificationNotification::class,
],
GenesisCompleted::class => [
CreateWorkflows::class,
CreateExtraFields::class,
// You can add as much as you need...
],
];

/**
Expand Down
96 changes: 96 additions & 0 deletions app/Services/Engine/AbstractIndexer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace App\Services\Engine;

use Betalabs\Engine\Request;
use Illuminate\Support\Collection;
use Psr\Http\Message\ResponseInterface;

abstract class AbstractIndexer
{
/**
* @var array
*/
private $query = [];
/**
* @var int
*/
private $limit = 10;
/**
* @var int
*/
private $offset = 0;

/**
* Set the query property.
*
* @param array $query
*
* @return self
*/
public function setQuery(array $query): self
{
$this->query = $query;
return $this;
}

/**
* Set the limit property.
*
* @param int $limit
*
* @return self
*/
public function setLimit(int $limit): self
{
$this->limit = $limit;
return $this;
}

/**
* Set the offset property.
*
* @param int $offset
*
* @return self
*/
public function setOffset(int $offset): self
{
$this->offset = $offset;
return $this;
}

/**
* Return Engine endpoint
*
* @return string
*/
abstract protected function endpoint(): string;

/**
* Handle request response
*
* @param \Psr\Http\Message\ResponseInterface $response
*/
abstract protected function handleResponse(ResponseInterface $response): void;

/**
* Retrieve a resource
*
* @return \Illuminate\Support\Collection
*/
public function retrieve(): Collection
{
$query = http_build_query(array_merge($this->query, [
'_limit' => $this->limit,
'_offset' => $this->offset
]));

$request = Request::get();
$index = $request->send("{$this->endpoint()}?{$query}");

$this->handleResponse($request->getResponse());

return collect($index->data);
}
}
33 changes: 33 additions & 0 deletions app/Services/Engine/Event/Indexer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Services\Engine\Event;

use App\Services\Engine\AbstractIndexer;
use Psr\Http\Message\ResponseInterface;

class Indexer extends AbstractIndexer
{
/**
* Return Engine endpoint
*
* @return string
*/
protected function endpoint(): string
{
return 'events';
}

/**
* Handle request response
*
* @param \Psr\Http\Message\ResponseInterface $response
*/
protected function handleResponse(ResponseInterface $response): void
{
if ($response->getStatusCode() >= 300) {
throw new \RuntimeException(
'Engine Events could not be retrieved.'
);
}
}
}
31 changes: 31 additions & 0 deletions app/Services/Engine/Indexer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Services\Engine\Listener;

use App\Services\Engine\AbstractIndexer;
use Psr\Http\Message\ResponseInterface;

class Indexer extends AbstractIndexer
{
/**
* Return Engine endpoint
*
* @return string
*/
protected function endpoint(): string
{
return 'listeners';
}

/**
* Handle request response
*
* @param \Psr\Http\Message\ResponseInterface $response
*/
protected function handleResponse(ResponseInterface $response): void
{
if ($response->getStatusCode() >= 400) {
throw new \RuntimeException('Unable to retrieve listeners');
}
}
}
Loading

0 comments on commit c57d350

Please sign in to comment.