Skip to content

Commit

Permalink
Added New Zoho Polymorphism model to reused across laravel project mo…
Browse files Browse the repository at this point in the history
…dels, that holds the zoho id inside local database.
  • Loading branch information
aemaddin committed Jun 10, 2020
1 parent 5c375af commit 5036126
Show file tree
Hide file tree
Showing 15 changed files with 484 additions and 72 deletions.
2 changes: 0 additions & 2 deletions database/migrations/.gitignore

This file was deleted.

33 changes: 33 additions & 0 deletions database/migrations/2020_06_04_000001_create_zohos_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateZohosTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('zohos', function (Blueprint $table) {
$table->id();
$table->string('zoho_id')->unique()->index();
$table->morphs('zohoable');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('personal_access_tokens');
}
}
22 changes: 22 additions & 0 deletions src/Contracts/Repositories/ZohoableRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Asciisd\Zoho\Contracts\Repositories;

use Asciisd\Zoho\CriteriaBuilder;

interface ZohoableRepository
{
/**
* This used when we need to search for your current model record on zoho
*
* @return String|CriteriaBuilder
*/
public function searchCriteria();

/**
* Array for mandatory fields that required to create new record
*
* @return array
*/
public function zohoMandatoryFields();
}
65 changes: 65 additions & 0 deletions src/CriteriaBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php


namespace Asciisd\Zoho;


class CriteriaBuilder
{
protected $criteria;
protected $operators = ['equals', 'starts_with'];

/**
* add criteria to the search
*
* @param $field
* @param $value
* @param string $operator
* @return $this
*/
public static function where($field, $value, $operator = 'equals')
{
$builder = new CriteriaBuilder();
$builder->criteria = "";

$builder->criteria .= static::queryBuilder($field, $operator, $value);

return $builder;
}

public function startsWith($field, $value, $operator = 'starts_with')
{
$this->criteria .= ' and ' . $this->queryBuilder($field, $operator, $value);

return $this;
}

public function andWhere($field, $value, $operator = 'equals')
{
$this->criteria .= ' and ' . $this->queryBuilder($field, $operator, $value);

return $this;
}

public function orWhere($field, $value, $operator = 'equals')
{
$this->criteria .= ' or ' . $this->queryBuilder($field, $operator, $value);

return $this;
}

private static function queryBuilder(...$args)
{
return "($args[0]:$args[1]:$args[2])";
}

public function toString()
{
return $this->getCriteria() ?? '';
}

public function getCriteria()
{
return $this->criteria;
}
}
31 changes: 31 additions & 0 deletions src/Exceptions/InvalidZohoable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Asciisd\Zoho\Exceptions;

use Exception;
use Illuminate\Database\Eloquent\Model;

class InvalidZohoable extends Exception
{
/**
* Create a new InvalidTapCustomer instance.
*
* @param Model $owner
* @return static
*/
public static function nonZohoable($owner)
{
return new static(class_basename($owner) . ' is not a Zohoable. See the createAsZohoable method.');
}

/**
* Create a new InvalidTapCustomer instance.
*
* @param Model $owner
* @return static
*/
public static function exists($owner)
{
return new static(class_basename($owner) . " is already a Zohoable with ID {$owner->zohoId()}");
}
}
25 changes: 25 additions & 0 deletions src/Models/Zoho.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Asciisd\Zoho\Models;

use Illuminate\Database\Eloquent\Model;

class Zoho extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'zohoable_type', 'zohoable_id', 'zoho_id'
];

/**
* Get the owning zohoable model.
*/
public function zohoable()
{
return $this->morphTo();
}
}
8 changes: 4 additions & 4 deletions src/Providers/ZohoServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ protected function registerPublishing()
__DIR__ . '/../../config/zoho.php' => $this->app->configPath('zoho.php'),
], 'zoho-config');

// $this->publishes([
// __DIR__ . '/../../database/migrations' => $this->app->databasePath('migrations'),
// ], 'zoho-migrations');
//
$this->publishes([
__DIR__ . '/../../database/migrations' => $this->app->databasePath('migrations'),
], 'zoho-migrations');

// $this->publishes([
// __DIR__ . '/../../resources/views' => $this->app->resourcePath('views/vendor/zoho'),
// ], 'zoho-views');
Expand Down
2 changes: 1 addition & 1 deletion src/RestClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct($rest)

public function getAllModules()
{
return $this->rest->getAllModules();
return $this->rest->getAllModules()->getData();
}

public function useModule($module_api_name = 'leads')
Expand Down
Loading

0 comments on commit 5036126

Please sign in to comment.