RFFRED
2 months ago
215
1
Laravel

delegate tasks to a service

Posted 2 months ago by RFFRED

Hi,

I am trying to refactor some of my code and remove functions I had in a model into some other class. I am not sure if we call that a helper class or a service....

Template model

class Template extends Model
{

    protected $fillable = [
        'name', 'display'
    ];


    public function update_modules_attached($attributes)
    {
	//removes all modules from the pivot table for this template
        $this->remove_all_modules_attached();

	$this->attach_modules();

    	//do lots of extra stuff
        
    }

    public function remove_all_modules_attached()
    {
        $this->template->modules()->detach();
    }

    public function attach_modules()
    {
    	//do lots of extra stuff
    }

    //creates pivot table relationsip
    public function modules(){
        return $this->belongsToMany('RF\RFTemplates\Models\Admin\TemplateModule');
    }
}

Controller

class TemplateController extends Controller
{

    /**
     * Create a new controller instance.
     * 
     * @return void
     */
    public function __construct()
    {
    }


    public function create()
    {

        $template = new Template;

        //selects all the modules available in the system
        $modules = DB::table('modules')->get();

        //initialises the modules attached to the template
        $used_modules = [];

        return view($this->views_path.'templates.create', compact('template', 'modules', 'used_modules'));
    }


    public function store(TemplateStoreRequest $request)
    {

        // Will return only validated data
        $validatedData = $request->validated();
 
        //create record
        $template = Template::create($validatedData);

        //updates the modules attached to the template
        $template->update_modules_attached(['modules_list'=>$request->input('modules-list')]);

        return redirect()->route('admin.templates.index')
                         ->with('success', trans($this->lang_path.'create_templates.success') );

    }

I would like to move the functions not really related to the model such as: "update_modules_attached", attach_modules() from the model to another class. There are a few other functions not listed here making the model class very big and that should really be moved

So I created a new class.

class TemplateManagerService
{

	protected $template;


    /**
     * Create a new instance.
     * 
     * @return void
     */
    public function __construct(Template $template)
    {

    	$this->template = $template;

    }

    public function remove_all_modules_attached()
    {

        $this->template->modules()->detach();

    }

    /**
     * Update the template's modules in the pivot table
     *
     * @param  Array  $attributes
     * @return 
     */
    function update_modules_attached($attributes)
    {

	//removes all modules from the pivot table for this template
        $this->template->remove_all_modules_attached();

     }

I have updated my controller to accept the service


class TemplateController extends Controller
{

    private $templateManagerService;

    /**
     * Create a new controller instance.
     * 
     * @return void
     */
    public function __construct(TemplateManagerService $templateManagerService)
    {

        $this->templateManagerService = $templateManagerService;

    }

    public function create(){
	    //does not change
    }

    public function store(TemplateStoreRequest $request)
    {

        // Will return only validated data
        $validatedData = $request->validated();
 
        //create record
        $template = Template::create($validatedData);

        $this->templateManagerService->remove_all_modules_attached();

        return redirect()->route('admin.templates.index')
                         ->with('success', trans($this->lang_path.'create_templates.success') );

    }

What I do not understand is how to pass my newly created instance of $template to the service.

when I run from the controller,

$this->templateManagerService->remove_all_modules_attached();

How does my service know which instance of template it needs to work with??

Please sign in or create an account to participate in this conversation.