Completed
Push — master ( 91fa6a...3f50ad )
by Eric
104:33 queued 39:31
created

Base64FileTrait::copyStreamToStream()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 29
Code Lines 16

Duplication

Lines 8
Ratio 27.59 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 8
loc 29
ccs 14
cts 14
cp 1
rs 8.5806
cc 4
eloc 16
nc 8
nop 2
crap 4
1
<?php
2
3
/*
4
 * This file is part of the Ivory Base64 File package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\Base64FileBundle\Model;
13
14
/**
15
 * @author GeLo <[email protected]>
16
 */
17
trait Base64FileTrait
18
{
19
    /**
20
     * @var resource
21
     */
22
    private $resource;
23
24
    /**
25
     * @param string|resource $value
26
     * @param bool            $encoded
27
     */
28 540
    public function __construct($value, $encoded = true)
29
    {
30 540
        $this->resource = tmpfile();
31
32 540
        if ($encoded) {
33 420
            $filter = stream_filter_append($this->resource, 'convert.base64-decode', STREAM_FILTER_WRITE);
34 392
        }
35
36
        try {
37 540
            if (is_string($value)) {
38 270
                $this->copyStringToStream($value, $this->resource);
39 480
            } elseif (is_resource($value)) {
40 240
                $this->copyStreamToStream($value, $this->resource);
41 196
            } else {
42 30
                throw new \InvalidArgumentException(sprintf(
43 59
                    'The base64 file value must be a string or a resource, got "%s".',
44 28
                    gettype($value)
45 28
                ));
46
            }
47 511
        } catch (\Exception $e) {
48 105
            fclose($this->resource);
49
50 105
            throw $e;
51
        }
52
53 435
        if (isset($filter)) {
54 315
            stream_filter_remove($filter);
55 294
        }
56
57 435
        fflush($this->resource);
58 435
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 405
    public function __destruct()
64
    {
65 405
        if (is_resource($this->resource)) {
66 405
            fclose($this->resource);
67 378
        }
68 405
    }
69
70
    /**
71
     * @param bool $encoded
72
     * @param bool $asResource
73
     *
74
     * @return resource|string
75
     */
76 405
    public function getData($encoded = true, $asResource = true)
77
    {
78 405
        $resource = fopen('php://temp', 'rb+');
79
80 405
        if ($encoded) {
81 225
            $filter = stream_filter_append($resource, 'convert.base64-encode', STREAM_FILTER_WRITE);
82 210
        }
83
84 405
        $this->copyStreamToStream($this->resource, $resource);
85
86 405
        if (isset($filter)) {
87 225
            stream_filter_remove($filter);
88 210
        }
89
90 405
        if ($asResource) {
91 180
            return $resource;
92
        }
93
94 225
        $content = stream_get_contents($resource);
95 225
        fclose($resource);
96
97 225
        return $content;
98
    }
99
100
    /**
101
     * @param string   $from
102
     * @param resource $to
103
     */
104 270
    private function copyStringToStream($from, $to)
105
    {
106 270
        $toPosition = ftell($to);
107 270
        $success = @fwrite($to, $from);
108 270
        fseek($to, $toPosition);
109
110 270 View Code Duplication
        if (!$success) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111 45
            $error = error_get_last();
112
113 45
            throw new \RuntimeException(sprintf(
114 45
                'An error occurred while copying the value (%s).',
115 45
                $error['message']
116 42
            ));
117
        }
118 225
    }
119
120
    /**
121
     * @param resource $from
122
     * @param resource $to
123
     */
124 465
    private function copyStreamToStream($from, $to)
125
    {
126 465
        $metadata = stream_get_meta_data($from);
127 465
        $seekable = $metadata['seekable'];
128 465
129 465
        $toPosition = ftell($to);
130 465
131 465
        if ($seekable) {
132
            $fromPosition = ftell($from);
133 465
            rewind($from);
134 30
        }
135
136 30
        $success = @stream_copy_to_stream($from, $to);
137 30
138 30
        if (isset($fromPosition)) {
139 28
            fseek($from, $fromPosition);
140
        }
141 435
142
        fseek($to, $toPosition);
143
144 View Code Duplication
        if (!$success) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
145
            $error = error_get_last();
146
147
            throw new \RuntimeException(sprintf(
148
                'An error occurred while copying the value (%s).',
149
                $error['message']
150
            ));
151
        }
152
    }
153
}
154