Completed
Push — master ( a4b34c...4714ce )
by Rougin
02:31
created

BaseValidator::fails()   D

Complexity

Conditions 9
Paths 6

Size

Total Lines 40
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 9

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 40
ccs 20
cts 20
cp 1
rs 4.909
cc 9
eloc 22
nc 6
nop 0
crap 9
1
<?php
2
3
namespace Rougin\Combustor\Validator;
4
5
/**
6
 * Base Validator
7
 *
8
 * Checks if Wildfire or Doctrine is available
9
 * 
10
 * @package Combustor
11
 * @author  Rougin Royce Gutib <[email protected]>
12
 */
13
class BaseValidator implements ValidatorInterface
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $file = [];
19
20
    /**
21
     * @var boolean
22
     */
23
    protected $isCamel = false;
24
25
    /**
26
     * @var string
27
     */
28
    protected $library = '';
29
30
    /**
31
     * @var string
32
     */
33
    protected $message = '';
34
35
    /**
36
     * @param boolean $isCamel
37
     * @param array   $file
38
     */
39
    public function __construct($isCamel, $file)
40
    {
41
        $this->file = $file;
42
        $this->isCamel = $isCamel;
43
    }
44
45
    /**
46
     * Checks if the validator fails.
47
     * 
48
     * @return boolean
49
     */
50
    public function fails()
51 30
    {
52
        $hasDoctrine = file_exists(APPPATH . 'libraries/Doctrine.php');
53 30
        $hasWildfire = file_exists(APPPATH . 'libraries/Wildfire.php');
54 30
55 30
        if ( ! $hasWildfire && ! $hasDoctrine) {
56 30
            $this->message = 'Please install Wildfire or Doctrine!';
57 30
58
            return true;
59
        }
60
61
        if ($hasWildfire && $hasDoctrine) {
62
            $this->message = 'Both Wildfire and Doctrine exists! Choose only one.';
63
64 30
            return true;
65
        }
66 30
67 30
        if ($hasWildfire && $this->isCamel) {
68
            $this->message = 'Wildfire does not support camel casing!';
69 30
70 6
            return true;
71
        }
72 6
73
        if (file_exists($this->file['path'])) {
74
            $name = $this->file['name'];
75 24
            $type = $this->file['type'];
76 3
77
            $this->message = 'The "' . $name . '" ' . $type . ' already exists!';
78 3
79
            return true;
80
        }
81 21
82 3
        if ($hasDoctrine) {
83 3
            $this->library = 'doctrine';
84
        } else {
85 3
            $this->library = 'wildfire';
86
        }
87 3
88
        return false;
89
    }
90 21
91 3
    /**
92
     * Gets the rendered message.
93 3
     * 
94
     * @return string
95
     */
96 18
    public function getMessage()
97 6
    {
98 6
        return $this->message;
99
    }
100 18
101
    /**
102 12
     * Gets the selected library.
103 12
     * 
104
     * @return string
105 18
     */
106
    public function getLibrary()
107
    {
108
        return $this->library;
109
    }
110
}
111