Completed
Push — master ( 79d36b...3588f7 )
by Eric
66:08 queued 03:30
created

Base64FileTrait::copyStringToStream()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 8
Ratio 53.33 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 8
loc 15
ccs 11
cts 11
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
nop 2
crap 2
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
        $fromPosition = ftell($from);
127 465
        $toPosition = ftell($to);
128 465
        rewind($from);
129 465
        $success = @stream_copy_to_stream($from, $to);
130 465
        fseek($from, $fromPosition);
131 465
        fseek($to, $toPosition);
132
133 465 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...
134 30
            $error = error_get_last();
135
136 30
            throw new \RuntimeException(sprintf(
137 30
                'An error occurred while copying the value (%s).',
138 30
                $error['message']
139 28
            ));
140
        }
141 435
    }
142
}
143