Completed
Push — master ( 0f7d17...393ba8 )
by Adam
02:40
created

File::toVOString()   A

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
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php namespace BestServedCold\PhalueObjects;
2
3
use BestServedCold\PhalueObjects\Format\Json;
4
use BestServedCold\PhalueObjects\Format\Xml;
5
use BestServedCold\PhalueObjects\Format\Yaml;
6
7
/**
8
 * Class File
9
 *
10
 * @package BestServedCold\PhalueObjects
11
 */
12
class File extends ValueObject
13
{
14
    /**
15
     * @var int
16
     */
17
    protected $timeout;
18
19
    /**
20
     * @var bool
21
     */
22
    protected $mustExist;
23
24
    /**
25
     * File constructor.
26
     * @param array $value
27
     * @param bool $mustExist
28
     * @param int $timeout
29
     */
30 10
    public function __construct($value, $mustExist = true, $timeout = 10)
31
    {
32 10
        parent::__construct($value);
33 10
        $this->timeout = $timeout;
34 10
        $this->mustExist = $mustExist;
35 10
    }
36
37
    /**
38
     * @return bool
39
     */
40 6
    public function exists()
0 ignored issues
show
Coding Style introduced by
function exists() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
41
    {
42 6
        return file_exists($this->getValue());
43
    }
44
45
    /**
46
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
47
     */
48 5
    public function getContents()
49
    {
50 5
        return !$this->mustExist || $this->exists()
51 5
            ? file_get_contents($this->getValue())
52 5
            : false;
53
    }
54
55
    /**
56
     * @return string
57
     */
58 1
    public function getExtension()
59
    {
60 1
        return pathinfo($this->getValue(), PATHINFO_EXTENSION);
61
    }
62
63
    /**
64
     * @return string
65
     */
66 1
    public function getDirectoryName()
67
    {
68 1
        return pathinfo($this->getValue(), PATHINFO_DIRNAME);
69
    }
70
71
    /**
72
     * @return string
73
     */
74 1
    public function getFileName()
75
    {
76 1
        return pathinfo($this->getValue(), PATHINFO_FILENAME);
77
    }
78
79
    /**
80
     * @return VOString
81
     */
82 1
    public function toVOString()
83
    {
84 1
        return new VOString($this->getContents());
85
    }
86
87
    /**
88
     * @return Xml
89
     */
90 1
    public function toXml()
91
    {
92 1
        return Xml::fromString($this->getContents());
93
    }
94
95
    /**
96
     * @return Json
97
     */
98 1
    public function toJson()
99
    {
100 1
        return Json::fromString($this->getContents());
101
    }
102
103
    /**
104
     * @return Yaml
105
     */
106 1
    public function toYaml()
107
    {
108 1
        return Yaml::fromString($this->getContents());
109
    }
110
}
111