Passed
Push — master ( ca068f...f2d295 )
by Alexey
03:12 queued 16s
created

ZipNewEntry   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 55
rs 10
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 4
A getEntryContent() 0 11 3
A __clone() 0 4 1
A __destruct() 0 5 4
1
<?php
2
3
namespace PhpZip\Model\Entry;
4
5
use PhpZip\Exception\InvalidArgumentException;
6
7
/**
8
 * @author Ne-Lexa [email protected]
9
 * @license MIT
10
 */
11
class ZipNewEntry extends ZipAbstractEntry
12
{
13
    /** @var resource|string|null */
14
    protected $content;
15
16
    /** @var bool */
17
    private $clone = false;
18
19
    /**
20
     * ZipNewEntry constructor.
21
     *
22
     * @param string|resource|null $content
23
     */
24
    public function __construct($content = null)
25
    {
26
        parent::__construct();
27
28
        if ($content !== null && !\is_string($content) && !\is_resource($content)) {
29
            throw new InvalidArgumentException('invalid content');
30
        }
31
        $this->content = $content;
32
    }
33
34
    /**
35
     * Returns an string content of the given entry.
36
     *
37
     * @return string|null
38
     */
39
    public function getEntryContent()
40
    {
41
        if (\is_resource($this->content)) {
42
            if (stream_get_meta_data($this->content)['seekable']) {
43
                rewind($this->content);
44
            }
45
46
            return stream_get_contents($this->content);
47
        }
48
49
        return $this->content;
50
    }
51
52
    /**
53
     * Clone extra fields.
54
     */
55
    public function __clone()
56
    {
57
        $this->clone = true;
58
        parent::__clone();
59
    }
60
61
    public function __destruct()
62
    {
63
        if (!$this->clone && $this->content !== null && \is_resource($this->content)) {
64
            fclose($this->content);
65
            $this->content = null;
66
        }
67
    }
68
}
69