Passed
Push — main ( 22aa22...c7f4b0 )
by Stefano
01:06
created

IndexFileService::run()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 16
rs 9.9666
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * Chatlas BEdita plugin
6
 *
7
 * Copyright 2023 Atlas Srl
8
 */
9
namespace BEdita\Chatlas\Job\Service;
10
11
use BEdita\Chatlas\Index\CollectionHandler;
12
use BEdita\Core\Job\JobService;
13
use Cake\Log\LogTrait;
14
use Cake\ORM\Locator\LocatorAwareTrait;
15
use Throwable;
16
17
class IndexFileService implements JobService
18
{
19
    use LocatorAwareTrait;
20
    use LogTrait;
21
22
    /**
23
     * Run an async job using $payload input data and optional $options.
24
     *
25
     * It can return:
26
     * - a boolean i.e. `true` on success, `false` on failure
27
     * - an array with keys:
28
     *   - 'success' (required) => `true` on success, `false` on failure
29
     *   - 'messages' (optional) => array of messages
30
     *
31
     * @param array $payload Input data for running this job.
32
     * @param array $options Options for running this job.
33
     * @return bool
34
     */
35
    public function run(array $payload, array $options = []): bool
36
    {
37
        $handler = new CollectionHandler();
38
        try {
39
            /** @var \BEdita\Core\Model\Entity\ObjectEntity $collection */
40
            $collection = $this->fetchTable('Collections')->get($payload['collection_id']);
41
            /** @var \BEdita\Core\Model\Entity\ObjectEntity $file */
42
            $file = $this->fetchTable('Files')->get($payload['file_id']);
43
            $handler->uploadDocument($collection, $file);
44
45
            return true;
46
        } catch (Throwable $th) {
47
            $this->log(sprintf('IndexFile async job error - %s', $th->getMessage()), 'error');
48
        }
49
50
        return false;
51
    }
52
}
53