1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 21:52:35 +01:00
invoiceninja/app/Ninja/Repositories/BaseRepository.php

86 lines
1.8 KiB
PHP
Raw Normal View History

2015-10-28 20:22:07 +01:00
<?php namespace App\Ninja\Repositories;
class BaseRepository
{
public function getClassName()
{
return null;
}
private function getInstance()
{
$className = $this->getClassName();
return new $className();
}
2015-10-29 23:27:26 +01:00
private function getEventClass($entity, $type)
2015-10-28 20:22:07 +01:00
{
2015-10-29 23:27:26 +01:00
return 'App\Events\\' . ucfirst($entity->getEntityType()) . 'Was' . $type;
2015-10-28 20:22:07 +01:00
}
public function archive($entity)
{
2016-03-23 14:02:53 +01:00
if ($entity->trashed()) {
return;
}
2015-10-28 20:22:07 +01:00
$entity->delete();
2015-10-29 23:27:26 +01:00
$className = $this->getEventClass($entity, 'Archived');
2015-11-05 23:37:04 +01:00
if (class_exists($className)) {
event(new $className($entity));
}
2015-10-28 20:22:07 +01:00
}
public function restore($entity)
{
2016-03-23 14:02:53 +01:00
if ( ! $entity->trashed()) {
return;
}
2015-10-29 23:27:26 +01:00
$fromDeleted = false;
2015-10-28 20:22:07 +01:00
$entity->restore();
2015-10-29 23:27:26 +01:00
if ($entity->is_deleted) {
$fromDeleted = true;
$entity->is_deleted = false;
$entity->save();
}
2015-10-28 20:22:07 +01:00
2015-10-29 23:27:26 +01:00
$className = $this->getEventClass($entity, 'Restored');
2015-11-05 23:37:04 +01:00
if (class_exists($className)) {
event(new $className($entity, $fromDeleted));
}
2015-10-28 20:22:07 +01:00
}
public function delete($entity)
{
2016-03-23 14:02:53 +01:00
if ($entity->is_deleted) {
return;
}
2015-10-28 20:22:07 +01:00
$entity->is_deleted = true;
$entity->save();
$entity->delete();
2015-10-29 23:27:26 +01:00
$className = $this->getEventClass($entity, 'Deleted');
2015-11-05 23:37:04 +01:00
if (class_exists($className)) {
event(new $className($entity));
}
2015-10-28 20:22:07 +01:00
}
public function findByPublicIds($ids)
{
return $this->getInstance()->scope($ids)->get();
}
public function findByPublicIdsWithTrashed($ids)
{
return $this->getInstance()->scope($ids)->withTrashed()->get();
}
}