<?php

namespace App\Classes\Modules\Transactions\Processors;

use App\Classes\Modules\Transactions\Services\ListsTransactions;
use App\Classes\Modules\Jobs\Processors\UpdateJobResultProcessor;
use App\Classes\General\Helper;
use App\Classes\Modules\Jobs\DataTransferObjects\ListGenericJobObject;
use App\Http\Resources\ListTransactionJobResource;

class ListTransactionsJobProcessor
{

    /** @var ListsTransactions */
    private $listsTransactions;

    /** @var UpdateJobResultProcessor */
    private $updateJobResultProcessor;

    /**
     * ListTransactionsJobProcessor constructor.
     * @param ListsTransactions $listsTransactions
     * @param UpdateJobResultProcessor $updateJobResultProcessor
     */
    public function __construct(ListsTransactions $listsTransactions, UpdateJobResultProcessor $updateJobResultProcessor)
    {
        $this->listsTransactions = $listsTransactions;
        $this->updateJobResultProcessor = $updateJobResultProcessor;
    }

    /**
     * @param ListGenericJobObject $listGenericJobObject
     * @return void
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\JobResourceNotFoundException
     */
    public function execute(ListGenericJobObject $listGenericJobObject) {

        $query = $this->listsTransactions->execute($this->listsTransactions->deserializeFilters($listGenericJobObject->getPayload()['filters']), ['page' => $listGenericJobObject->getPayload()['page']]);

        $resultCurrent = Helper::collectionResponse(ListTransactionJobResource::collection($query));
        $this->updateJobResultProcessor->execute($listGenericJobObject, $resultCurrent);
    }
}

