Passed
Push — master ( 3896ee...e50616 )
by Raffael
05:45
created

AbstractFile   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 55
ccs 13
cts 14
cp 0.9286
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A flush() 0 18 4
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * tubee.io
7
 *
8
 * @copyright   Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Tubee\Endpoint;
13
14
use Psr\Log\LoggerInterface;
15
use Tubee\Collection\CollectionInterface;
16
use Tubee\Storage\StorageInterface;
17
use Tubee\Workflow\Factory as WorkflowFactory;
18
19
abstract class AbstractFile extends AbstractEndpoint
20
{
21
    /**
22
     * File.
23
     *
24
     * @var string
25
     */
26
    protected $file;
27
28
    /**
29
     * Storage.
30
     *
31
     * @var StorageInterface
32
     */
33
    protected $storage;
34
35
    /**
36
     * Files.
37
     *
38
     * @var array
39
     */
40
    protected $files = [];
41
42
    /**
43
     * Init endpoint.
44
     */
45 24
    public function __construct(string $name, string $type, string $file, StorageInterface $storage, CollectionInterface $collection, WorkflowFactory $workflow, LoggerInterface $logger, ?Iterable $resource = [])
46
    {
47 24
        $this->storage = $storage;
48 24
        $this->file = $file;
49 24
        parent::__construct($name, $type, $collection, $workflow, $logger, $resource);
50 24
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 8
    public function flush(bool $simulate = false): bool
56
    {
57 8
        $this->logger->info('flush file ['.$this->file.'] from endpoint ['.$this->name.']', [
58 8
            'category' => get_class($this),
59
        ]);
60
61 8
        if ($simulate === true) {
62
            return true;
63
        }
64
65 8
        foreach ($this->files as $stream) {
66 8
            if (ftruncate($stream['stream'], 0) === false) {
67 8
                throw new Exception\WriteOperationFailed('failed flush file '.$this->file);
68
            }
69
        }
70
71 7
        return true;
72
    }
73
}
74