FileResource   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 3
cbo 0
dl 0
loc 161
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A __construct() 0 7 1
A getConfiguration() 0 4 1
A setConfiguration() 0 6 1
A getResource() 0 4 1
A setResource() 0 6 1
A getAlias() 0 4 1
A setAlias() 0 9 2
A hasAlias() 0 4 2
A isRestorable() 0 4 2
A setRestorable() 0 6 1
A exists() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the ConfigCacheBundle package.
5
 *
6
 * Copyright (c) 2015-2016 Yahoo Japan Corporation
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace YahooJapan\ConfigCacheBundle\ConfigCache\Resource;
13
14
use Symfony\Component\Config\Definition\ConfigurationInterface;
15
16
/**
17
 * FileResource represents a resource stored on the filesystem.
18
 *
19
 * The resource can be a file or a directory.
20
 */
21
class FileResource implements ResourceInterface
22
{
23
    protected $resource;
24
    protected $configuration;
25
    protected $alias;
26
    protected $restore;
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param string                 $resource
32
     * @param ConfigurationInterface $configuration
33
     * @param string                 $alias
34
     * @param bool                   $restore
35
     *
36
     * @deprecated The third and fourth arguments are deprecated since version 1.2 and will be removed in 2.0.
37
     */
38
    public function __construct($resource, ConfigurationInterface $configuration = null, $alias = null, $restore = false)
39
    {
40
        $this->resource      = $resource;
41
        $this->configuration = $configuration;
42
        $this->alias         = $alias;
43
        $this->restore       = $restore;
44
    }
45
46
    /**
47
     * Creates a FileResource.
48
     *
49
     * @param string                 $resource
50
     * @param ConfigurationInterface $configuration
51
     * @param string                 $alias
52
     * @param bool                   $restore
53
     *
54
     * @deprecated The third and fourth arguments are deprecated since version 1.2 and will be removed in 2.0.
55
     *
56
     * @return FileResource
57
     */
58
    public static function create($resource, ConfigurationInterface $configuration = null, $alias = null, $restore = false)
59
    {
60
        return new static($resource, $configuration, $alias, $restore);
61
    }
62
63
    /**
64
     * Gets configuration.
65
     *
66
     * @return ConfigurationInterface
67
     */
68 44
    public function getConfiguration()
69
    {
70 44
        return $this->configuration;
71
    }
72
73
    /**
74
     * Sets configuration.
75
     *
76
     * @param ConfigurationInterface $configuration
77
     *
78
     * @return FileResource
79
     */
80 1
    public function setConfiguration(ConfigurationInterface $configuration)
81
    {
82 1
        $this->configuration = $configuration;
83
84 1
        return $this;
85
    }
86
87
    /**
88
     * Returns the resource tied to this Resource.
89
     *
90
     * @return mixed The resource
91
     */
92 49
    public function getResource()
93
    {
94 49
        return $this->resource;
95
    }
96
97
    /**
98
     * Sets the resource.
99
     *
100
     * @param string $resource
101
     *
102
     * @return FileResource
103
     */
104 1
    public function setResource($resource)
105
    {
106 1
        $this->resource = $resource;
107
108 1
        return $this;
109
    }
110
111
    /**
112
     * Gets an alias (as ConfigCache service name suffix).
113
     *
114
     * @return string
115
     */
116 18
    public function getAlias()
117
    {
118 18
        return $this->alias;
119
    }
120
121
    /**
122
     * Sets an alias (as ConfigCache service name suffix).
123
     *
124
     * @param string $alias
125
     *
126
     * @return FileResource
127
     *
128
     * @throws \InvalidArgumentException
129
     */
130 6
    public function setAlias($alias)
131
    {
132 6
        if (!is_string($alias)) {
133 2
            throw new \InvalidArgumentException("Alias[{$alias}] must be string.");
134
        }
135 4
        $this->alias = $alias;
136
137 4
        return $this;
138
    }
139
140
    /**
141
     * Whether FileResource has an alias or not.
142
     *
143
     * @return bool
144
     */
145 32
    public function hasAlias()
146
    {
147 32
        return !is_null($this->alias) && $this->alias !== '';
148
    }
149
150
    /**
151
     * Whether this resources is restorable or not.
152
     *
153
     * @return bool
154
     */
155 16
    public function isRestorable()
156
    {
157 16
        return $this->restore ? true : false;
158
    }
159
160
    /**
161
     * Sets restorable.
162
     *
163
     * @param bool $restore
164
     *
165
     * @return FileResource
166
     */
167 1
    public function setRestorable($restore)
168
    {
169 1
        $this->restore = $restore;
170
171 1
        return $this;
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177 26
    public function exists()
178
    {
179 26
        return file_exists($this->resource);
180
    }
181
}
182