Passed
Push — master ( e1f86a...4e1a3a )
by Siad
05:23
created

AttribTask::setSystem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19
20
namespace Phing\Tasks\System;
21
22
use Phing\Exception\BuildException;
23
use Phing\Io\File;
24
use Phing\Project;
25
use Phing\Type\FileSet;
26
27
/**
28
 * Changes the attributes of a file or all files inside specified directories.
29
 * Right now it has effect only under Windows. Each of the 4 possible
30
 * permissions has its own attribute, matching the arguments for the `attrib`
31
 * command.
32
 *
33
 * Example:
34
 * ```
35
 *    <attrib file="${input}" readonly="true" hidden="true" verbose="true"/>
36
 * ```
37
 *
38
 * @author  Siad Ardroumli <[email protected]>
39
 * @package phing.tasks.system
40
 */
41
class AttribTask extends ApplyTask
42
{
43
    private static $ATTR_READONLY = 'R';
44
    private static $ATTR_ARCHIVE = 'A';
45
    private static $ATTR_SYSTEM = 'S';
46
    private static $ATTR_HIDDEN = 'H';
47
    private static $SET = '+';
48
    private static $UNSET = '-';
49
50
    private $attr = false;
51
52
    public function init()
53
    {
54
        parent::init();
55
        parent::setExecutable('attrib');
56
        parent::setParallel(false);
57
    }
58
59
    /**
60
     * @throws BuildException
61
     */
62
    public function main()
63
    {
64
        $this->checkConfiguration();
65
        parent::main();
66
    }
67
68
    /**
69
     * @param bool $b
70
     */
71
    public function setVerbose($b)
0 ignored issues
show
Unused Code introduced by
The parameter $b is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

71
    public function setVerbose(/** @scrutinizer ignore-unused */ $b)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
72
    {
73
        $this->loglevel = Project::MSG_VERBOSE;
74
    }
75
76
    /**
77
     * A file to be attribed.
78
     *
79
     * @param File $src a file
80
     */
81
    public function setFile(File $src)
82
    {
83
        $fs = new FileSet();
84
        $fs->setFile($src);
85
        $this->addFileSet($fs);
86
    }
87
88
    /**
89
     * Set the ReadOnly file attribute.
90
     *
91
     * @param boolean $value
92
     */
93
    public function setReadonly($value)
94
    {
95
        $this->addArg($value, self::$ATTR_READONLY);
96
    }
97
98
    /**
99
     * Set the Archive file attribute.
100
     *
101
     * @param boolean $value
102
     */
103
    public function setArchive($value)
104
    {
105
        $this->addArg($value, self::$ATTR_ARCHIVE);
106
    }
107
108
    /**
109
     * Set the System file attribute.
110
     *
111
     * @param boolean $value
112
     */
113
    public function setSystem($value)
114
    {
115
        $this->addArg($value, self::$ATTR_SYSTEM);
116
    }
117
118
    /**
119
     * Set the Hidden file attribute.
120
     *
121
     * @param boolean $value
122
     */
123
    public function setHidden($value)
124
    {
125
        $this->addArg($value, self::$ATTR_HIDDEN);
126
    }
127
128
    /**
129
     * Check the attributes.
130
     *
131
     * @throws BuildException
132
     */
133
    protected function checkConfiguration()
134
    {
135
        if (!$this->hasAttr()) {
136
            throw new BuildException(
137
                'Missing attribute parameter',
138
                $this->getLocation()
139
            );
140
        }
141
    }
142
143
    /**
144
     * Set the executable.
145
     * This is not allowed, and it always throws a BuildException.
146
     *
147
     * @param  mixed $e
148
     * @throws BuildException
149
     */
150
    public function setExecutable($e): void
151
    {
152
        throw new BuildException(
153
            $this->getTaskType() . ' doesn\'t support the executable attribute',
154
            $this->getLocation()
155
        );
156
    }
157
158
    /**
159
     * Add source file.
160
     * This is not allowed, and it always throws a BuildException.
161
     *
162
     * @param  boolean $b ignored
163
     * @throws BuildException
164
     */
165
    public function setAddsourcefile(bool $b)
166
    {
167
        throw new BuildException(
168
            $this->getTaskType()
169
            . ' doesn\'t support the addsourcefile attribute',
170
            $this->getLocation()
171
        );
172
    }
173
174
    /**
175
     * Set max parallel.
176
     * This is not allowed, and it always throws a BuildException.
177
     *
178
     * @param  int $max ignored
179
     * @throws BuildException
180
     */
181
    public function setMaxParallel($max)
182
    {
183
        throw new BuildException(
184
            $this->getTaskType()
185
            . ' doesn\'t support the maxparallel attribute',
186
            $this->getLocation()
187
        );
188
    }
189
190
    /**
191
     * Set parallel.
192
     * This is not allowed, and it always throws a BuildException.
193
     *
194
     * @param  boolean $parallel ignored
195
     * @throws BuildException
196
     */
197
    public function setParallel(bool $parallel)
198
    {
199
        throw new BuildException(
200
            $this->getTaskType()
201
            . ' doesn\'t support the parallel attribute',
202
            $this->getLocation()
203
        );
204
    }
205
206
    private static function getSignString($attr)
207
    {
208
        return ($attr ? self::$SET : self::$UNSET);
209
    }
210
211
    private function addArg($sign, $attribute)
212
    {
213
        $this->createArg()->setValue(self::getSignString($sign) . $attribute);
214
        $this->attr = true;
215
    }
216
217
    /**
218
     * @return bool
219
     */
220
    private function hasAttr()
221
    {
222
        return $this->attr;
223
    }
224
}
225