StringResource::getTransport()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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