1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 00:41:34 +02:00
invoiceninja/app/Ninja/Repositories/BaseRepository.php

117 lines
2.2 KiB
PHP
Raw Normal View History

2015-10-28 20:22:07 +01:00
<?php namespace App\Ninja\Repositories;
/**
* Class BaseRepository
*/
2015-10-28 20:22:07 +01:00
class BaseRepository
{
/**
* @return null
*/
2015-10-28 20:22:07 +01:00
public function getClassName()
{
return null;
}
/**
* @return mixed
*/
2015-10-28 20:22:07 +01:00
private function getInstance()
{
$className = $this->getClassName();
return new $className();
}
/**
* @param $entity
* @param $type
* @return string
*/
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
}
/**
* @param $entity
*/
2015-10-28 20:22:07 +01:00
public function archive($entity)
{
2016-03-23 14:02:05 +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
}
/**
* @param $entity
*/
2015-10-28 20:22:07 +01:00
public function restore($entity)
{
2016-03-23 14:02:05 +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
}
/**
* @param $entity
*/
2015-10-28 20:22:07 +01:00
public function delete($entity)
{
2016-03-23 14:02:05 +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
}
/**
* @param $ids
* @return mixed
*/
2015-10-28 20:22:07 +01:00
public function findByPublicIds($ids)
{
return $this->getInstance()->scope($ids)->get();
}
/**
* @param $ids
* @return mixed
*/
2015-10-28 20:22:07 +01:00
public function findByPublicIdsWithTrashed($ids)
{
return $this->getInstance()->scope($ids)->withTrashed()->get();
}
}