Completed
Push — master ( b30e53...9fbf6c )
by Victor
01:55
created

TextFileWriterValidator::validateIsWriteable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: victor
5
 * Date: 10/04/16
6
 * Time: 23:33
7
 */
8
9
namespace LazyEight\DiTesto\Validator;
10
11
12
use LazyEight\DiTesto\Exceptions\InvalidFileLocationException;
13
use LazyEight\DiTesto\ValueObject\FileLocation;
14
use LazyEight\DiTesto\ValueObject\TextFile\TextFile;
15
16
class TextFileWriterValidator extends AbstractTextFileValidator
17
{
18
    /**
19
     * @var TextFile
20
     */
21
    private $file;
22
23
    /**
24
     * TextFileWriterValidator constructor.
25
     * @param TextFile $file
26
     */
27
    public function __construct(TextFile $file)
28
    {
29
        $this->file = $file;
30
    }
31
32
    /**
33
     * @throws InvalidFileLocationException
34
     * @return bool
35
     */
36
    public function validate()
37
    {
38
        return $this->validateFilePath();
39
    }
40
41
    /**
42
     * @throws InvalidFileLocationException
43
     * @return bool
44
     */
45
    protected function validateFile()
46
    {
47
        if ($this->file->exists()) {
48
            return $this->validateIsWriteable();
49
        }
50
        return $this->validateFilePath();
51
    }
52
53
    /**
54
     * @throws InvalidFileLocationException
55
     * @return bool
56
     */
57
    protected function validateIsWriteable()
58
    {
59
        if (!$this->file->isWritable()) {
60
            throw new InvalidFileLocationException('The file is not writable.', 104);
61
        }
62
        return true;
63
    }
64
65
    /**
66
     * @throws InvalidFileLocationException
67
     * @return bool
68
     */
69
    protected function validateFilePath()
70
    {
71
        $fileDir = $this->file->getLocation()->getFileDirectory();
72
        if (!is_writable($fileDir->getValue())) {
73
            throw new InvalidFileLocationException("The directory {$fileDir->getValue()} is not writable.", 103);
74
        }
75
        return true;
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81
    protected function getFileLocation()
82
    {
83
        return $this->file->getLocation();
84
    }
85
}