Skip to content

Commit

Permalink
Add additional events (andersao#709)
Browse files Browse the repository at this point in the history
* Add additional events

Signed-off-by: Lloric Mayuga Garcia <[email protected]>

* docs

Signed-off-by: Lloric Mayuga Garcia <[email protected]>
  • Loading branch information
lloricode authored May 14, 2020
1 parent 24c473c commit 098b5b2
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/Prettus/Repository/Eloquent/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
use Prettus\Repository\Contracts\RepositoryCriteriaInterface;
use Prettus\Repository\Contracts\RepositoryInterface;
use Prettus\Repository\Events\RepositoryEntityCreated;
use Prettus\Repository\Events\RepositoryEntityCreating;
use Prettus\Repository\Events\RepositoryEntityDeleted;
use Prettus\Repository\Events\RepositoryEntityDeleting;
use Prettus\Repository\Events\RepositoryEntityUpdated;
use Prettus\Repository\Events\RepositoryEntityUpdating;
use Prettus\Repository\Exceptions\RepositoryException;
use Prettus\Repository\Traits\ComparesVersionsTrait;
use Prettus\Validator\Contracts\ValidatorInterface;
Expand Down Expand Up @@ -628,6 +631,8 @@ public function create(array $attributes)
$this->validator->with($attributes)->passesOrFail(ValidatorInterface::RULE_CREATE);
}

event(new RepositoryEntityCreating($this, $attributes));

$model = $this->model->newInstance($attributes);
$model->save();
$this->resetModel();
Expand Down Expand Up @@ -671,6 +676,9 @@ public function update(array $attributes, $id)
$this->skipPresenter(true);

$model = $this->model->findOrFail($id);

event(new RepositoryEntityUpdating($this, $model));

$model->fill($attributes);
$model->save();

Expand Down Expand Up @@ -704,6 +712,8 @@ public function updateOrCreate(array $attributes, array $values = [])

$this->skipPresenter(true);

event(new RepositoryEntityCreating($this, $attributes));

$model = $this->model->updateOrCreate($attributes, $values);

$this->skipPresenter($temporarySkipPresenter);
Expand Down Expand Up @@ -734,6 +744,8 @@ public function delete($id)
$this->skipPresenter($temporarySkipPresenter);
$this->resetModel();

event(new RepositoryEntityDeleting($this, $model));

$deleted = $model->delete();

event(new RepositoryEntityDeleted($this, $originalModel));
Expand All @@ -757,6 +769,8 @@ public function deleteWhere(array $where)

$this->applyConditions($where);

event(new RepositoryEntityDeleting($this, $this->model));

$deleted = $this->model->delete();

event(new RepositoryEntityDeleted($this, $this->model->getModel()));
Expand Down
26 changes: 26 additions & 0 deletions src/Prettus/Repository/Events/RepositoryEntityCreating.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Prettus\Repository\Events;

use Illuminate\Database\Eloquent\Model;
use Prettus\Repository\Contracts\RepositoryInterface;

/**
* Class RepositoryEntityCreated
*
* @package Prettus\Repository\Events
* @author Anderson Andrade <[email protected]>
*/
class RepositoryEntityCreating extends RepositoryEventBase
{
/**
* @var string
*/
protected $action = "creating";

public function __construct(RepositoryInterface $repository, array $model)
{
parent::__construct($repository);
$this->model = $model;
}
}
15 changes: 15 additions & 0 deletions src/Prettus/Repository/Events/RepositoryEntityDeleting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace Prettus\Repository\Events;

/**
* Class RepositoryEntityDeleted
* @package Prettus\Repository\Events
* @author Anderson Andrade <[email protected]>
*/
class RepositoryEntityDeleting extends RepositoryEventBase
{
/**
* @var string
*/
protected $action = "deleting";
}
15 changes: 15 additions & 0 deletions src/Prettus/Repository/Events/RepositoryEntityUpdating.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace Prettus\Repository\Events;

/**
* Class RepositoryEntityUpdated
* @package Prettus\Repository\Events
* @author Anderson Andrade <[email protected]>
*/
class RepositoryEntityUpdating extends RepositoryEventBase
{
/**
* @var string
*/
protected $action = "updating";
}
4 changes: 2 additions & 2 deletions src/Prettus/Repository/Events/RepositoryEventBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ abstract class RepositoryEventBase
* @param RepositoryInterface $repository
* @param Model $model
*/
public function __construct(RepositoryInterface $repository, Model $model)
public function __construct(RepositoryInterface $repository, Model $model = null)
{
$this->repository = $repository;
$this->model = $model;
}

/**
* @return Model
* @return Model|array
*/
public function getModel()
{
Expand Down

0 comments on commit 098b5b2

Please sign in to comment.