Completed
Pull Request — master (#141)
by Raffael
11:16
created

CleanTempStorage   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 60
ccs 0
cts 23
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A start() 0 22 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2018 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Balloon\Async;
13
14
use MongoDB\BSON\UTCDateTime;
15
use MongoDB\Database;
16
use Psr\Log\LoggerInterface;
17
use TaskScheduler\AbstractJob;
18
19
class CleanTempStorage extends AbstractJob
20
{
21
    /**
22
     * Databse.
23
     *
24
     * @var Database
25
     */
26
    protected $db;
27
28
    /**
29
     * Logger.
30
     *
31
     * @var LoggerInterface
32
     */
33
    protected $logger;
34
35
    /**
36
     * Default data.
37
     *
38
     * @var array
39
     */
40
    protected $data = [
41
        'max_age' => 172800,
42
    ];
43
44
    /**
45
     * Constructor.
46
     */
47
    public function __construct(Database $db, LoggerInterface $logger)
48
    {
49
        $this->db = $db;
50
        $this->logger = $logger;
51
    }
52
53
    /**
54
     * Start.
55
     */
56
    public function start(): bool
57
    {
58
        $lt = (time() - $this->data['max_age']) * 1000;
59
        $result = $this->db->selectCollection('fs.files')->find([
60
            'uploadDate' => ['$lt' => new UTCDateTime($lt)],
61
            'metadata.temporary' => true,
62
        ]);
63
64
        $count = 0;
65
        $gridfs = $this->db->selectGridFSBucket();
66
67
        foreach ($result as $blob) {
68
            $gridfs->delete($blob['_id']);
69
            ++$count;
70
        }
71
72
        $this->logger->info('found ['.$count.'] temporary storage blobs for cleanup', [
73
            'category' => get_class($this),
74
        ]);
75
76
        return true;
77
    }
78
}
79