Completed
Push — master ( 267f53...0f0d6a )
by Rougin
04:34
created

BaseValidator   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 115
ccs 34
cts 34
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getLibrary() 0 4 1
C fails() 0 43 13
A getMessage() 0 4 1
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 boolean
27
     */
28
    protected $isDoctrine = false;
29
30
    /**
31
     * @var boolean
32
     */
33
    protected $isWildfire = false;
34
35
    /**
36
     * @var string
37
     */
38
    protected $library = '';
39
40
    /**
41
     * @var string
42
     */
43
    protected $message = '';
44
45
    /**
46
     * @param boolean $isDoctrine
47
     * @param boolean $isWildfire
48
     * @param boolean $isCamel
49
     * @param array   $file
50
     */
51 27
    public function __construct($isDoctrine, $isWildfire, $isCamel, $file)
52
    {
53 27
        $this->file = $file;
54 27
        $this->isCamel = $isCamel;
55 27
        $this->isWildfire = $isWildfire;
56 27
        $this->isDoctrine = $isDoctrine;
57 27
    }
58
59
    /**
60
     * Checks if the validator fails.
61
     * 
62
     * @return boolean
63
     */
64 27
    public function fails()
65
    {
66 27
        $hasDoctrine = file_exists(APPPATH . 'libraries/Doctrine.php');
67 27
        $hasWildfire = file_exists(APPPATH . 'libraries/Wildfire.php');
68
69 27
        if ( ! $hasWildfire && ! $hasDoctrine) {
70 3
            $this->message = 'Please install Wildfire or Doctrine!';
71
72 3
            return true;
73
        }
74
75 24
        if ($hasWildfire && $hasDoctrine) {
76 3
            $this->message = 'Please select "--wildfire" or "--doctrine"!';
77
78 3
            return true;
79
        }
80
81 21
        if (file_exists($this->file['path'])) {
82 3
            $name = $this->file['name'];
83 3
            $type = $this->file['type'];
84
85 3
            $this->message = 'The "' . $name . '" ' . $type . ' already exists!';
86
87 3
            return true;
88
        }
89
90 21
        if (($this->isWildfire || $hasWildfire) && $this->isCamel) {
91 3
            $this->message = 'Wildfire does not support camel casing!';
92
93 3
            return true;
94
        }
95
96 18
        if ($this->isDoctrine || $hasDoctrine) {
97 6
            $this->library = 'doctrine';
98 6
        }
99
100 18
        if ($this->isWildfire || $hasWildfire) {
101
102 12
            $this->library = 'wildfire';
103 12
        }
104
105 18
        return false;
106
    }
107
108
    /**
109
     * Gets the rendered message.
110
     * 
111
     * @return string
112
     */
113 12
    public function getMessage()
114
    {
115 12
        return $this->message;
116
    }
117
118
    /**
119
     * Gets the selected library.
120
     * 
121
     * @return string
122
     */
123 18
    public function getLibrary()
124
    {
125 18
        return $this->library;
126
    }
127
}
128