2014-07-05 16:19:15 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace Firefly\Storage\Component;
|
|
|
|
|
2014-07-25 13:02:01 +02:00
|
|
|
use Firefly\Exception\FireflyException;
|
|
|
|
use Illuminate\Database\QueryException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class EloquentComponentRepository
|
|
|
|
*
|
|
|
|
* @package Firefly\Storage\Component
|
|
|
|
*/
|
2014-07-05 16:19:15 +02:00
|
|
|
class EloquentComponentRepository implements ComponentRepositoryInterface
|
|
|
|
{
|
|
|
|
public $validator;
|
2014-09-02 17:27:28 +02:00
|
|
|
protected $_user = null;
|
2014-07-05 16:19:15 +02:00
|
|
|
|
2014-07-25 13:02:01 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2014-07-05 16:19:15 +02:00
|
|
|
public function __construct()
|
|
|
|
{
|
2014-09-02 17:27:28 +02:00
|
|
|
$this->_user = \Auth::user();
|
2014-07-05 16:19:15 +02:00
|
|
|
}
|
|
|
|
|
2014-07-25 13:02:01 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2014-07-05 16:19:15 +02:00
|
|
|
public function count()
|
|
|
|
{
|
2014-09-02 17:27:28 +02:00
|
|
|
return $this->_user->components()->count();
|
2014-07-05 16:19:15 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-25 13:02:01 +02:00
|
|
|
/**
|
|
|
|
* @return mixed|void
|
|
|
|
* @throws \Firefly\Exception\FireflyException
|
|
|
|
*/
|
2014-07-15 22:16:29 +02:00
|
|
|
public function get()
|
|
|
|
{
|
2014-07-25 13:02:01 +02:00
|
|
|
throw new FireflyException('No implementation.');
|
2014-07-15 06:58:08 +02:00
|
|
|
}
|
|
|
|
|
2014-09-02 17:27:28 +02:00
|
|
|
/**
|
|
|
|
* @param \User $user
|
|
|
|
* @return mixed|void
|
|
|
|
*/
|
|
|
|
public function overruleUser(\User $user)
|
|
|
|
{
|
|
|
|
$this->_user = $user;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-25 13:02:01 +02:00
|
|
|
/**
|
|
|
|
* @param $data
|
|
|
|
*
|
|
|
|
* @return \Budget|\Category|mixed
|
|
|
|
* @throws \Firefly\Exception\FireflyException
|
|
|
|
*/
|
2014-07-05 16:19:15 +02:00
|
|
|
public function store($data)
|
|
|
|
{
|
2014-07-05 19:44:26 +02:00
|
|
|
if (!isset($data['class'])) {
|
2014-07-25 13:02:01 +02:00
|
|
|
throw new FireflyException('No class type present.');
|
2014-07-05 19:44:26 +02:00
|
|
|
}
|
|
|
|
switch ($data['class']) {
|
2014-07-25 13:02:01 +02:00
|
|
|
default:
|
2014-07-05 19:44:26 +02:00
|
|
|
case 'Budget':
|
|
|
|
$component = new \Budget;
|
|
|
|
break;
|
|
|
|
case 'Category':
|
|
|
|
$component = new \Category;
|
|
|
|
break;
|
|
|
|
|
2014-07-05 16:19:15 +02:00
|
|
|
}
|
|
|
|
$component->name = $data['name'];
|
2014-09-02 17:27:28 +02:00
|
|
|
$component->user()->associate($this->_user);
|
2014-07-05 16:19:15 +02:00
|
|
|
try {
|
|
|
|
$component->save();
|
2014-07-25 13:02:01 +02:00
|
|
|
} catch (QueryException $e) {
|
2014-07-05 16:19:15 +02:00
|
|
|
\Log::error('DB ERROR: ' . $e->getMessage());
|
2014-07-25 13:02:01 +02:00
|
|
|
throw new FireflyException('Could not save component ' . $data['name'] . ' of type'
|
2014-07-05 19:44:26 +02:00
|
|
|
. $data['class']);
|
2014-07-05 16:19:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $component;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|