StringResource::getFile()   A
last analyzed

Complexity

Conditions 3
Paths 5

Size

Total Lines 14
Code Lines 9

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 0
Metric Value
dl 14
loc 14
ccs 8
cts 9
cp 0.8889
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 5
nop 0
crap 3.0123
1
<?php
2
3
namespace TreeHouse\Feeder\Resource;
4
5
use TreeHouse\Feeder\Exception\TransportException;
6
7
class StringResource implements ResourceInterface
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $data;
13
14
    /**
15
     * @var \SplFileObject
16
     */
17
    protected $file;
18
19
    /**
20
     * @param string $data
21
     *
22
     * @throws \InvalidArgumentException When anything other than a string is passed
23
     */
24 142
    public function __construct($data)
25
    {
26 142
        if (!is_string($data)) {
27 8
            throw new \InvalidArgumentException('You must pass a string to a StringResource');
28
        }
29
30 134
        $this->data = $data;
31 134
    }
32
33
    /**
34
     * @inheritdoc
35
     */
36 30
    public function getTransport()
37
    {
38 30
        return null;
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44 2
    public function setFile(\SplFileObject $file)
45
    {
46 2
        $this->file = $file;
47 2
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52 100 View Code Duplication
    public function getFile()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
53
    {
54 100
        if ($this->file === null) {
55
            try {
56 98
                $this->file = new TempFile();
57 98
                $this->file->fwrite($this->data);
58 98
                $this->file->fseek(0);
59 98
            } catch (\RuntimeException $e) {
60
                throw new TransportException($e->getMessage(), null, $e);
61
            }
62 98
        }
63
64 100
        return $this->file;
65
    }
66
}
67