Skip to content

Commit

Permalink
Merge branch '0.7-hotfix-mail' into 0.7-hotfix-migration
Browse files Browse the repository at this point in the history
  • Loading branch information
kaioken committed Jan 24, 2021
2 parents 81c63ef + 26c10db commit fae930d
Show file tree
Hide file tree
Showing 11 changed files with 313 additions and 70 deletions.
26 changes: 23 additions & 3 deletions src/Database/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,28 @@ public function beforeUpdate()
$this->updated_at = date('Y-m-d H:i:s');
}

/**
* Make a cascade softdelete.
*
* @return void
*/
public function cascadeSoftDelete(): void
{
foreach ($this->getDependentRelationships() as $relation => $data) {
$relationData = $this->{'get'.$relation}();

if ($data['type'] === Relation::HAS_ONE) {
if (isset($relationData)) {
$relationData->softDelete();
}
} elseif ($data['type'] === Relation::HAS_MANY && isset($relationData[0])) {
foreach ($relationData as $singleData) {
$singleData->softDelete();
}
}
}
}

/**
* Soft Delete.
*
Expand Down Expand Up @@ -388,9 +410,7 @@ public function hasProperty(string $property) : bool
*/
public function getRelations() : array
{
$class = get_class($this);

return $this->getModelsManager()->getRelations($class);
return $this->getModelsManager()->getRelations(get_class($this));
}

/*
Expand Down
104 changes: 94 additions & 10 deletions src/Elasticsearch/Objects/Documents.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

namespace Baka\Elasticsearch\Objects;

use Baka\Contracts\Database\ElasticModelInterface;
use Baka\Elasticsearch\Client;
use Baka\Elasticsearch\Query;
use function Baka\getShortClassName;

abstract class Documents
abstract class Documents implements ElasticModelInterface
{
public int $id;
public array $data;
Expand All @@ -19,17 +20,103 @@ abstract class Documents
protected array $dateNormal = ['date', 'yyyy-MM-dd'];
protected array $dateTime = ['date', 'yyyy-MM-dd HH:mm:ss'];
protected string $decimal = 'float';
protected array $relations = [];

/**
* Constructor.
* __construct.
*
* @param int $id
* @param array $data
* @param $argv
*
* @return void
*/
public function __construct($argv = null)
{
if (is_array($argv)) {
$this->assign($argv);
}
$this->initialize();
}

/**
* initialize.
*
* @return void
*/
public function initialize() : void
{
}

/**
* assign.
*
* @param array $data
*
* @return self
*/
public function assign(array $data) : self
{
foreach ($data as $key => $value) {
$this->{$key} = $value;
}
return $this;
}

/**
* addRelation.
*
* @param string $index
* @param array $options
*
* @return void
*/
protected function addRelation(string $index, array $options) : void
{
$this->relations[] = new Relation($index, $options);
}

/**
* useRawElasticRawData.
*
* @return bool
*/
public function useRawElasticRawData() : bool
{
return false;
}

/**
* getSource.
*
* @return string
*/
public function getSource() : string
{
return $this->getIndices();
}

/**
* getRelations.
*
* @return array
*/
public function getRelations() : array
{
return $this->relations;
}

/**
* setData.
*
* @param int $id
* @param array $data
*
* @return void
*/
public function __construct(int $id, array $data)
public function setData(int $id, array $data) : self
{
$this->id = $id;
$this->data = $data;
return $this;
}

/**
Expand Down Expand Up @@ -132,16 +219,13 @@ public function delete() : array
public static function getById(int $id) : self
{
$params = [
'index' => (new static($id, []))->getIndices(),
'index' => (new static())->getIndices(),
'id' => $id
];

$response = Client::getInstance()->get($params);

return new static(
$id,
$response['_source']
);
return (new static())->setData($id, $response['_source']);
}

/**
Expand Down
35 changes: 35 additions & 0 deletions src/Elasticsearch/Objects/Relation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Baka\Elasticsearch\Objects;

class Relation
{
protected array $options;
protected string $name;

/**
* __construct.
*
* @param string $index
* @param array $options
*
* @return void
*/
public function __construct(string $index, array $options)
{
$this->name = $index;
$this->options = $options;
}

/**
* getOptions.
*
* @return array
*/
public function getOptions() : array
{
return $this->options;
}
}
1 change: 1 addition & 0 deletions src/Elasticsearch/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function find() : array
$payload = [
$this->getPostKey() => trim($this->sql)
];

curl_setopt($ch, CURLOPT_URL, $this->getHost() . $this->getDriverUrl());
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
Expand Down
1 change: 0 additions & 1 deletion src/Elasticsearch/Query/FromClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public function get() : array
$queryNodes = [null]; //add 1 element to force , at the start
$searchNodes = [];
$replaceNodes = [];

/**
* @todo cache the relationships
*/
Expand Down
17 changes: 9 additions & 8 deletions src/Mail/Jobs/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,26 @@
use Baka\Mail\Message;
use Phalcon\Di;
use Swift_Mailer;
use Swift_Message;
use Swift_SmtpTransport;

class Mail extends Job implements QueueableJobInterface
{
protected object $config;
protected array $options;
protected Message $message;
protected Swift_Message $message;

/**
* Setup the mail job construct.
*
* @param Message $message
* @param Swift_Message $message
* @param array $options
*/
public function __construct(Message $message, array $options = [])
public function __construct(Swift_Message $message, array $options = [])
{
$this->message = $message;
$this->config = $message->getManager()->getConfigure();
$this->options = $options;
$this->config = $options['config'];
$this->options = $options['options'];
}

/**
Expand All @@ -46,12 +47,12 @@ public function handle()
$username = $auth['username'];
$password = $auth['password'];

//ovewrite host
//overwrite host
if (array_key_exists('host', $auth)) {
$host = $auth['host'];
}

//ovewrite port
//overwrite port
if (array_key_exists('port', $auth)) {
$port = $auth['port'];
}
Expand All @@ -65,7 +66,7 @@ public function handle()

$swift = Swift_Mailer::newInstance($transport);
$failures = [];
if ($swift->send($this->message->getMessage(), $failures)) {
if ($swift->send($this->message, $failures)) {
Di::getDefault()->get('log')->info('Email successfully sent to', [$this->message->getTo()]);
} else {
Di::getDefault()->get('log')->error('Email error sending ', [$failures]);
Expand Down
8 changes: 7 additions & 1 deletion src/Mail/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,13 @@ public function send()
$this->failedRecipients = [];
$options = $this->auth ? ['auth' => $this->smtp] : [];

Mail::dispatch($this, $options);
Mail::dispatch(
$this->getMessage(),
[
'options' => $options,
'config' => $this->getManager()->getConfigure()
]
);
}

/**
Expand Down
58 changes: 17 additions & 41 deletions tests/_support/ElasticModel/Vehicle.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,22 @@
namespace Baka\Test\Support\ElasticModel;

use Baka\Elasticsearch\Objects\Documents;
use stdClass;

class Vehicle extends Documents
{
/**
* Index data.
* initialize.
*
* @return stdClass
* @return void
*/
/* public function data() : stdClass
public function initialize() : void
{
$object = new stdClass();
$object->id = 1;
$this->setId($object->id);
$object->description = 'tetada';
$object->date = '2018-01-01';
$object->money = 10.1;
$object->anotherMoney = 10.1;
$photos[] = [
'name' => 'test',
'url' => 'http://mctekk.com',
'vehicles' => [
'id' => 2,
'date' => '2018-01-02',
'name' => 'wtf',
]
];
$photos[] = [
'name' => 'test',
'url' => 'http://mctekk.com',
'vehicles' => [[
'id' => 2,
'date' => '2018-01-02',
'name' => 'wtf', ], [
'id' => 2,
'date' => '2018-01-02',
'name' => 'wtf',
]
]
];
$object->photo = $photos;
return $object;
} */
$this->setIndices('vehicles');
$this->addRelation('photos', ['alias' => 'photos', 'elasticAlias' => 'ph', 'elasticIndex' => 1]);
$this->addRelation('rooftop', ['alias' => 'rooftop', 'elasticAlias' => 'rp', 'elasticIndex' => 1]);
$this->addRelation('selling_price', ['alias' => 'selling_price', 'elasticAlias' => 'sp', 'elasticIndex' => 1]);
$this->addRelation('issues', ['alias' => 'issues', 'elasticAlias' => 'iss', 'elasticIndex' => 1]);
}

/**
* Define the structure of this index.
Expand Down Expand Up @@ -91,6 +59,14 @@ public function structure() : array
'name' => $this->text
]
]
],
'selling_price' => [
'price' => [
'value' => $this->integer,
],
],
'issues' => [
'key' => $this->text
]
];
}
Expand Down
Loading

0 comments on commit fae930d

Please sign in to comment.