BaseValidator::fails()   B
last analyzed

Complexity

Conditions 9
Paths 6

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 21
c 1
b 0
f 0
nc 6
nop 0
dl 0
loc 39
ccs 22
cts 22
cp 1
crap 9
rs 8.0555
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 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 30
    public function __construct($isCamel, $file)
40
    {
41 30
        $this->file = $file;
42 30
        $this->isCamel = $isCamel;
43 30
    }
44
45
    /**
46
     * Checks if the validator fails.
47
     *
48
     * @return boolean
49
     */
50 30
    public function fails()
51
    {
52 30
        $hasDoctrine = file_exists(APPPATH . 'libraries/Doctrine.php');
0 ignored issues
show
Bug introduced by
The constant Rougin\Combustor\Validator\APPPATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
53 30
        $hasWildfire = file_exists(APPPATH . 'libraries/Wildfire.php');
54
55 30
        if (! $hasWildfire && ! $hasDoctrine) {
56 6
            $this->message = 'Please install Wildfire or Doctrine!';
57
58 6
            return true;
59
        }
60
61 24
        if ($hasWildfire && $hasDoctrine) {
62 3
            $this->message = 'Both Wildfire and Doctrine exists! Choose only one.';
63
64 3
            return true;
65
        }
66
67 21
        if ($hasWildfire && $this->isCamel) {
68 3
            $this->message = 'Wildfire does not support camel casing!';
69
70 3
            return true;
71
        }
72
73 18
        if (file_exists($this->file['path'])) {
74 3
            $name = $this->file['name'];
75 3
            $type = $this->file['type'];
76
77 3
            $this->message = 'The "' . $name . '" ' . $type . ' already exists!';
78
79 3
            return true;
80
        }
81
82 18
        if ($hasDoctrine) {
83 6
            $this->library = 'doctrine';
84 4
        } else {
85 12
            $this->library = 'wildfire';
86
        }
87
88 18
        return false;
89
    }
90
91
    /**
92
     * Gets the rendered message.
93
     *
94
     * @return string
95
     */
96 15
    public function getMessage()
97
    {
98 15
        return $this->message;
99
    }
100
101
    /**
102
     * Gets the selected library.
103
     *
104
     * @return string
105
     */
106 18
    public function getLibrary()
107
    {
108 18
        return $this->library;
109
    }
110
}
111