Completed
Push — master ( 30d679...ef761f )
by Adam
07:29
created

File   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 20%

Importance

Changes 8
Bugs 2 Features 0
Metric Value
wmc 12
c 8
b 2
f 0
lcom 1
cbo 4
dl 0
loc 80
ccs 5
cts 25
cp 0.2
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A exists() 0 4 1
A getContents() 0 6 3
A getExtension() 0 4 1
A getDirectoryName() 0 4 1
A getFileName() 0 4 1
A fromString() 0 4 1
A arrayFromXml() 0 4 1
A arrayFromJson() 0 4 1
A arrayFromYaml() 0 4 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
class File extends ValueObject
8
{
9
    protected $checkExists = true;
10
    protected $timeout;
11
    protected $mustExist;
12
13 1
    public function __construct($value, $mustExist = true, $timeout = 10)
14
    {
15 1
        parent::__construct($value);
16 1
        $this->timeout = $timeout;
17 1
        $this->mustExist = $mustExist;
18 1
    }
19
20
    /**
21
     * @return bool
22
     */
23
    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...
24
    {
25
        return file_exists($this->getValue());
26
    }
27
28
    /**
29
     * @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...
30
     */
31
    public function getContents()
32
    {
33
        return ! $this->mustExist || $this->exists()
34
            ? file_get_contents($this->getValue())
35
            : false;
36
    }
37
38
    /**
39
     * @return string
40
     */
41
    public function getExtension()
42
    {
43
        return pathinfo($this->getValue(), PATHINFO_EXTENSION);
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    public function getDirectoryName()
50
    {
51
        return pathinfo($this->getValue(), PATHINFO_DIRNAME);
52
    }
53
54
    /**
55
     * @return string
56
     */
57
    public function getFileName()
58
    {
59
        return pathinfo($this->getValue(), PATHINFO_FILENAME);
60
    }
61
62
    /**
63
     * @param string $string
64
     * @return static
65
     */
66
    public static function fromString($string)
67
    {
68
        return new static($string);
69
    }
70
71
72
    public function arrayFromXml()
73
    {
74
        return Xml::fromString($this->getContents())->parse();
75
    }
76
77
    public function arrayFromJson()
78
    {
79
        return Json::fromString($this->getContents())->parse();
80
    }
81
82
    public function arrayFromYaml()
83
    {
84
        return Yaml::fromString($this->getContents())->parse();
85
    }
86
}
87